我试图添加一个视图,当用户按住按钮时,该视图将从小屏幕扩展到整个屏幕。视图实际上将是一个圆圈,但只是在测试时我将它保持为正方形。
我在MotionEvent.TOUCH_DOWN
上有动画代码:
if (abortIndicatorView == null)
abortIndicatorView = new View(this);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(50, 50);
params.gravity = Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL;
abortIndicatorView.setLayoutParams(params);
abortIndicatorView.setBackgroundColor(0x80FF0000);
((FrameLayout)findViewById(android.R.id.content)).addView(abortIndicatorView);
double height = Resources.getSystem().getDisplayMetrics().heightPixels;
double width = Resources.getSystem().getDisplayMetrics().widthPixels;
final float radius = (float)Math.sqrt(Math.pow(width, 2) + Math.pow(height, 2)) / 2;
Log.i(TAG, "Radius: " + radius);
abortIndicatorView.startAnimation(new Animation() {
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
abortIndicatorView.getLayoutParams().width = (int) (radius * interpolatedTime);
abortIndicatorView.getLayoutParams().height = (int) (radius * interpolatedTime);
}
@Override
public boolean willChangeBounds() {
return true;
}
});
但它没有动画效果,视图会以50,50的大小和中心添加,但没有别的。
答案 0 :(得分:0)
在abortIndicatorView.requestLayout();
中添加applyTransformation
并为动画设置持续时间。
Animation animation = new Animation() {
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
abortIndicatorView.getLayoutParams().width = (int) (radius * interpolatedTime);
abortIndicatorView.getLayoutParams().height = (int) (radius * interpolatedTime);
abortIndicatorView.requestLayout();// Add this line
}
@Override
public boolean willChangeBounds() {
return true;
}
};
animation.setDuration(1000);
abortIndicatorView.startAnimation(animation);