2

我在图像视图中显示图像并应用一些滤色器,这是我的代码

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_2);

    SeekBarTxt=(TextView)findViewById(R.id.value_txt);
    iv=(ImageView)findViewById(R.id.img_vw1);

    sb_red = (SeekBar)findViewById(R.id.seek_bar1);
    sb_green = (SeekBar)findViewById(R.id.seek_bar_green);
    sb_blue = (SeekBar)findViewById(R.id.seek_bar_blue);

    sb_red.setOnSeekBarChangeListener(colorBarChangeListener);
    sb_green.setOnSeekBarChangeListener(colorBarChangeListener);
    sb_blue.setOnSeekBarChangeListener(colorBarChangeListener);

    setColorFilter(iv);
}

OnSeekBarChangeListener colorBarChangeListener=new OnSeekBarChangeListener() {

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub
        seekBar.setSecondaryProgress(seekBar.getProgress());
        //SeekBarTxt.setText("ended tracking touch");
    }

    @Override  
    public void onStartTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub
        //SeekBarTxt.setText("starting to track touch");                
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress,
            boolean fromUser) {
        // TODO Auto-generated method stub
        //SeekBarTxt.setText(Integer.toString(progress)+"%");       
        setColorFilter(iv);  
    }
};

private void setColorFilter(ImageView iv)  
{       

    float redValue,greenValue,blueValue;

    if(sb_red.getProgress()!=0 || sb_green.getProgress()!=0 || sb_blue.getProgress()!=0)
    {       
        redValue = ((float)sb_red.getProgress())/255;  
        greenValue = ((float)sb_green.getProgress())/255;   
        blueValue = ((float)sb_blue.getProgress())/255;  
    }
    else
    {
        redValue = 1;  
        greenValue = 1;  
        blueValue = 1;  
    }

     float[] colorMatrix = { 
               redValue, 0, 0, 0, 0,  //red
               0, greenValue, 0, 0, 0, //green
               0, 0, blueValue, 0, 0,  //blue
               0, 0, 0, 1, 0    //alpha    
         }; 

    SeekBarTxt.setText(redValue+"%");
    ColorFilter colorFilter = new ColorMatrixColorFilter(colorMatrix);

    iv.setColorFilter(colorFilter);
}

现在我的问题是当我滑动搜索栏的小图像立即变黑时,我想在图像上慢慢地应用颜色效果。我怎么能做到这一点。

请帮忙!

4

0 回答 0