可以将动画师附加到路径吗?有没有其他方法在画布上绘制动画线条?在我发布之前我搜索了这个,但没有任何关于这一点。在另外两篇帖子here和here中,有解决方案并且不适合我。
我在onDraw方法中发布我的代码,以指定我想要的内容。
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(2);
paint.setColor(Color.BLACK);
Path path = new Path();
path.moveTo(10, 50); // THIS TRANSFORMATIONS TO BE ANIMATED!!!!!!!!
path.lineTo(40, 50);
path.moveTo(40, 50);
path.lineTo(50, 40);
// and so on...
canvas.drawPath(path, paint);
任何想法????
答案 0 :(得分:45)
您可以按时间转换画布,即:
class MyView extends View {
int framesPerSecond = 60;
long animationDuration = 10000; // 10 seconds
Matrix matrix = new Matrix(); // transformation matrix
Path path = new Path(); // your path
Paint paint = new Paint(); // your paint
long startTime;
public MyView(Context context) {
super(context);
// start the animation:
this.startTime = System.currentTimeMillis();
this.postInvalidate();
}
@Override
protected void onDraw(Canvas canvas) {
long elapsedTime = System.currentTimeMillis() - startTime;
matrix.postRotate(30 * elapsedTime/1000); // rotate 30° every second
matrix.postTranslate(100 * elapsedTime/1000, 0); // move 100 pixels to the right
// other transformations...
canvas.concat(matrix); // call this before drawing on the canvas!!
canvas.drawPath(path, paint); // draw on canvas
if(elapsedTime < animationDuration)
this.postInvalidateDelayed( 1000 / framesPerSecond);
}
}
答案 1 :(得分:21)
试试这个:
class PathDrawable extends Drawable implements AnimatorUpdateListener {
private Path mPath;
private Paint mPaint;
private ValueAnimator mAnimator;
public PathDrawable() {
mPath = new Path();
mPaint = new Paint();
mPaint.setColor(0xffffffff);
mPaint.setStrokeWidth(5);
mPaint.setStyle(Style.STROKE);
}
public void startAnimating() {
Rect b = getBounds();
mAnimator = ValueAnimator.ofInt(-b.bottom, b.bottom);
mAnimator.setDuration(1000);
mAnimator.addUpdateListener(this);
mAnimator.start();
}
@Override
public void draw(Canvas canvas) {
canvas.drawPath(mPath, mPaint);
}
@Override
public void setAlpha(int alpha) {
}
@Override
public void setColorFilter(ColorFilter cf) {
}
@Override
public int getOpacity() {
return PixelFormat.TRANSLUCENT;
}
@Override
public void onAnimationUpdate(ValueAnimator animator) {
mPath.reset();
Rect b = getBounds();
mPath.moveTo(b.left, b.bottom);
mPath.quadTo((b.right-b.left)/2, (Integer) animator.getAnimatedValue(), b.right, b.bottom);
invalidateSelf();
}
}
测试它在onCreate方法中添加:
TextView view = new TextView(this);
view.setText("click me");
view.setTextColor(0xffcccccc);
view.setGravity(Gravity.CENTER);
view.setTextSize(48);
final PathDrawable d = new PathDrawable();
view.setBackgroundDrawable(d);
OnClickListener l = new OnClickListener() {
@Override
public void onClick(View v) {
d.startAnimating();
}
};
view.setOnClickListener(l);
setContentView(view);
答案 2 :(得分:1)
您可以为路径创建PathMeasure
并确定其长度:
private PathMeasure pathMeasure; // field
// After you've created your path
pathMeasure = new PathMeasure(path, false);
pathLength = pathMeasure.getLength();
然后,您可以使用getPosTan()在路径中获取特定点,例如,ValueAnimator的更新侦听器:
final float[] position = new float[2]; // field
// Inside your animation's update method, where dt is your 0..1 animated fraction
final float distance = dt * pathLength;
pathMeasure.getPosTan(distance, position, null);
// If using onDraw you'll need to tell the view to redraw using the new position
invalidate();
然后,您可以使用onDraw(或其他)中的位置。
canvas.drawCircle(position[0], position[1], radius, paint);
这种方法的优点是它很简单,不需要粗略的数学,并适用于所有API。
如果使用API 21+,您可以使用ValueAnimator并传入路径以使用其位置,这更简单。 Example SO question