//设置动画属性
animation = new ScaleAnimation(1.0f, 0.5f, 1.0f, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f,Animation.RELATIVE_TO_SELF, 0.5f);
animation.setDuration(500);// 设置动画持续时间
animation.setRepeatCount(1);// 设置重复次数
animation.setRepeatMode(Animation.REVERSE);
//onTouchEvent()事件
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
int width = iv.getWidth();
int height = iv.getHeight();
iv.setX(event.getX() - (width/2) );//iv是图片所在的imageView控件
iv.setY(event.getY() - (height/2) );
iv.startAnimation(animation);
break;
}
return true;
}
就是想实现一个类似相机手动点击聚焦的动画效果,但是实际效果是在点击屏幕后动画位置能确定,但是缩放中心是变化的,并不是设置的图片的中心点,
因为你做动画前突然setX值等,中心不好确定了。换用ObjectAnimation或ValueAnimation
吧,自己实现缩放机制
ValueAnimator valueAnimator = ValueAnimator.ofFloat(1, 0.5f);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float v = (Float) animation.getAnimatedValue();
ball.setScaleX(v);
ball.setScaleY(v);
}
});
valueAnimator.setDuration(500);// 设置动画持续时间
valueAnimator.setRepeatCount(1);// 设置重复次数
valueAnimator.setRepeatMode(Animation.REVERSE);
@Override
public boolean onTouch(MotionEvent event) {
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
int width = ball.getWidth();
int height = ball.getHeight();
ball.setX(event.getX() - (width / 2));//ball是图片所在的imageView控件
ball.setY(event.getY() - (height / 2));
ball.setPivotX(ball.getWidth() / 2f);
ball.setPivotY(ball.getHeight() / 2f);
valueAnimator.start();
break;
}
return true;
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。