Android的旋转ImageView的,我不能设置ImageView的最终位置onAnimationEnd() [英] Android rotate imageview, i cant set the final position of imageview in onAnimationEnd()

查看:137
本文介绍了Android的旋转ImageView的,我不能设置ImageView的最终位置onAnimationEnd()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在每点击一个按钮,旋转30度的ImageView的。

在第一集团公司,我可以正确设置动画,但我不能成功,以更新动画后ImageView的位置。当我再次点击该按钮,动画从ImageView的原始位置开始,而不是从第一个动画之后的最终位置。

下面是我的code:

 公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);

    turnImg =(ImageView的)findViewById(R.id.imageViewturnImg);
    BMP位= BitmapFactory.de codeResource(getResources(),R.drawable.imgTurn);
    //获取宽度和放大器;给定图像的高度。
    INT W = bmp.getWidth();
    INT H = bmp.getHeight();

    turnImg .setImageBitmap(BMP);

    按钮buttonok =(按钮)findViewById(R.id.buttonok);
    buttonok.setOnClickListener(新OnClickListener(){
        公共无效的onClick(视图v){
            转();
        }
    });

}

公共无效转()
{
    浮度= 30;

    RotateAnimation动画=新RotateAnimation(0度,Animation.RELATIVE_TO_SELF,0.5F,Animation.RELATIVE_TO_SELF,0.5F);
    anim.setInterpolator(新LinearInterpolator());
    anim.setDuration(300);
    anim.setFillEnabled(真正的);

    anim.setFillAfter(真正的);

    anim.setAnimationListener(新AnimationListener(){
        @覆盖
        公共无效onAnimationEnd(动画为arg0){

        矩阵垫= turnImg.getImageMatrix();
            mat.postRotate(30,turnImg.getWidth()/ 2,turnImg.getHeight()/ 2);
            turnImg.setScaleType(ScaleType.MATRIX);
            turnImg.setImageMatrix(垫);

        }

        @覆盖
        公共无效onAnimationRepeat(动画动画){}
        @覆盖
        公共无效onAnimationStart(动画动画){}
    });


    turnImg.startAnimation(动画);

}
 

我觉得从我actualize在onAnimationEnd()的位置的方式,问题就来了。

PS:抱歉,我的英语不好...

解决方案

现在的问题是,你的动画不影响同一个对象作为基体旋转。也就是说,你在动画ImageView的对象的旋转,但你要设置那就是ImageView的对象中的图像的旋转。因此,例如,在第一次旋转中,ImageView的从0到30度旋转,然后由于调用setFillAfter(真)保持在30度的旋转。然后onAnimationEnd()处理程序运行,它通过旋转30度的内部图像。这有效地跳跃图像以60度的旋转,所看到的用户(30为视图,30为位图)。然后动画下一次运行时,其旋转从0度到30度的图,与内部图像仍然以30度的旋转。这使得它看起来给用户像它的从30到60度的旋转。然后在应用另一个旋转内部图像上时动画完成,结束与图像旋转到60度,视图旋转(再次)到30度,使图像现在视觉上旋转到90度。

等等。

有不同的方式来处理这个问题,但一个简单的方法是避免影响到两个不同的对象 - 刚刚与ImageView的工作。代替从0到30每次旋转,旋转从任何的当前旋转是该值加30度。您可以删除onAnimationEnd()处理程序,因为你将不再需要旋转图像的视图内。

由此产生的code轮到()是非常简单的:

 公共无效转()
{
    RotateAnimation动画=新RotateAnimation(currentRotation,currentRotation + 30,
            Animation.RELATIVE_TO_SELF,0.5F,Animation.RELATIVE_TO_SELF,0.5F);
    currentRotation =(currentRotation + 30)%360;

    anim.setInterpolator(新LinearInterpolator());
    anim.setDuration(1000);
    anim.setFillEnabled(真正的);

    anim.setFillAfter(真正的);
    turnImg.startAnimation(动画);
}
 

这code假定currentRotation,这是用来跟踪最后一个度视图被旋转到一个实例变量。 它有点更复杂,如果你允许用户点击中间的动画,但不是太困难。<​​/ P>

顺便说一下,这是更简单的动画系统中3.0;现在有上查看了旋转属性,并可以运行在该属性的动画,并可以跟踪自己的当前值。但上面的方法应该为旧版本。

I want to rotate an imageview from 30 degrees on each click on a button.

On the first clic, i can set the animation properly but i can't succeed to update the imageview position after the animation. When i clicked again on the button, the animation start from the original position of the imageview and not from the final position after the first animation.

Here is my code :

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);

}

I think the problem came from the way i actualize the position in the onAnimationEnd().

ps : sorry for my bad english...

解决方案

The problem is that your animation is not affecting the same object as your matrix rotation. That is, you are animating the rotation of the ImageView object, but then you are setting the rotation of the image that is within that ImageView object. So, for example, the first time you rotate it, the ImageView rotates from 0 to 30 degrees, and then remains at a rotation of 30 degrees due to the call to setFillAfter(true). Then the onAnimationEnd() handler runs, which rotates the internal image by 30 degrees. This effectively jumps the image to a rotation of 60 degrees, as seen by the user (30 for the View, 30 for the bitmap). Then the next time the animation runs, it rotates the view from 0 to 30 degrees, with the internal image still at its rotation of 30 degrees. This makes it look to the user like it's rotating from 30 to 60 degrees. Then you apply another rotation on the internal image when that animation finishes, ending up with the image rotated to 60 degrees and the view rotated (again) to 30 degrees, so the image is now visually rotated to 90 degrees.

And so on.

There are different ways to handle this problem, but one simple way is to avoid affecting the two different objects - just work with the ImageView. Instead of rotating from 0 to 30 each time, rotate from whatever the current rotation is to that value plus 30 degrees. You can remove the onAnimationEnd() handler, because you will no longer need to rotate the image inside the view.

The resulting code for turn() is much simpler:

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);
}

This code assumes an instance variable of currentRotation, which is used to track the last degrees the view was rotated to. Its a bit more involved if you allow the user to click mid-animation, but not too difficult.

By the way, this is much simpler in the animation system in 3.0; there is now a 'rotation' property on View, and you can run an animation on that property and it can track its own current value. But the approach above should work for older releases.

这篇关于Android的旋转ImageView的,我不能设置ImageView的最终位置onAnimationEnd()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆