如何使位图透明?

问题描述 投票:3回答:5
/**
 * @param bitmap
 *            The source bitmap.
 * @param opacity
 *            a value between 0 (completely transparent) and 255 (completely
 *            opaque).
 * @return The opacity-adjusted bitmap. If the source bitmap is mutable it
 *         will be adjusted and returned, otherwise a new bitmap is created.
 *         Source : http://stackoverflow.com/questions/7392062/android-
 *         semitransparent-bitmap-background-is-black/14858913#14858913
 */
private Bitmap adjustOpacity(Bitmap bitmap, int opacity) {
    Bitmap mutableBitmap = bitmap.isMutable() ? bitmap : bitmap.copy(Bitmap.Config.ARGB_8888, true);
    Canvas canvas = new Canvas(mutableBitmap);
    int colour = (opacity & 0xFF) << 24;
    canvas.drawColor(colour, PorterDuff.Mode.DST_IN);
    return mutableBitmap;
}

使用adjustOpacity,使ImageViewBitmap是半透明的。

Bitmap newBitmap = adjustOpacity(orignalBitmap, 10);
view.setImageBitmap(newBitmap);
view.setBackgroundColor(Color.WHITE);

但是,Imageview在显示白色之前显示更多的暗度。如何使用Bitmap制作半透明(白色背景)Imageview?

android bitmap imageview opacity transparent
5个回答
7
投票
// Convert transparentColor to be transparent in a Bitmap.
public static Bitmap makeTransparent(Bitmap bit, Color transparentColor) {
    int width =  bit.getWidth();
    int height = bit.getHeight();
    Bitmap myBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    int [] allpixels = new int [ myBitmap.getHeight()*myBitmap.getWidth()];
    bit.getPixels(allpixels, 0, myBitmap.getWidth(), 0, 0, myBitmap.getWidth(),myBitmap.getHeight());
    myBitmap.setPixels(allpixels, 0, width, 0, 0, width, height);

    for(int i =0; i<myBitmap.getHeight()*myBitmap.getWidth();i++){
     if( allpixels[i] == transparentColor)

                 allpixels[i] = Color.alpha(Color.TRANSPARENT);
     }

      myBitmap.setPixels(allpixels, 0, myBitmap.getWidth(), 0, 0, myBitmap.getWidth(), myBitmap.getHeight());
      return myBitmap;
}

上面的代码将获取一个位图,并返回一个位图,其中颜色为transparentColor的每个像素都是透明的。这在低至8级的API中起作用,而我还没有对其进行任何更低的测试。

我通常使用Color.RED之类的东西来制作透明像素,因为我在资产中没有使用很多RED,但是如果我这样做,那是自定义红色。


4
投票

尝试一下。另请注意,setAlpha在0-255范围内

//bmp is your Bitmap object

BitmapDrawable bd = new BitmapDrawable(bmp);
bd.setAlpha(50);

ImageView v = (ImageView) findViewById(R.id.image);
v.setImageDrawable(bd);

0
投票

尝试一下,

newBitmap.setImageResource(android.R.color.transparent);

0
投票

@@ akaya的答案是正确的方法,除了不赞成使用构造函数。自API 4以来的新方法是使用:

BitmapDrawable drawable = new BitmapDrawable(getResources(), bitmap);
drawable.setAlpha(100);

0
投票

添加方法

private static Bitmap makeTransparentBitmap(Bitmap bmp, int alpha) {
        Bitmap transBmp = Bitmap.createBitmap(bmp.getWidth(),
                bmp.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(transBmp);
        final Paint paint = new Paint();
        paint.setAlpha(alpha);
        canvas.drawBitmap(bmp, 0, 0, paint);
        return transBmp;
    }



@Override
protected void onDraw(Canvas canvas) {
    canvas.drawColor(Color.rgb(43,44,45));

    bmp = BitmapFactory.decodeResource(getResources(), R.drawable.mouse);
    canvas.drawBitmap(bmp, pos_x, pos_y, null); 

}

您的活动:

bmp = makeTransparentBitmap( bmp, 122 );
DrawView.invalidate();

DrawView

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    DrawView = new DrawView(this);
    setContentView( DrawView );
}

我找到这个答案here

© www.soinside.com 2019 - 2024. All rights reserved.