Search code examples
androidopacityalphaseekbar

How to dynamically change the opacity of the bitmap in android


Possible Duplicate:
Set alpha of Bitmap image!

I want to change the opacity of bitmap on change of seekbar's progress. I am using the below code.

opacityseekbar.setMax(255);
opacityseekbar.setProgress(255);
opacityseekbar.setOnSeekBarChangeListener( new OnSeekBarChangeListener()
                {

                    public void onStopTrackingTouch(SeekBar arg0) {
                        // TODO Auto-generated method stub


                    }

                    public void onStartTrackingTouch(SeekBar arg0) {
                        // TODO Auto-generated method stub

                    }

                    public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
                        // TODO Auto-generated method stub
                        Log.e("Progress Value", "Progress number" + arg1);


                        Bitmap mutableBitmap = theTattoo.isMutable()? theTattoo: theTattoo.copy(Bitmap.Config.ARGB_8888, true);
                        Canvas canvas = new Canvas(mutableBitmap);
                        int colour = ((opacityseekbar.getProgress() << 24) & 0xFF000000);
                        canvas.drawColor(colour, Mode.DST_IN);

                        theTattoo=mutableBitmap;




                    }
                });

But, when I try to do this. It change the opacity very fast as comparison to the seekbar. Also It always decreases the opacity even when when we increase the seek bar. What is wrong I am doing? Please share your suggestions.


Solution

  • you can use Color.argb .. here is sample code.

    canvas.drawColor(Color.argb(alpha, red, green, blue));
    

    where alpha is opacity so you can use your progess value in alpha..

    here aplha, red, green & blue must be 0 to 255

    in your case you have to pass everytime same RGB just change Alpha value