android - 如何为 View 的笔划宽度设置动画

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

我有一个背景设置为 drawable.circle 的 ImageView。圆圈的笔画宽度较粗。我想为笔划宽度设置动画,使其在指定的持续时间内从设置的宽度缩小到 1dp。如果这不能用可绘制对象完成,我还有一个 customView,该路径是一个圆圈,paint.style 设置为 Stroke。我想将该动画应用于 drawable.circle 或 customView。

自定义圆 View :

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);
}

谢谢

最佳答案

我会做这样的事情,然后在您的 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();
}

关于android - 如何为 View 的笔划宽度设置动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44507163/

相关文章:

android - 从背景 fragment 中隐藏进度对话框

java - 与用户交互 UI 并从后台线程更新 UI

java - 如何从处理程序中删除所有回调?

java - 为什么我们需要 JSF 标签,如果我们有 HTML 标签

android - 如何根据 Android 版本应用主题

java - java 数组和 Action 监听器

java - JButtons 不遵循预期的处理程序?

android - RecyclerView item展开动画

java - View 中的动画列表

android - 在 RecyclerView 的项目上使用 MotionLayout OnSwipe