旋转布局

时间:2017-04-21 09:39:04

标签: java android android-layout android-animation animator

我的布局有2 * 2/3 * 3/4 * 4 ... ImageView。其中一些是随机着色的:

enter image description here

对于Imageview中的色彩鲜艳的,我使用此代码:

Random random = new Random();

        id = array_image1.get(random.nextInt(array_image1.size()));


        ObjectAnimator animator = ObjectAnimator.ofInt(findViewById(id), "backgroundResource", R.drawable.original_img, R.drawable.org_state).setDuration(1500);

        animator.setStartDelay(1500);
        animator.setEvaluator(new ArgbEvaluator());
        animator.start();

我在彩色ImageView上处理一些点击事件,如下所示:

     for (int i = 0; i < array_image11.size(); ++i) {
                final int btn = array_image11.get(i);

                findViewById(btn).setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {

                        if ((findViewById(id)).equals(findViewById(btn)))
                        //Inform the user the button has been clicked
                        {

   //forward
   findViewById(id).setBackgroundResource(R.drawable.original_img);

     }else {

                           //backward
                            Toast.makeText(StaticSpatial.this, "wrong", Toast.LENGTH_SHORT).show();

                    }
                });
            }

直到上面没有问题。

但是当我添加一个像这样的随机旋转动画代码时:

            int n;
            int[] a=new int[]{1,2};
            final Random rnd = new Random();
            n=a[rnd.nextInt(a.length)];
            if(n==1)
            {
                 ObjectAnimator rotateAnimator = ObjectAnimator.ofFloat(l4, View.ROTATION, 0, 90);
                rotateAnimator.setDuration(2000); // miliseconds
                rotateAnimator.start();n);


            }
            else
            {
              ObjectAnimator rotateAnimator = ObjectAnimator.ofFloat(l4, View.ROTATION, 0, 90);
                rotateAnimator.setDuration(2000); // miliseconds
                rotateAnimator.start();
            }

旋转后的图像看起来像这样(即着色的Imageview chage it&#39; s):

enter image description here

添加此旋转动画代码后,我的clickEvent无法获得正确的ImageView ID。 即当imageview改变它的位置时,它们的id应该与该位置一致。但是imageview id仍处于同一位置。

那么如何在轮换后获得所需的ImageviewId

更新

使用azizbekian的答案(现在使用动画师代替动画)后,我找到了正确的图像视图ID,但只是第一次。 也就是第一次当Activity开始时效果很好但是当我进入一个新视图(例如3 * 3)并再次返回到该视图(2 * 2)时,它会返回错误的图像视图ID,直到Activity重新启动再次看到我更新的参考代码

工作和问题说明:

我已经说过像Imageview有很多matix,比如2 * 2/3 * 3/4 * 4 .....所以当我们启动应用程序首次加载imageview的2 * 2矩阵时,点击奇数彩色图像视图它转到3 * 3矩阵但是当我们点击奇数彩色图像以外它然后它进入下一级别说3 * 3.所以当应用程序启动第一次点击奇数彩色图像时它转到3 * 3矩阵但是在点击奇数彩色图像视图以外的其他时间再次返回2 * 2时,再次单击彩色图像时,它将无法获得正确的图像ID。如果有任何其他查询请问?

如何解决此问题?

2 个答案:

答案 0 :(得分:1)

  

ImageView 更改其位置时,他们的ID应该与该位置一致。但 ImageView ID仍保持在同一位置。

那是因为你使用Animation,实际上并没有改变你View的位置。它是什么造成了动画的幻觉,所以它只会改变视图的矩阵,但不会对视图进行布局更改。

使用Animator API代替Animation

<强>更新

鉴于此动画xml

<set xmlns:android="schemas.android.com/apk/res/android">
    <rotate
        android:fromDegrees="0" 
        android:toDegrees="90" 
        android:pivotX="50%" 
        android:pivotY="50%" 
        android:duration="2000" /> 
</set> 

这可以用以下代替:

ObjectAnimator rotateAnimator = ObjectAnimator.ofFloat(yourView, View.ROTATION, 0, 90);
rotateAnimator.setDuration(2000);
rotateAnimator.start();

<强>更新

只需在每次加载视图(或布局)时设置旋转零点,如下所示:

mylayoutname.setRotation(0);

因为旋转后它不会自动进入零度,直到活动停止。

答案 1 :(得分:0)

也许不是使用旋转,您可以将ListViews的ID存储在List或Array中,并将当前彩色ImageView的Id存储在一个单独的变量中,当您必须旋转时,您只需从中选择一个随机ImageView列表/数组(不包括当前着色的颜色),从上一个颜色中删除颜色并为新颜色着色 顺便说一下,当你有3x3,5x5,7x7等布局时,中间的ImageView从未着色过?因为在这种情况下,旋转不会改变任何东西(至少在视觉上)。我的意思是,旋转适用于在2x2正方形的布局中选择随机正方形,但在任何其他情况下都不行。您是否需要随机选择正方形或者是否需要始终通过旋转选择它们? (我希望我的意思很明确......)