- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我需要在 value
属性更改时为进度条值设置动画,我的自定义 View 如下所示,
public class ProgressBar extends View {
public ProgressBar(Context context) {
this(context, null);
}
public ProgressBar(Context context, AttributeSet attrs) {
super(context, attrs);
}
ObjectAnimator animator;
double value = 50;
public double getValue() {
return value;
}
public void setTargetValue(double targetValue) {
animator = ObjectAnimator.ofFloat(this, "value", (float) this.value,(float) targetValue);
animator.setDuration(1500);
animator.start();
this.value = targetValue;
}
public void setValue(double tempValue) {
setTargetValue(tempValue);
this.invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setStrokeWidth(3);
RectF borderRectF = new RectF(50,100,400,200);
RectF backgroundRectF = new RectF(53,103,397,197);
RectF filledRectF = new RectF(53,103,53,197);
paint.setColor(Color.LTGRAY);
canvas.drawRect(borderRectF, paint);
paint.setStrokeWidth(0);
paint.setColor(Color.WHITE);
canvas.drawRect(backgroundRectF, paint);
paint.setColor(Color.BLUE);
filledRectF = getfilledRect();
canvas.drawRect(filledRectF, paint);
}
private RectF getfilledRect(){
float filledValue = (float)(53+(3.44 * this.value));
filledValue = Math.min(filledValue,397);
return new RectF(53,103,filledValue,197);
}
}
但是动画不工作,我是不是错过了什么,或者我可以做不同的事情吗?
最佳答案
您需要两个函数而不是这个函数。一个函数应该用你的值的新目标调用,另一个函数应该用来实现每个步骤。在第一个中使用 ObjectAnimator,它会针对每个增量步骤多次调用您的第二个函数。像这样:
public void setProgress(float progress) {
animator = ObjectAnimator.ofFloat(this, "value", this.value, progress);
animator.setDuration(1500);
animator.start();
}
public void setValue(float value) {
this.value = value;
invalidate();
}
private RectF getfilledRect() {
float filledValue = 53f + (3.44f * this.value);
filledValue = Math.min(filledValue, 397f);
return new RectF(53f, 103f, filledValue, 197f);
}
一些注意事项:
编辑
啊,我明白了,您正在制作自己的进度条。在那种情况下,您调用 invalidate 是正确的,因为您正在覆盖 onDraw。我修改了上面的答案,将“setTargetValue”更改为“setProgress”。这是一个只能从此类外部调用的函数——无论谁知道进展情况。您不希望 setProgress 调用 setValue,反之亦然。
新笔记:
关于Android Progressbar(自定义 View )值动画不起作用-无效后 View 中的对象动画器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26902066/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!