我正在尝试使用canvas.clipPath()在约束圈内绘制位图图像,类似于APP演示中的剪辑活动。问题是我的代码只能在模拟器上正确呈现,当在实际的三星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);
}
Galaxy Nexus
仿真器
答案 0 :(得分:4)
硬件加速不支持ClipPath
您可以使用此关闭硬件加速:
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
答案 1 :(得分:2)
硬件加速不支持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);
执行一次,然后绘制剪切的位图而不是源位图
答案 2 :(得分: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;
}
}
}