gpt4 book ai didi

Android Gauge动画问题

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:07:10 25 4
gpt4 key购买 nike

好吧,我已经尝试这样做了几天,但我一无所获。所以我有以下两张图片:

第一个是转速计

RPM Gauge

第二张图片是一个全白的图形,表示转速计已满:

Full Gauge

我想做以下事情:

  1. 要求用户输入 RPM,例如,如果他们输入 1.2,仪表将按如下方式填充:

output

我的用户输入工作正常,我需要有关动画的帮助。这是我尝试过的:

  1. 我尝试过使用 PorterDuff,但它还会在背景中剪切仪表,而不仅仅是白条
  2. 我试过将图像拆分成小位图并将它们存储到数组中,以便我可以记忆部分,但这很慢而且经常崩溃
  3. 通过先将 Gauge 应用到 Canvas 然后保存 Canvas ,我取得了一些进展:canvas.save();然后在白色图像上剪切一条路径,然后恢复 Canvas 。但是我不知道如何从左下角开始以圆形方式剪辑到右下角(CW)180 度。这是最好的方法吗?

我知道可能有更简单或更有效的方法来做到这一点,但我只是不知道。大家有什么好主意吗?

*注意所有图像都是PNG的

提前致谢!

最佳答案

正如您已经发现的,我会使用剪辑:

  • 绘制背景图片
  • 设置剪辑
  • 绘制前景图像

我会用

Canvas.clipPath()

路径看起来像从圆心开始的饼图,像这样:

Clip path

要创建剪辑路径,请使用类似的东西:

public class PieView extends View {

private int width = 200;
private int angleStart = 135;
private int sweep = 270;

private Path p;

private Paint paint = new Paint();

public PieView(Context context, AttributeSet attrs) {
super(context, attrs);
p = new Path();
//move into center of the circle
p.setLastPoint(width/2, width/2);
//add line from the center to arc at specified angle
p.lineTo(width/2+(float)Math.cos(Math.toRadians(angleStart))*(width/2),
width/2+(float)Math.sin(Math.toRadians(angleStart))*(width/2));
//add arc from start angle with specified sweep
p.addArc(new RectF(0, 0, width, width), angleStart, sweep);
//from end of arc return to the center of circle
p.lineTo(width/2, width/2);

paint.setColor(Color.RED);
paint.setStrokeWidth(1);
paint.setStyle(Style.STROKE);
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawRect(0,0,width,width, paint);
canvas.drawPath(p,paint);
}
}

关于Android Gauge动画问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6156674/

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