java android 路径动画

标签 java android canvas view

所以我快要结束了。 我一整天都在研究如何做到这一点。 绘制一条从一点到另一点生长(动画)的路径。 我已经用 Matrix 尝试过,但这只是以改变我的整个路径而结束。

这是我的项目的图片: my project

My goal is to draw a animated path from one circle to the other.

代码:

 public void init(@Nullable AttributeSet attr) {
        circle = new Paint();
        circle.setColor(Color.GREEN);
        circle.setStyle(Paint.Style.FILL);
        circle.setAntiAlias(true);

        line = new Paint();
        line.setColor(Color.GREEN);
        line.setStyle(Paint.Style.STROKE);
        line.setStrokeWidth(10);
        line.setAntiAlias(true);


        Collections.addAll(height, 100, 20, 50, 40, 70, 10, 50); // in percent
        System.out.println(height.size() + " this is the size");
    }

    @Override
    protected void onDraw(Canvas canvas) {
        float y = getHeight() / 20 * 14;
        float x = getWidth() / 8;
        float radius = (canvas.getWidth() * canvas.getHeight()) / 40940;
        for (int c = 1; c < 8; c++) {
            System.out.println("at " + c);
            canvas.drawCircle(x * c, y - ((getHeight() / 20) * (height.get(c - 1) / 10)), radius, circle);
            points.add(new PointF(x * c, (y - ((getHeight() / 20) * (height.get(c - 1) / 10)))));
        }

    }

请帮忙,

谢谢

最佳答案

您只需要使用 ValueAnimator 为路径添加动画

创建一个路径对象

Path path = new Path();

并创建动画师

ValueAnimator animator = new ValueAnimator();
float startX = // starting circle x co-ordinate
float startY = // starting circle y co-ordinate
float endX = // end circle x co-ordinate
float endY = // end circle y co-ordinate
PropertyValuesHolder propertyX = PropertyValuesHolder.ofFloat("x",startX,endX);
PropertyValuesHolder propertyY = PropertyValuesHolder.ofFloat("y",startY,endY);
valueAnimator.setValues(propertyX,propertyY);
valueAnimator.setDuration(1000); // animation time
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            float x = (float) animation.getAnimatedValue("x");
            float y = (float) animation.getAnimatedValue("y");
            path.lineTo(x,y);
            invalidate();
        }
    });
valueAnimator.start();

并在onDraw()中绘制路径

canvas.drawPath(path,paint);

关于java android 路径动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60966476/

相关文章:

java - 如何在 Spring Boot JPA 中返回自定义 json 作为输出?

java - 用手指在安卓屏幕上画画

html5 Canvas 未显示完整图像

javascript - 如何计算直线和任意形状的交点?

delphi - 如何在 Delphi 中在 FMX Canvas 上画一条线

java - 从 ZipInputStream 获取特定文件

java - 如何使用 JSch 执行多个操作

java - 创建和使用 jar 库

android - 如何从 Android 与 Google 文档交互?

java - 如何使用Java更改音频频率