【发布时间】:2021-07-05 14:22:33
【问题描述】:
我想为我在“MyView”类中绘制的六边形制作动画。我想制作一条路径,并且能够沿着这条路径制作动画。
了解这个问题的几个前兆。我在 MyView 类中有 xPropertyName 和 yPropertyName 的 getter 和 setter。设置器不会主动更改 MyView 类中的 onDraw 方法。我不确定这是否是问题。
我已尝试执行 moveTo()、lineTo()、setLastPoint() 等(可能不正确)以获取正确位置的路径。
我还阅读了Android move object along a path 和Animate ImageView along Path Android 问题,并且我不想覆盖任何方法或做任何除了调用预设方法和使用我的视图类之外的复杂操作。我只想让十六进制对象沿着路径移动。
我想知道原因是否可能是 MyView 的 onDraw 方法中的“startX”和“startY”没有更新。但是,没有这方面的文档。
float xCenter = PHONE_DIMS.x / 2;
float yOffset = PHONE_DIMS.y / 3;
MyView hex = new MyView(this, yellow, 10,10, 90);
doodleView.addView(hex);
Path hexPath = new Path();
hexPath.moveTo(10,10);
hexPath.lineTo(10,PHONE_DIMS.y *4/5);
hexPath.moveTo(10, PHONE_DIMS.y *4/5);
ObjectAnimator hexAn = ObjectAnimator.ofFloat(hex,"startX","startY",hexPath);
OvershootInterpolator interpolator = new OvershootInterpolator();
hexAn.setInterpolator(interpolator);
hexAn.setDuration(5000);
hexAn.start();
//Below is MyView just for context
public class MyView extends DrawView {
private float size,x,y;
/**
* Constructor for a basic Draw View
*
* @param context The Context the view is running in, through which it can access the current theme, resources, etc.
* @param brush A paint object for styling when drawing
*/
public MyView(Context context, Paint brush, float startX, float startY, float size) {
super(context, brush);
setStartX(startX);
setStartY(startY);
this.setSize(DimHelp.DP2PX(size,context));
initFromParentCoordsPX(
DimHelp.DP2PX(getX(),context),
DimHelp.DP2PX(getY(),context),
DimHelp.DP2PX(size + getX(), context),
DimHelp.DP2PX(size + getY(), context)
);
}
/**
* Draw something on the Canvas
* @param canvas the canvas that is drawn upon
*/
protected void onDraw(Canvas canvas) {
Paint myBrush = this.getBrush();
float comp = myBrush.getStrokeWidth()/2;
float canWidth = getSize();
float canHeight = getSize();
canvas.drawLine(canWidth/3, 0+comp, 2*canWidth/3, 0+comp, myBrush);
canvas.drawLine(2*canWidth/3, 0+comp, canWidth-comp, canHeight/3, myBrush);
canvas.drawLine(canWidth-comp, canHeight/3, canWidth-comp, 2*canHeight/3, myBrush);
canvas.drawLine(canWidth-comp, 2*canHeight/3, 2*canWidth/3, canHeight-comp, myBrush);
canvas.drawLine(2*canWidth/3, canHeight-comp, canWidth/3, canHeight-comp, myBrush);
canvas.drawLine(canWidth/3, canHeight-comp, 0+comp, 2*canHeight/3, myBrush);
canvas.drawLine(0+comp, 2*canHeight/3, 0+comp, canHeight/3, myBrush);
canvas.drawLine(0+comp, canHeight/3, canWidth/3, 0+comp, myBrush);
}
//Getters and setters for the size, x and y variables
public void setSize(float s) {size = s;}
public float getSize() {return size;}
public void setStartX(float ex) {x = ex;}
public float getX() {return x;}
public void setStartY(float ey) {y = ey;}
public float getY() {return y;}
}
【问题讨论】:
-
我不想覆盖任何东西。在这方面,它不是重复的,因为这些答案会覆盖。