How to make bitmap transparent?

前端 未结 6 812
夕颜
夕颜 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: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

提交回复
热议问题