2012-08-28 49 views
1

可能重複:
Set alpha of Bitmap image!如何動態地更改位圖的不透明度的Android

我想改變位圖的透明度上的搜索條的進度而變化。我正在使用下面的代碼。

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; 




        } 
       }); 

但是,當我嘗試這樣做。與seekbar相比,它可以非常快地改變不透明度。即使當我們增加搜索欄時,它總是會降低不透明度。我在做什麼錯?請分享你的建議。

+0

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

+0

嘗試先搜索http://stackoverflow.com/search?q=[android]+bitmap+opacity – Merlin

回答

2

可以使用Color.argb ..這裏是示例代碼。

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

其中alpha是不透明,所以你可以使用你的阿爾法值陸侃..

這裏APLHA,紅,綠&藍色必須是0到255

你的情況

你必須通過每次相同的RGB只是改變阿爾法值

4

使用BitmapDrawable而不是直接與Bitmap一起使用。

這是很容易改變的不透明度:

BitmapDrawable bmpDrawable = new BitmapDrawable(getResources(), bitmap); 
bmpDrawable.setAlpha(150); 
+0

我需要得到的最終結果爲位圖 –

+0

在這種情況下,您可以創建一個新的位圖,並繪製BitmapDrawable位圖。 – Dalmas

+0

我試過這段代碼。但它一次又一次地返回相同的位圖。我認爲它只適用於不透明度的靜態變化而不是動態變化。 –

相關問題