android - 重复一个switch语句

时间:2016-12-23 10:39:14

标签: java android

在我的应用中,我想随机选择一张图片并将其放在屏幕中央。之后,可以执行动画,之后图像将被移动并重置到其原点位置。然后我想选择开关盒的新图像,但图像始终保持不变。我该如何解决?

public class MainActivity extends Activity implements OnGestureListener
{       

        private ImageView imageView;
        float x1,x2;
        float y1, y2;  
        public int sco = 0;
        TextView score;
        final Random rand = new Random();

            @Override
            protected void onCreate(Bundle savedInstanceState)
            {        
                        super.onCreate(savedInstanceState);                       
                        setContentView(R.layout.activity_main);                        
                        imageView = (ImageView) findViewById(R.id.imageview1);
                        imageView.setImageResource(getMyRandomResId());
                        score = (TextView)findViewById(R.id.textView2);                  
            }


            int imag = rand.nextInt(4);
                int getMyRandomResId() {


                    switch (imag) {
                    case 0:

                        return R.drawable.a;
                    case 1:

                        return R.drawable.b;
                    case 2:

                        return R.drawable.c;
                    default:

                        return R.drawable.d;
                }
            }


            private boolean animationRunning = false;

            public boolean onTouchEvent(MotionEvent touchevent)
            {

                final ViewPropertyAnimator animator = imageView.animate();

                switch (touchevent.getAction())
                {

                    case MotionEvent.ACTION_DOWN:
                    {

                        x1 = touchevent.getX();
                        y1 = touchevent.getY();
                        break;
                    }
                    case MotionEvent.ACTION_UP:
                    {

                        x2 = touchevent.getX();
                        y2 = touchevent.getY();

                        if (!animationRunning) {
                    //left to right sweep event on screen
                        if (x1 < x2 && (x2-x1)>=(y1-y2) && (x2-x1)>=(y2-y1)) {

                            animationRunning = true;                        
                            animator.translationX(imageView.getWidth() * 2)                             
                                    .setDuration(180)                                    
                                    .setListener(new AnimatorListenerAdapter() {
                                        @Override
                                        public void onAnimationEnd(Animator animation) {
                                            if(getMyRandomResId() == R.drawable.ballgrue) {
                                                sco++;
                                            }
                                            imageView.setTranslationX(0);
                                            imageView.setImageResource(getMyRandomResId()); 
                                            animationRunning = false;

                                        }
                                    })
                                    .start();           
                        }
            }
           }
           }
        }
}

1 个答案:

答案 0 :(得分:1)

            int getMyRandomResId() {

     int imag = rand.nextInt(4);
                switch (imag) {
                case 0:

                    return R.drawable.a;
                case 1:

                    return R.drawable.b;
                case 2:

                    return R.drawable.c;
                default:

                    return R.drawable.d;
            }
        }

你只有这个图像bcoz的原因

希望有所帮助