-1

I have a layout with 2*2/3*3/4*4... ImageView. and some of them randomly colored like this:

enter image description here

For ranodomly colored one of Imageview, I have Use this code:

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

And I'm handling some click event on colored ImageView like this:

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

                    }
                });
            }

There is no problem till above.

But When I'm adding a random rotate animation code like this:

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

After rotation image look like this(i.e. colored Imageview chage it's position):

enter image description here

After adding this rotate animation code my clickEvent not get correct ImageView id. i.e. when imageview change it's position,their id should be go with that positon. but imageview id ramains at same postion.

So How to get required ImageviewId after rotation

Update:

After using azizbekian's answer(now using animator instead of animation) I found correct image-view id , but only first time. i.e. first time when Activity starts it works good but when I'm going to a new view(say 3*3) and return back again to this view(2*2),it returns wrong image-view id till Activity restarts again.see my updated code for ref.

Explanation of working and issue:

I have already said that there are many matix of Imageview such as 2*2/3*3/4*4..... So when we start Application first load 2*2 matrix of imageview and when we click the odd colored imageview it goes to 3*3 matrix but when we click other than odd colored imageviewthen it goes to next level say 3*3.So when app start first time when I click on odd colored imageview it goes to 3*3 matrix but when return again on 2*2 after clicking other than odd colored imageview.and then again when we click on colored imageview it not get correct image id. and if any other query plz ask?

How to resolve this issue ?

4

2 回答 2

1

ImageView改变它的位置时,他们的 id 应该与那个位置一致。但ImageView id 保持在同一位置。

那是因为您正在使用Animation,这实际上并没有改变您View的 ' 位置。什么是动画的错觉,所以它只改变视图的矩阵,但不执行视图的布局更改。

使用AnimatorAPI 而不是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);

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

于 2017-04-21T09:44:59.133 回答
0

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

于 2017-05-02T15:50:50.733 回答