1

我正在尝试使用 canvas.clipPath() 在类似于 APP 演示中的 Clipping 活动的约束圈内绘制位图图像。问题是我的代码只能在模拟器上正确呈现,当在实际的三星 Galaxy Nexus 4.2 上运行时,它看起来好像 clipPath 更像是一个矩形剪辑。我完全被难住了!我创建一个新的 Path() 并在我的视图 ctor 中解码位图。有什么建议么?

@Override
protected void onDraw(Canvas canvas) {
    Point point = getPoint();
    path.reset();
    canvas.clipPath(path); // makes the clip empty
    path.addCircle(point.x, point.y, getScale() * 140, Path.Direction.CCW);
    canvas.clipPath(path, Region.Op.REPLACE);

    Point scaledSize = new Point((int) (bitmapSize.x * getScale()), (int) (bitmapSize.y * getScale()));
    Point topLeft = new Point((point.x - (scaledSize.x / 2)), (point.y - (scaledSize.y / 2)));
    canvas.drawBitmap(bitmap, null, new Rect(topLeft.x, topLeft.y, topLeft.x + scaledSize.x, topLeft.y + scaledSize.y), paint);
}

银河连结 银河系

模拟器

模拟器

4

3 回答 3

4

硬件加速不支持 ClipPath

您可以使用以下命令关闭硬件加速:

setLayerType(View.LAYER_TYPE_SOFTWARE, null);
于 2013-07-09T03:28:16.467 回答
3

硬件加速不支持 clipPath。您可以使用以下内容创建剪切位图:

Bitmap clippedBitmap = ... // Create same size bitmap as source
Paint paint = new Paint();
Canvas canvas = new Canvas(clippedBitmap);
paint.setColor(Color.RED);
paint.setStyle(PAint.Style.FILL);
paint.setFilterBitmap(true);
canvas.drawCircle(cx, cy, radius, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));    
canvas.drawBitmap(sourceBitmap, 0, 0, paint);

这样做一次,然后绘制剪辑的位图而不是源位图

于 2013-06-03T04:28:49.447 回答
2

硬件加速不支持 ClipPath。检查以下主题不支持的绘图操作下的链接。

http://developer.android.com/guide/topics/graphics/hardware-accel.html#drawing-support

您可以参考以下内容并修改绘制圆的参数以满足您的需要。

public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    DrawingView dv = new DrawingView(this);
    dv.setBackgroundColor(Color.RED);
    setContentView(dv);
}

class DrawingView extends View{
Bitmap bitmap;


 public DrawingView(Context context)
 {
 super(context);
 bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.sqaure);   
}


@Override
public void onDraw(Canvas canvas)
{
 Paint paint = new Paint();
 //paint.setStyle(Paint.Style.FILL);
 // paint.setColor(Color.CYAN);
 canvas.drawBitmap(getclip(), 0, 0, paint);//originally x and y is o and o .

 } 
 public Bitmap getclip()
 {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
        bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(),
        bitmap.getHeight());
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.FILL);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
//paint.setColor(color);
canvas.drawCircle(bitmap.getWidth() / 2-40,
        bitmap.getHeight() / 2, bitmap.getWidth() / 2-40, paint);
    // change the parameters accordin to your needs.
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
    }
  } 
}

在此处输入图像描述

于 2013-06-03T05:35:36.170 回答