gpt4 book ai didi

Android绘制动画线

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

我目前正在处理图形和路径,我可以成功显示我想要的任何内容。

但我不想直接在我的 SurfaceView 上画一条线,而是想在动画中逐步绘制它。

到目前为止,我所做的是创建一个路径,然后使用 PathMeasure 沿路径逐步检索坐标。到目前为止,这基本上是我所做的

PathMeasure pm = new PathMeasure(myPath, false);

float position = 0;
float end = pm.getLength();
float[] coord = {0,0,0,0,0,0,0,0,0};

while (position < end){
Matrix m = new Matrix();
// put the current path position coordinates into the matrix
pm.getMatrix(position, m, PathMeasure.POSITION_MATRIX_FLAG | PathMeasure.TANGENT_MATRIX_FLAG);
// put the matrix data into the coord array (coord[2] = x and coord[5] = y)
m.getValues(coord);
????
position += 1;

}

问号是我卡住的地方。我想逐步绘制路径并在屏幕上看到它的动画。我在互联网上找不到太多关于它的信息,所以如果你已经遇到过同样的情况,任何线索都会非常感激。我想要创建的最终效果就像铅笔画一样自动逐渐变成文本。

最佳答案

每次您想绘制更多路径时,您可以使用 ObjectAnimator 类回调到您的类的方法之一,而不是创建一个 for 循环。

import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.graphics.PathMeasure;
import android.util.AttributeSet;
import android.view.View;
import android.util.Log;

public class PathView extends View
{
Path path;
Paint paint;
float length;

public PathView(Context context)
{
super(context);
}

public PathView(Context context, AttributeSet attrs)
{
super(context, attrs);
}

public PathView(Context context, AttributeSet attrs, int defStyleAttr)
{
super(context, attrs, defStyleAttr);
}

public void init()
{
paint = new Paint();
paint.setColor(Color.BLUE);
paint.setStrokeWidth(10);
paint.setStyle(Paint.Style.STROKE);

path = new Path();
path.moveTo(50, 50);
path.lineTo(50, 500);
path.lineTo(200, 500);
path.lineTo(200, 300);
path.lineTo(350, 300);

// Measure the path
PathMeasure measure = new PathMeasure(path, false);
length = measure.getLength();

float[] intervals = new float[]{length, length};

ObjectAnimator animator = ObjectAnimator.ofFloat(PathView.this, "phase", 1.0f, 0.0f);
animator.setDuration(3000);
animator.start();
}

//is called by animtor object
public void setPhase(float phase)
{
Log.d("pathview","setPhase called with:" + String.valueOf(phase));
paint.setPathEffect(createPathEffect(length, phase, 0.0f));
invalidate();//will calll onDraw
}

private static PathEffect createPathEffect(float pathLength, float phase, float offset)
{
return new DashPathEffect(new float[] { pathLength, pathLength },
Math.max(phase * pathLength, offset));
}

@Override
public void onDraw(Canvas c)
{
super.onDraw(c);
c.drawPath(path, paint);
}
}

然后,只需调用 init() 来开始动画,就像这样(或者如果您希望它在 View 膨胀后立即开始,请将 init() 调用放在构造函数中):

PathView path_view = (PathView) root_view.findViewById(R.id.path);
path_view.init();

另见此问题 here , 和 this示例,我的代码基于此。

关于Android绘制动画线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5367950/

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