Android旋转imageview,我无法在onAnimationEnd()中设置imageview的最终位置

Android旋转imageview,我无法在onAnimationEnd()中设置imageview的最终位置,第1张

概述我想在每次点击按钮时将图像视图从30度旋转.在第一个clic,我可以正确设置动画,但我不能成功更新动画后的imageview位置.当我再次单击按钮时,动画从imageview的原始位置开始,而不是从第一个动画后的最终位置开始.这是我的代码:publicvoidonCreate(BundlesavedInstanceState){

我想在每次点击按钮时将图像视图从30度旋转.

在第一个clic,我可以正确设置动画,但我不能成功更新动画后的imagevIEw位置.当我再次单击按钮时,动画从imagevIEw的原始位置开始,而不是从第一个动画后的最终位置开始.

这是我的代码:

public voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.main);    turnimg = (ImageVIEw) findVIEwByID(R.ID.imageVIEwturnimg );    Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.imgTurn);    // Getting wIDth & height of the given image.    int w = bmp.getWIDth();    int h = bmp.getHeight();    turnimg .setimageBitmap(bmp);    button buttonok = (button) findVIEwByID(R.ID.buttonok);    buttonok.setonClickListener(new OnClickListener() {        public voID onClick(VIEw v) {            turn();        }    });}public voID turn(){    float degrees = 30;     RotateAnimation anim = new RotateAnimation(0, degrees,Animation.relative_TO_SELF, 0.5f, Animation.relative_TO_SELF,0.5f);    anim.setInterpolator(new linearInterpolator());    anim.setDuration(300);    anim.setFillEnabled(true);    anim.setFillAfter(true);    anim.setAnimationListener(new AnimationListener() {        @OverrIDe        public voID onAnimationEnd(Animation arg0) {        Matrix mat = turnimg.getimageMatrix();            mat.postRotate(30,turnimg.getWIDth()/2, turnimg.getHeight()/2);            turnimg.setScaleType(ScaleType.MATRIX);            turnimg.setimageMatrix(mat);        }        @OverrIDe        public voID onAnimationRepeat(Animation animation) {}        @OverrIDe        public voID onAnimationStart(Animation animation) {}    });    turnimg.startAnimation(anim);}

我认为问题来自于我在onAnimationEnd()中实现位置的方式.

ps:抱歉我的英语不好……

解决方法:

问题是您的动画不会影响与矩阵旋转相同的对象.也就是说,您正在设置ImageVIEw对象的旋转动画,但是您正在设置该ImageVIEw对象内的图像的旋转.因此,例如,第一次旋转时,ImageVIEw会从0度旋转到30度,然后由于调用setFillAfter(true)而保持30度旋转.然后运行onAnimationEnd()处理程序,将内部图像旋转30度.这有效地将图像跳转到60度的旋转,如用户所见(视图为30,位图为30).然后,下次动画运行时,它会将视图从0度旋转到30度,内部图像仍然处于30度旋转状态.这使得用户看起来像是从30度旋转到60度.然后在该动画结束时对内部图像应用另一个旋转,最后将图像旋转到60度并将视图旋转(再次)到30度,因此图像现在可视地旋转到90度.

等等.

有不同的方法来处理这个问题,但一种简单的方法是避免影响两个不同的对象 – 只需使用ImageVIEw.不是每次从0旋转到30,而是从当前旋转到该值加上30度旋转.您可以删除onAnimationEnd()处理程序,因为您将不再需要在视图内旋转图像.

turn()的结果代码更简单:

public voID turn(){    RotateAnimation anim = new RotateAnimation(currentRotation, currentRotation + 30,            Animation.relative_TO_SELF, 0.5f, Animation.relative_TO_SELF,0.5f);    currentRotation = (currentRotation + 30) % 360;    anim.setInterpolator(new linearInterpolator());    anim.setDuration(1000);    anim.setFillEnabled(true);    anim.setFillAfter(true);    turnimg.startAnimation(anim);}

此代码假定currentRotation的实例变量,用于跟踪视图旋转到的最后度数.
如果您允许用户单击动画中期,但它不会太复杂.

顺便说一句,这在3.0中的动画系统中要简单得多; VIEw上现在有一个’rotation’属性,你可以在该属性上运行动画,它可以跟踪它自己的当前值.但上述方法适用于旧版本.

总结

以上是内存溢出为你收集整理的Android旋转imageview,我无法在onAnimationEnd()中设置imageview的最终位置全部内容,希望文章能够帮你解决Android旋转imageview,我无法在onAnimationEnd()中设置imageview的最终位置所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1097589.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存