【问题标题】:How do I animate a view along a path in android?如何在android中沿路径为视图设置动画?
【发布时间】:2021-07-05 14:22:33
【问题描述】:

我想为我在“MyView”类中绘制的六边形制作动画。我想制作一条路径,并且能够沿着这条路径制作动画。

了解这个问题的几个前兆。我在 MyView 类中有 xPropertyNameyPropertyName 的 getter 和 setter。设置器不会主动更改 MyView 类中的 onDraw 方法。我不确定这是否是问题。

我已尝试执行 moveTo()、lineTo()、setLastPoint() 等(可能不正确)以获取正确位置的路径。

我还阅读了Android move object along a pathAnimate 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;}
}



【问题讨论】:

标签: java android


【解决方案1】:

您需要为xy 属性设置动画以移动图像。修改现有代码,如下所示:

    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 变笨,以帮助找出原因,例如只需绘制一个固定大小的简单正方形,删除那些属性设置器/获取器setStartXsetStartYgetXgetY。那么“x”和“y”的动画是否有效?
猜你喜欢
  • 2010-11-11
  • 1970-01-01
  • 2013-11-29
  • 1970-01-01
  • 2012-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-16
相关资源
最近更新 更多