问题描述
我创建一个像素的狩猎游戏。因此,我的活动显示了一个ImageView的。我想创建一个提示告诉我在哪里对象。为此,我需要模糊图像整体除围绕点对象所位于的圆。模糊的而不是我可以给一个公正的半透明的黑色背景。有没有什么问题画一个半透明的矩形画布上。但我不知道如何从它裁剪透明圈。结果应该像这样的:
I am creating a pixel-hunting game. So my activity shows an ImageView. And I want to create a hint "show me where is the object". For this I need to blur whole image except a circle around a point where the object is located. Instead of blur I can show a just semitransparent black background.There is there is no problem to draw a semitransparent rectangle on the Canvas.But I don't know how to crop a transparent circle from it.The result should look like like this:
请帮我实现同样的结果在Android SDK。
Please help me to achieve same result on Android SDK.
推荐答案
所以最后我成功地做到这一点。
So finally I managed to do this.
首先,我借鉴全景半透明的黑色矩形。在使用PorterDuff.Mode.CLEAR之后我剪一个透明圈,以显示猫的地位。
Firstly I draw a semitransparent black rectangle on whole view.After that using PorterDuff.Mode.CLEAR I cut a transparent circle to show cat's position.
我有问题PorterDuff.Mode.CLEAR:首先我得到了一个透明的黑圈代替
I had problem with PorterDuff.Mode.CLEAR: firstly I was getting a black circle instead of a transparent one.
由于罗曼盖伊的跟贴:这里评论我明白,我的窗户是不透明的,我应该利用另一个位图。只有经过借鉴视图的画布。
Thanks to Romain Guy's comments here: comment here I understood that my window is opaque and I should draw on another bitmap. And only after draw on view's canvas.
下面是我的OnDraw方法:
Here is my onDraw method:
private Canvas temp;
private Paint paint;
private Paint p = new Paint();
private Paint transparentPaint;
private void init(){
temp = new Canvas(bitmap);
Bitmap bitmap = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888);
paint = new Paint();
paint.setColor(0xcc000000);
transparentPaint = new Paint();
transparentPaint.setColor(getResources().getColor(android.R.color.transparent));
transparentPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
}
protected void onDraw(Canvas canvas) {
temp.drawRect(0, 0, temp.getWidth(), temp.getHeight(), paint);
temp.drawCircle(catPosition.x + radius / 2, catPosition.y + radius / 2, radius, transparentPaint);
canvas.drawBitmap(bitmap, 0, 0, p);
}
这篇关于Android的画布:利用图像透明圈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!