How to make bitmap transparent?

前端 未结 6 746
夕颜
夕颜 2021-01-14 11:40
/**
 * @param bitmap
 *            The source bitmap.
 * @param opacity
 *            a value between 0 (completely transparent) and 255 (completely
 *            op         


        
相关标签:
6条回答
  • 2021-01-14 12:21

    Try this. Also note that setAlpha is in 0-255 range

    //bmp is your Bitmap object
    
    BitmapDrawable bd = new BitmapDrawable(bmp);
    bd.setAlpha(50);
    
    ImageView v = (ImageView) findViewById(R.id.image);
    v.setImageDrawable(bd);
    
    0 讨论(0)
  • 2021-01-14 12:22

    Add method

    Bitmap bmp; DrawView DrawView;
    
    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); 
    
    }
    

    You event:

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

    DrawView is

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        DrawView = new DrawView(this);
        setContentView( DrawView );
    }
    

    I found this answer here

    0 讨论(0)
  • 2021-01-14 12:31

    Making an existing bitmap transparent is too simple.

    If you want to make your bitmap transparent before further drawing follow this.

    if your bitmap is mBitmap then Just use:-

    mBitmap.eraseColor(Color.TRANSPARENT) 
    

    Thats it. Good luck !!

    0 讨论(0)
  • 2021-01-14 12:31

    @akaya's answer is the right way except that the constructor is deprecated. The new way since API 4 would be to use:

    BitmapDrawable drawable = new BitmapDrawable(getResources(), bitmap);
    drawable.setAlpha(100);
    
    0 讨论(0)
  • 2021-01-14 12:32
    // 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;
    }
    

    The above code will take a Bitmap, and return a Bitmap where every pixel which the color is transparentColor is transparent. This works in API as low as level 8, and I have not tested it in any lower.

    I typically use something like Color.RED to make my transparent pixels, because I don't use a lot of RED in my assets, but if I do it's a custom red color.

    0 讨论(0)
  • 2021-01-14 12:35

    try this,

    newBitmap.setImageResource(android.R.color.transparent);
    
    0 讨论(0)
提交回复
热议问题