gpt4 book ai didi

Android沿路径移动对象

转载 作者:IT老高 更新时间:2023-10-28 23:29:10 26 4
gpt4 key购买 nike

我已经创建了一个路径和圆圈,并在屏幕上显示它们如下:

public void onDraw(Canvas canvas){

Path sPath = new Path();
sPath.moveTo(100, 100);
sPath.lineTo(300, 100);
sPath.lineTo(300, 300);
sPath.lineTo(100,300);
sPath.lineTo(100,100);
sPath.close();

Paint ballPaint = new Paint();
ballPaint.setColor(Color.GREEN);
Paint pathPaint = new Paint();
pathPaint.setColor(Color.BLUE);

canvas.drawPath(sPath, ballPaint);
canvas.drawCircle(100,100,20,pathPaint);
}

我想让圆圈沿着路径移动,我该怎么做?

最佳答案

以下是我使用的动画师:

目的:沿路径“路径”移动 View “ View ”

v21+:

ValueAnimator pathAnimator = ObjectAnimator.ofFloat(view, "x", "y", path)

v11+:

ValueAnimator pathAnimator = ValueAnimator.ofFloat(0.0f, 1.0f);

pathAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
float[] point = new float[2];

@Override
public void onAnimationUpdate(ValueAnimator animation) {
float val = animation.getAnimatedFraction();
PathMeasure pathMeasure = new PathMeasure(path, true);
pathMeasure.getPosTan(pathMeasure.getLength() * val, point, null);
view.setX(point[0]);
view.setY(point[1]);
}
});

关于Android沿路径移动对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6154370/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com