我的客户想要应用程序中的以下小部件: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);
}
第一个问题:是否可以改进此代码以使用更少的资源?
第二个问题:如何将文本添加到finalBitmap
(现在它是与图像一起TextView
显示在顶部的ImageView
)?