Java 如何在android中沿路径设置视图动画?
我想为我在“MyView”类中绘制的六边形设置动画。我想创建一条路径,并能够沿着该路径设置动画 了解这个问题的一些先兆。我在MyView类中有Java 如何在android中沿路径设置视图动画?,java,android,Java,Android,我想为我在“MyView”类中绘制的六边形设置动画。我想创建一条路径,并能够沿着该路径设置动画 了解这个问题的一些先兆。我在MyView类中有xPropertyName和yPropertyName的getter和setter。setter不会主动更改MyView类中的onDraw方法。我不确定这是否是问题所在 我已尝试执行moveTo()、lineTo()、setLastPoint()等操作(可能不正确),以获得正确位置的路径 我还阅读了和问题,除了调用预设方法和使用我的视图类之外,我不想重写任
xPropertyName
和yPropertyName
的getter和setter。setter不会主动更改MyView类中的onDraw方法。我不确定这是否是问题所在
我已尝试执行moveTo()、lineTo()、setLastPoint()等操作(可能不正确),以获得正确位置的路径
我还阅读了和问题,除了调用预设方法和使用我的视图类之外,我不想重写任何方法或做任何复杂的事情。我只想让十六进制对象沿着路径移动 我想知道原因是否是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;}
}
要移动图像,需要设置
x
和y
属性的动画。修改现有代码,如下所示:
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);
// you've already moved to this position with the above lineTo() call
// hexPath.moveTo(10, PHONE_DIMS.y *4/5);
// Animate the x and y properties, not startX and startY
ObjectAnimator hexAn = ObjectAnimator.ofFloat(hex,"x","y",hexPath);
OvershootInterpolator interpolator = new OvershootInterpolator();
hexAn.setInterpolator(interpolator);
hexAn.setDuration(5000);
hexAn.start();
可能重复?我不想覆盖任何内容。在这方面,它不是重复的,因为这些答案会覆盖。我已经尝试过了,但不幸的是,它也不起作用。我建议将
MyView
简化,以帮助确定原因,例如,只需绘制一个固定大小的简单正方形,删除那些属性setter/gettersetStartX
,setStartY
,getX
,getY
。那么“x”和“y”的动画能工作吗?