我正在尝试使用 android canvas 将圆切割成半圆。圆是使用 Bitmap 类加载的。
例子如下:
我一直在寻找任何解决方案,尤其是那些使您能够使用坐标裁剪位图的解决方案,但无济于事。
感谢任何帮助,在此之前感谢..
最佳答案
我之前遇到过同样的挑战,我用简单的方法解决了它,主要思想很简单!使用位图掩码,用最高整数值 (0xFFFFFFFF) 填充要保存的像素(在本例中为饼图),因此您可以使用按位AND
来获得结果颜色, mask 的其他像素 Bitmap
将是透明的黑色 (0x00000000),完成 mask 后,创建结果 Bitmap
并填充按照以下方法处理像素:
public Bitmap applyPieMask(Bitmap src, float startAngle, float sweepAngle) {
int width = src.getWidth();
int height = src.getHeight();
//create bitmap mask with the same dimension of the src bitmap
Bitmap mask = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(mask);
canvas.drawColor(0x00000000);//fill mask bitmap with transparent black!
//init mask paint
Paint maskPaint = new Paint();
maskPaint.setColor(0xFFFFFFFF);//pick highest value for bitwise AND operation
maskPaint.setAntiAlias(true);
//choose entire bitmap as a rect
RectF rect = new RectF(0, 0, width, height);
canvas.drawArc(rect, startAngle, sweepAngle, true, maskPaint);//mask the pie
Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
//combine src color and mask to gain the result color
int color = mask.getPixel(i, j) & src.getPixel(i, j);
result.setPixel(i, j, color);
}
}
return result;
}
我们开始吧......
public void doIt(View view) {
ImageView imageView = (ImageView) findViewById(R.id.iv);
Bitmap src = Bitmap.createBitmap(500, 500, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(src);
canvas.drawColor(Color.BLUE);//fill src bitmap with blue color
imageView.setImageBitmap(applyPieMask(src, -90, 60));
}
希望对你有帮助
关于java - Android位图 - 将圆裁剪成半圆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36318851/