可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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
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 it, any snippets on it will be very much helpful.Thanks
回答1:
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);
}
回答2:
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!
回答3:
canvas.drawColor(Color.argb(0, 255, 255, 255));
first attribute is alpha and rest are RGB colors.
or
canvas.drawColor(Color.TRANSPARENT);
回答4:
In your onDraw()
method add this:
canvas.drawColor(0x00AAAAAA);
This will make your canvas
transparent and background View
will be visible.
回答5:
this make canvas tramsparent and it is work for me :)
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.OVERLAY);
回答6:
You can create a Bitmap of the same size as the canvas. Erase 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);
回答7:
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)
}
回答8:
well, I am not sure about drawing the transparent canvas, but in your case you can do a tweak, that draw the canvas using the background image iteself.
And then you can draw/ paint by 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);
回答9:
Set canvas's view background color to #00000000