android - 使用 Path 从 Bitmap 中剪切区域

标签 android android-canvas android-bitmap android-paint

我正在尝试从图像中剪下一 block 拼图,创建一个新的 Bitmap 图像。我正在使用 Path 对象来执行此操作。这是当前的结果。

enter image description here

我是如何做到这一点的

    Path path = new Path();
    // Multiple path methods to create shape of puzzle piece...
    path.close();

    Bitmap source = BitmapFactory.decodeResource(getResources(), R.drawable.flowers);
    Bitmap workingCopy = source.copy(Bitmap.Config.ARGB_8888, true);
    Canvas canvas = new Canvas(workingCopy);
    path.setFillType(Path.FillType.INVERSE_WINDING);
    Paint paint = new Paint();
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
    canvas.drawPath(path, paint);

    ((ImageView) findViewById(R.id.myImage)).setImageBitmap(workingCopy);

我希望我可以让它透明而不是黑色,并切掉 path 边界之外的所有内容。

最佳答案

我试过使用具有透明度的 PNG 文件,背景是透明的而不是黑色。

关于android - 使用 Path 从 Bitmap 中剪切区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41486217/

相关文章:

iphone - 保持同一应用程序的 android 和 iphone 版本的最佳策略是什么

java - 如何正确删除Android中按钮周围的填充(或边距?)?

安卓 M : Canvas strokeWidth and strokeStyle issue while drawing arcs

android - res/中无法识别目录的 build.xml 错误

android - SurfaceView 实现 Runnable - 线程不启动

android - 在 Canvas 中旋转文本

Android Picasso 将图像加载到 Bitmap 变量并将其裁剪为正方形

java - 位图而不是 View 上的波纹动画

java - Android 图形 - 拖动位图时清除 Canvas

android - 无法在android webview中选择选项