gpt4 book ai didi

android - 如何绘制局部位图圆弧?就像一个圆形进度轮,但带有越来越多显示的位图。

转载 作者:太空宇宙 更新时间:2023-11-03 12:08:27 24 4
gpt4 key购买 nike

我要搜索的是一个以顺时针圆形方式显示图像的 View 。当进度为 25% 时,应显示前 90 度,当进度为 100% 时,应绘制完整的 360 度。

它与使用 canvas.drawArc() 非常接近,但此方法仅适用于 Paint 对象,不适用于位图。

其他答案也只适用于全色: How To Make Circle Custom Progress Bar in Android

我发现这是一个非常难的问题,我希望有人能解决。

最佳答案

处理这个:

public class MyView extends View {

private Bitmap mBitmap;
private Paint mPaint;
private RectF mOval;
private float mAngle = 135;
private Paint mTextPaint;

public MyView(Context context) {
super(context);
// use your bitmap insted of R.drawable.ic_launcher
mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mOval = new RectF();
mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mTextPaint.setTextSize(48);
mTextPaint.setTextAlign(Align.CENTER);
mTextPaint.setColor(0xffffaa00);
mTextPaint.setTypeface(Typeface.DEFAULT_BOLD);
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
Matrix m = new Matrix();
RectF src = new RectF(0, 0, mBitmap.getWidth(), mBitmap.getHeight());
RectF dst = new RectF(0, 0, w, h);
m.setRectToRect(src, dst, ScaleToFit.CENTER);
Shader shader = new BitmapShader(mBitmap, TileMode.CLAMP, TileMode.CLAMP);
shader.setLocalMatrix(m);
mPaint.setShader(shader);
m.mapRect(mOval, src);
}

@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(0xff0000aa);
canvas.drawArc(mOval, -90, mAngle, true, mPaint);
canvas.drawText("click me", getWidth() / 2, getHeight() / 2, mTextPaint);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
float w2 = getWidth() / 2f;
float h2 = getHeight() / 2f;
mAngle = (float) Math.toDegrees(Math.atan2(event.getY() - h2, event.getX() - w2));
mAngle += 90 + 360;
mAngle %= 360;
invalidate();
return true;
}
}

关于android - 如何绘制局部位图圆弧?就像一个圆形进度轮,但带有越来越多显示的位图。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19710613/

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