使触摸的某个区域的位图透明

我的想法是将两个图像重叠在一起,在onTouch上重叠,顶部图像应在被触摸的半径上变为透明,从而暴露底部图像。

这是我如何覆盖2个图像:

Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig()); Canvas canvas = new Canvas(bmOverlay); canvas.drawBitmap(bmp1, new Matrix(), null); canvas.drawBitmap(bmp2, new Matrix(), null); 

我已经看了这个post,并有一个像下面的油漆,使其透明:

  mPaint = new Paint(); mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC)); mPaint.setColor(Color.TRANSPARENT); mPaint.setAntiAlias(true); public void onDraw(Canvas canvas) { canvas.drawCircle(40, 40, 30, mPaint); //hardcode to test } 

问题是,我认为直接的圆使2个图像在定义的半径上透明,我怎样才能使顶部位图透明?

  • Android 4.4 - 半透明状态/导航条 - fitsSystemWindows / clipToPadding不能通过片段事务工作
  • Android上的透明LinearLayout
  • 用不同的点击状态创build不规则形状的ImageButton
  • 在android中设置形状背景透明
  • VideoView顶部的android透明WebView
  • 工具栏不会在透明状态栏下滚动
  • 使工具栏透明
  • Android图像对话框/popup与图像相同的大小和无边框
  •  import android.app.Activity; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Bitmap.Config; import android.graphics.BlurMaskFilter.Blur; import android.graphics.BitmapFactory; import android.graphics.BlurMaskFilter; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.PorterDuff.Mode; import android.graphics.PorterDuffXfermode; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; public class StartActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(new TouchView(this)); } class TouchView extends View{ Bitmap bgr; Bitmap overlayDefault; Bitmap overlay; Paint pTouch; int X = -100; int Y = -100; Canvas c2; public TouchView(Context context) { super(context); bgr = BitmapFactory.decodeResource(getResources(),R.drawable.bgr); overlayDefault = BitmapFactory.decodeResource(getResources(),R.drawable.over); overlay = BitmapFactory.decodeResource(getResources(),R.drawable.over).copy(Config.ARGB_8888, true); c2 = new Canvas(overlay); pTouch = new Paint(Paint.ANTI_ALIAS_FLAG); pTouch.setXfermode(new PorterDuffXfermode(Mode.SRC_OUT)); pTouch.setColor(Color.TRANSPARENT); pTouch.setMaskFilter(new BlurMaskFilter(15, Blur.NORMAL)); } @Override public boolean onTouchEvent(MotionEvent ev) { switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: { X = (int) ev.getX(); Y = (int) ev.getY(); invalidate(); break; } case MotionEvent.ACTION_MOVE: { X = (int) ev.getX(); Y = (int) ev.getY(); invalidate(); break; } case MotionEvent.ACTION_UP: break; } return true; } @Override public void onDraw(Canvas canvas){ super.onDraw(canvas); //draw background canvas.drawBitmap(bgr, 0, 0, null); //copy the default overlay into temporary overlay and punch a hole in it c2.drawBitmap(overlayDefault, 0, 0, null); //exclude this line to show all as you draw c2.drawCircle(X, Y, 80, pTouch); //draw the overlay over the background canvas.drawBitmap(overlay, 0, 0, null); } } } 

    呃,你的post里面有答案!

    基本上尝试像这样的:

     public void onDraw(Canvas canvas) { if (myPaintFlag) { canvas.drawBitmap(bmp1, new Matrix(), null); canvas.drawBitmap(bmp2, new Matrix(), mPaint); } else { canvas.drawBitmap(bmp1, new Matrix(), mPaint); canvas.drawBitmap(bmp2, new Matrix(), null); } }