如何在android中屏蔽以获得此效果?

时间:2015-07-19 05:22:41

标签: android masking

http://www.flashfridge.com/tutorial.asp?ID=135

我想在android中有类似的屏蔽效果。我想移动模糊的椭圆形,因为用户移动他的手指,我希望椭圆形掩盖底层位图,这样我只能看到位图的那一部分,但不知道如何在android中获得此效果。

1 个答案:

答案 0 :(得分:1)

您可以使用PorterDuff.Mode.DST_IN来实现此目标:

public static Bitmap getMaskedBitmap(Resources res, int sourceResId, int maskResId) {
  BitmapFactory.Options options = new BitmapFactory.Options();
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    options.inMutable = true;
  }
  options.inPreferredConfig = Bitmap.Config.ARGB_8888;
  Bitmap source = BitmapFactory.decodeResource(res, sourceResId, options);
  Bitmap bitmap;
  if (source.isMutable()) {
    bitmap = source;
  } else {
    bitmap = source.copy(Bitmap.Config.ARGB_8888, true);
    source.recycle();
  }
  bitmap.setHasAlpha(true);
  Canvas canvas = new Canvas(bitmap);
  Bitmap mask = BitmapFactory.decodeResource(res, maskResId);
  Paint paint = new Paint();
  paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
  canvas.drawBitmap(mask, 0, 0, paint);
  mask.recycle();
  return bitmap;
}

完整教程here