How to make Canvas draw area transparent in android?

前端 未结 9 1837
悲&欢浪女
悲&欢浪女 2020-12-08 09:01

I would like to make Canvas area transparent because I would like to add an Image behind it so that Canvas actions happen above the image.My code for the canvas is here

相关标签:
9条回答
  • 2020-12-08 09:02

    If you want to have a canvas with a transparent background you have to configure the background image for the

    mCanvas = new Canvas(mBackgroundBitmap);

    with Bitmap.Config.ARGB_4444

    And use colors like 0x00000000 as transparent

        Bitmap mBackgroundImage = Bitmap.createBitmap(Size, Size,
                        Bitmap.Config.ARGB_4444);
        mCanvas = new Canvas(mBackgroundImage);
    

    hope this helps! I have a pretty transparent canvas :D yay!

    0 讨论(0)
  • 2020-12-08 09:14

    I got the output by this

     public Panel(Context context, AttributeSet attrs) { 
        super(context, attrs); 
        this.setBackgroundColor(Color.TRANSPARENT);                 
        this.setZOrderOnTop(true); //necessary                
        getHolder().setFormat(PixelFormat.TRANSPARENT); 
        getHolder().addCallback(this); 
        mThread = new ViewThread(this); 
    
    } 
    
    0 讨论(0)
  • 2020-12-08 09:14
    canvas.drawColor(Color.argb(0, 255, 255, 255));
    

    first attribute is alpha and rest are RGB colors.

    or

    canvas.drawColor(Color.TRANSPARENT);
    
    0 讨论(0)
  • 2020-12-08 09:17
    canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR)
    

    Works fine for me with

    override fun onFinishInflate() {
        super.onFinishInflate()
        setZOrderOnTop(true)
        holder.setFormat(PixelFormat.TRANSLUCENT)
        isFocusable = true
        holder.addCallback(surfaceCallback)
    }
    
    0 讨论(0)
  • 2020-12-08 09:18

    this make canvas tramsparent and it is work for me :)

    canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.OVERLAY);
    
    0 讨论(0)
  • 2020-12-08 09:19

    Set canvas's view background color to #00000000

    0 讨论(0)
提交回复
热议问题