How to make the canvas drawing area transparent in android?

I would like to make the Canvas area transparent, because I would like to add an image behind it so that the Canvas acts on the image. My canvas code is here

public class Panel extends SurfaceView implements SurfaceHolder.Callback { private ViewThread mThread; private ArrayList<Element> mElements = new ArrayList<Element>(); public Panel(Context context, AttributeSet attrs) { super(context, attrs); getHolder().addCallback(this); mThread = new ViewThread(this); } public void doDraw(Canvas canvas) { canvas.drawColor(Color.TRANSPARENT); synchronized (mElements) { for (Element element : mElements) { element.doDraw(canvas); } } } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // TODO Auto-generated method stub } @Override public void surfaceCreated(SurfaceHolder holder) { if (!mThread.isAlive()) { mThread = new ViewThread(this); mThread.setRunning(true); mThread.start(); } } @Override public void surfaceDestroyed(SurfaceHolder holder) { if (mThread.isAlive()) { mThread.setRunning(false); } } @Override public boolean onTouchEvent(MotionEvent event) { synchronized (mElements) { mElements.add(new Element(getResources(), (int) event.getX(), (int) event.getY())); } return super.onTouchEvent(event); } 

}

How to achieve this, any fragments on it will be very useful. thanks

+6
source share
8 answers

I got the result of 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); } 
+23
source

If you want to have a canvas with a transparent background, you need to adjust the background image for

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!

+12
source
 canvas.drawColor(Color.argb(0, 255, 255, 255)); 

The first attribute is alpha, and the rest are RGB colors.

or

 canvas.drawColor(Color.TRANSPARENT); 
+6
source

In your onDraw() method add this:

 canvas.drawColor(0x00AAAAAA); 

This will make your canvas transparent and the background View will be visible.

+4
source

this makes canvas tramsparent and works for me :)

 canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.OVERLAY); 
+1
source

well, I'm not sure about drawing a transparent canvas, but in your case you can do a trick that draws a canvas using a background image.

And then you can draw / draw with your finger on it.

Code example:

  BitmapDrawable bd = (BitmapDrawable)<YOUR_ACTIVITY>.this.getResources().getDrawable(R.drawable.<DRAWBLE_ID>); Bitmap b = bd.getBitmap(); mBitmap = Bitmap.createBitmap(b,0,0,100,100); // This line is required only if you wanna some change in the bitmap you created mCanvas = new Canvas(mBitmap); 
0
source

Set the background color of the canvas background to # 00000000

0
source

You can create a bitmap of the same size as the canvas. Remove all the colors of the bitmap using Color.TRANSPARENCY and set it as a canvas bitmap:

 Bitmap transparentBitmap = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888); transparentBitmap.eraseColor(Color.TRANSPARENT); canvas.setBitmap(transparentBitmap); 
0
source

Source: https://habr.com/ru/post/904925/


All Articles