如何为视图的笔触宽度设置动画

时间:2017-06-12 19:18:37

标签: android user-interface android-animation user-experience

我有一个ImageView,背景设置为drawable.circle。圆的行程宽度较粗。我想将该笔划宽度设置为在指定的持续时间内从设置为1dp缩小。如果使用drawable无法做到这一点,我还有一个customView,该路径是一个将paint.style设置为Stroke的圆。我想将该动画应用于drawable.circle或customView。

CustomCirleView:

public class CustomCircle extends View {

private Path path;
private Paint paint;
float customStrokeWidth = 80;


public float getCustomStrokeWidth() {
    return customStrokeWidth;
}

public void setCustomStrokeWidth(float customStrokeWidth) {
    this.customStrokeWidth = customStrokeWidth;
}


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

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    path = new Path();
    paint = new Paint();
    paint.setStyle(Paint.Style.STROKE);
    paint.setAntiAlias(true);
    paint.setColor(Color.parseColor("#FC6C2B"));
    paint.setStrokeWidth(customStrokeWidth);
    path.addCircle(getMeasuredWidth()/2, getMeasuredHeight()/2, (getMeasuredWidth()/2)-customStrokeWidth, Path.Direction.CW);

    canvas.drawPath(path, paint);
}

}

由于

1 个答案:

答案 0 :(得分:0)

我会做这样的事情,然后在你的onDraw方法中绘制圆圈。请永远不要在onDraw方法中进行所有绘制初始化,因为它总是需要一些时间,而onDraw方法应该尽可能少地执行

private void init() {   
  animator = ObjectAnimator.ofFloat(this, "animationProgress", startVal, endVal);
  animator.setStartDelay(ANIMATION_START_DELAY);
  animator.setDuration(ANIMATION_DURATION);
  animator.setInterpolator(new FastOutSlowInInterpolator());

  path = new Path();
  paint = new Paint();
  paint.setStyle(Paint.Style.STROKE);
  paint.setAntiAlias(true);
  paint.setColor(Color.parseColor("#FC6C2B"));
  paint.setStrokeWidth(customStrokeWidth);
  path.addCircle(getMeasuredWidth()/2, getMeasuredHeight()/2, (getMeasuredWidth()/2)-customStrokeWidth, Path.Direction.CW);
}

/**
 * Is called by the {@link #animator} after an animation update
 */
protected void setAnimationProgress(float strokeWidth) {
    this.strokeWidth = strokeWidth;
    postInvalidateOnAnimation();
}
相关问题