是否还有其他人使用ObjectAnimator来更改View元素的位置?它也会在屏幕上留下残余物吗?
查看上方中间按钮。蓝色框从那里开始动画,留下残余,屏幕撕裂或碎片。你们是怎么处理这个的?
代码是否重要:
public class AnimateComponentsActivity extends Activity {
private View box;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
box = findViewById(R.id.box);
box.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i("TEST", "Box got a click");
}
});
Button btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(clickListener);
btn = (Button)findViewById(R.id.button2);
btn.setOnClickListener(clickListener);
btn = (Button)findViewById(R.id.button3);
btn.setOnClickListener(clickListener);
}
private View.OnClickListener clickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i("TEST", "Animation should be playing");
// new way, but can't scale size
ObjectAnimator.ofFloat(box, "y", v.getY()).start();
ObjectAnimator.ofFloat(box, "x", v.getX()).start();
ValueAnimator.ofObject(new WidthEvaluator(box), box.getWidth(), v.getWidth()).start();
}
};
private class WidthEvaluator extends FloatEvaluator {
private View v;
public WidthEvaluator(View v) {
this.v = v;
}
@Override
public Object evaluate(float fraction, Object startValue, Object endValue)
{
Float num = (Float)super.evaluate(fraction, startValue, endValue);
ViewGroup.LayoutParams params = v.getLayoutParams();
params.width = Math.round(num);
v.setLayoutParams(params);
return num;
}
}
}