我的客户想在应用程序中使用以下小部件:
在Text来自服务器.梯度角取决于也来自服务器的变量.此外,客户希望动态填充渐变(用户必须从0开始查看渐变的填充方式).
现在我执行以下操作:我使用两个图像 - 一个是彩色圆圈,第二个是灰色圆圈.我创建一个具有一定角度的圆弧段并将其作为遮罩应用于灰色圆圈,然后将彩色圆圈与新的灰色圆圈(扇形截止的位置)组合在一起.
这是我的代码.我初始化调用的变量initializeVarsForCompoundImDrawing,然后在第二次调用中调用几次,makeCompoundImage最后调用nullVarsForCompoundImDrawing以释放资源:
private static Bitmap notColoredBitmap;
private static Bitmap coloredBitmap;
private static Bitmap notColoredWithMaskBitmap;
private static Bitmap finalBitmap;
private static Canvas notColoredWithMaskCanvas;
private static Paint paintForMask;
private static Paint smoothPaint;
private static Canvas finalCanvas;
private static RectF rectForMask;
public static void initializeVarsForCompoundImDrawing()
{
Context context = MainApplication.getContext();
notColoredBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.not_colored);
coloredBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.colored);
paintForMask = new Paint(Paint.ANTI_ALIAS_FLAG);
paintForMask.setStyle(Paint.Style.FILL);
paintForMask.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
rectForMask = new RectF(0, 0, notColoredBitmap.getWidth(), notColoredBitmap.getHeight());
smoothPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
}
public static void nullVarsForCompoundImDrawing()
{
notColoredBitmap = null;
coloredBitmap = null;
paintForMask = null;
rectForMask = null;
smoothPaint = null;
}
public static void makeCompoundImage(ImageView imageView, int angle)
{
notColoredWithMaskBitmap = Bitmap.createBitmap(notColoredBitmap.getWidth(), notColoredBitmap.getHeight(), Bitmap.Config.ARGB_8888);
notColoredWithMaskCanvas = new Canvas(notColoredWithMaskBitmap);
notColoredWithMaskCanvas.drawBitmap(notColoredBitmap, 0, 0, smoothPaint);
notColoredWithMaskCanvas.drawArc(rectForMask, 270, angle, true, paintForMask);
finalBitmap = Bitmap.createBitmap(notColoredBitmap.getWidth(), notColoredBitmap.getHeight(), Bitmap.Config.ARGB_8888);
finalCanvas = new Canvas(finalBitmap);
finalCanvas.drawBitmap(coloredBitmap, 0, 0, smoothPaint);
finalCanvas.drawBitmap(notColoredWithMaskBitmap, 0, 0, smoothPaint);
imageView.setImageBitmap(finalBitmap);
}
Run Code Online (Sandbox Code Playgroud)
第一个问题:是否可以改进此代码以使用更少的资源?
第二个问题:如何将文本添加到finalBitmap(现在它TextView是显示在ImageView图像顶部的文本)?
回答你的问题:是的,它可以更简单,更轻松地完成:
public class Ring extends View {
private Bitmap mBack;
private Paint mPaint;
private RectF mOval;
private Paint mTextPaint;
public Ring(Context context) {
super(context);
Resources res = getResources();
mBack = BitmapFactory.decodeResource(res, R.drawable.back);
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
Bitmap ring = BitmapFactory.decodeResource(res, R.drawable.ring);
mPaint.setShader(new BitmapShader(ring, TileMode.CLAMP, TileMode.CLAMP));
mOval = new RectF(0, 0, mBack.getWidth(), mBack.getHeight());
mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mTextPaint.setTextSize(24);
mTextPaint.setTextAlign(Align.CENTER);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.translate((getWidth() - mBack.getWidth()) / 2, (getHeight() - mBack.getHeight()) / 2);
canvas.drawBitmap(mBack, 0, 0, null);
float angle = 220;
canvas.drawArc(mOval, -90, angle, true, mPaint);
canvas.drawText("Text",
mBack.getWidth() / 2,
(mBack.getHeight() - mTextPaint.ascent()) / 2,
mTextPaint);
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:
这是一个替代解决方案(没有居中,没有文字,只是一个概念)
class Ring extends View {
private Bitmap back;
private Bitmap ring;
private RectF oval;
private Paint arcPaint;
public Ring(Context context) {
super(context);
Resources res = getResources();
back = BitmapFactory.decodeResource(res, R.drawable.back);
ring = BitmapFactory.decodeResource(res, R.drawable.ring);
arcPaint = new Paint();
arcPaint.setXfermode(new PorterDuffXfermode(Mode.CLEAR));
oval = new RectF(-1, -1, ring.getWidth()+1, ring.getHeight()+1);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawARGB(0xaa, 0, 255, 0);
canvas.drawBitmap(back, 0, 0, null);
canvas.saveLayer(oval, null, Canvas.HAS_ALPHA_LAYER_SAVE_FLAG);
canvas.drawBitmap(ring, 0, 0, null);
float angle = 300;
canvas.drawArc(oval, angle-90, 360-angle, true, arcPaint);
canvas.restore();
}
}
Run Code Online (Sandbox Code Playgroud)
您可以使用我的项目作为示例https://github.com/donvigo/CustomProgressControls.它还没有在github上记录,但我希望你能理解我的代码:)
编辑:圆圈内的文字它不是a TextView,它是使用绘制的paint.
| 归档时间: |
|
| 查看次数: |
9697 次 |
| 最近记录: |