问题描述
|
好的,所以我已经尝试了几天了,但是我什么也没有。所以我有以下两个图像:
首先是RPM表
第二张图片是全白图形,表示转速表已满:
我要执行以下操作:
要求用户输入RPM,例如,如果他们输入1.2,仪表将按以下方式填充:
我有用户输入在工作,我需要有关动画的帮助。这是我尝试过的:
我试过使用PorterDuff,但它也会在背景中剪裁量规,而不仅仅是白条
我尝试将图像分割成小的位图,然后将它们存储到数组中,以便我可以回忆起各个部分,但这很慢,而且经常崩溃
通过首先将“量具”应用于画布,然后保存画布,我取得了一些进展:canvas.save();然后在白色图像上剪切路径,然后恢复画布。但是,我不知道如何从左下开始到右下(CW)180度循环剪切。这是最好的方法吗?
我知道这样做可能是更简单或更有效的方式,我只是不知道。有什么好主意吗?
*请注意,所有图片均为PNG图片
提前致谢!
解决方法
正如您已经发现的那样,我将使用clip:
绘制背景图片
设置剪辑
绘制前景图像
我会用
Canvas.clipPath()
路径看起来像是从圆心开始的扇形切片,如下所示:
要创建剪辑路径,请使用以下方法:
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,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,paint);
canvas.drawPath(p,paint);
}
}
, 这是从Android ApiDemos绘制弧的方法:http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/Arcs.html
然后,您需要使用xfermode通过使用从位图派生的画布来删除顶部图像的一部分。您可以在此处看到此方法的一个示例:使位图的某些区域在触摸时透明