Android 如何设置视图笔划宽度的动画
我有一个Android 如何设置视图笔划宽度的动画,android,user-interface,android-animation,user-experience,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 pat
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);
}
}
谢谢我会这样做,然后用你的
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();
}
已设置动画的矢量绘图允许您设置笔划宽度的动画。如果需要对动画进行更精确的控制,则可以创建自定义可绘制视图并为其设置动画,它比自定义视图更解耦。