gpt4 book ai didi

android - 动态绘制自定义圆段

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:00:40 25 4
gpt4 key购买 nike

我的客户想要应用程序中的以下小部件:
enter image description here
文本来自服务器。梯度角度取决于同样来自服务器的变量。此外,客户希望动态填充渐变(用户必须看到渐变是如何从 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 的顶部,带有图片)?

最佳答案

回答你的问题:是的,它可以做得更容易,更容易:

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);
}
}

编辑:

这是另一种解决方案(没有居中,里面没有文字,只是一个概念)

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();
}
}

关于android - 动态绘制自定义圆段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19315953/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com