我有一个背景设置为 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/