当用户触摸屏幕时,我尝试隐藏图片并以交互方式显示其部分。我尝试了很多方法,比如背景视图与视图重叠,我会透明地以某种方式工作。最终解决方案应该是一个自定义视图,这将给我更多的绘画灵活性。
的活动:
hiddenPicture.setBackgroundResource(R.drawable.picture);
查看:
init()
eraserPaint = new Paint();
eraserPaint.setColor(Color.TRANSPARENT);
// eraserPaint.setAlpha(0);
eraserPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
veilPaint = new Paint();
veilPaint.setColor(Color.GRAY);
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawRect(0, 0, w, h, veilPaint);
canvas.drawOval(new RectF(20, 20, 220, 220), eraserPaint);
}
问题是椭圆形是黑色的。我发现了大量类似的问题,但我在Android中太初级了,无法将它们应用到我的案例中。如何清除灰色面纱中的椭圆形并显示背景图片?谢谢。
更新
我找到了不错的博客:http://www.41post.com/4794/programming/android-rendering-a-path-with-a-bitmap-fill
我将它回收到我的代码:
fillBMP = BitmapFactory.decodeResource(context.getResources(), R.drawable.picture);
fillBmpShader = new BitmapShader(fillBMP, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
eraserPaint.setColor(0xFFFFFFFF);
eraserPaint.setStyle(Paint.Style.FILL);
eraserPaint.setShader(fillBmpShader);
它似乎有效,但问题是它不会缩放用作画笔的图像。真的是以这种方式实现onDraw()
的唯一解决方案:
我担心性能。我不希望在每次用户交互后绘制完整的屏幕。我更愿意重新绘制相关部分。这甚至可能还是我过度优化了?
答案 0 :(得分:0)
我必须维护并绘制两个位图 - 一个用于背景,第二个用于带透明凹陷的面纱:
private void setupDrawing() {
eraserPaint = new Paint();
eraserPaint.setColor(Color.TRANSPARENT);
eraserPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
veilPaint = new Paint();
veilPaint.setColor(Color.GREEN);
canvasPaint = new Paint(Paint.DITHER_FLAG);
}
// http://developer.android.com/training/custom-views/custom-drawing.html
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
canvasBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
drawCanvas = new Canvas(canvasBitmap);
drawCanvas.drawRect(0, 0, w, h, veilPaint);
}
protected void onDraw(Canvas canvas) {
canvas.drawBitmap(fillBitmap, 0, 0, canvasPaint);
canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint);
}
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_MOVE) {
drawCanvas.drawCircle(event.getX(), event.getY(), 150, eraserPaint);
invalidate();
}
return true;
}