如何从View对象(SeekBar)发送值到运行后台线程?

时间:2014-02-27 18:27:02

标签: android android-asynctask

我正在开发一个客户端 - 服务器应用程序。 客户端是android。 服务器是我的电脑。 服务器向客户端发送字节数组,客户端从字节数组构建映像并通过ImageView显示它们(逐帧)。

我正在使用AsyncTask打开服务器的套接字并构建图像。到目前为止,一切正常。

现在,我想添加SeekBar以改变图像的亮度。

我的问题是如何将搜索栏值发送到构建图像的函数?

public void onClickConnect(View view)
{
    // connect to the server
    new ConnectTask().execute("");
}

public class ConnectTask extends AsyncTask<String, Bitmap, TcpClient> {

    @Override
    protected TcpClient doInBackground(String... message)
    {
        //we create a TCPClient object and
        mTcpClient = new TcpClient(new TcpClient.OnBitmapReceived() {
            @Override
            public void bitmapReceived(Bitmap bitmap) {
                //this method calls the onProgressUpdate
                publishProgress(bitmap);
            }
        });
        mTcpClient.run();
        return null;
    }

    @Override
    protected void onProgressUpdate(Bitmap... bitmap) {
        super.onProgressUpdate(bitmap); 

        //iv is the ImageView object that display the images
        iv.setImageBitmap(bitmap[0]);
    }
}

public class TcpClient {    
    private OnBitmapReceived mBitmapListener = null;    
    public TcpClient(OnBitmapReceived listener) {
        mBitmapListener = listener;
    }   
    public void run() {     
    mRun = true;        
    //creating a socket to make the connection with the server
    .....       
    while (mRun){           
        .....   
        //build color array
        **// HERE I NEED THE VALUE FROM THE SEEK BAR**          
        for(int i=0;i<bytePerFrame;i=i+2)                   
            {
                pixel = ((frameBytes[i] << 8) & 0x0000ff00) | (frameBytes[i+1] & 0x000000ff);
                colors[pixelIndex]=pixel;
                pixelIndex++;
            }                   
        Bitmap mBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
        Canvas c = new Canvas(mBitmap);
        Paint paint = new Paint();  
        c.drawBitmap(colors, 0, width, 0, 0, width, height, false, paint);          
        if(mBitmap!=null && mBitmapListener != null)
            {
                    mBitmapListener.bitmapReceived(mBitmap);
            }               
        }       
    }
}

1 个答案:

答案 0 :(得分:0)

检查下面的示例以更改图像的亮度。

  

可以通过一起增加/减少R,G,B值来改变图像亮度。

http://xjaphx.wordpress.com/2011/06/22/image-processing-brightness-over-image/