Android: 从2个pool中随机挑选图片

Android: Randomly pick images from 2 pools

我有一个应用程序,我打算测量屏幕上显示的一些设计。过程如下:

  1. 显示从池 1 中随机选取的图像 1 秒;
  2. 显示黑屏(1 秒)
  3. 显示从池 2 中随机选取的图像 1 秒;
  4. 显示红屏(等到按下按钮

重复1-4多次。 问题 1:实现这种行为的最佳实践是什么?我尝试使用 "animation drawable",但据我所知,你不能从 xml 中随机选择图像?

问题二:效率呢?按问题1所述尝试后,显示:"I/Choreographer: Skipped 59 frames! The application may be doing too much work on its main thread." -> do I need multithreading here?

最好的, 老虎代码

有趣的是:

我的 class (Gameactivity) 并首先尝试使用 AnimationDrawable:

public class GameActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.game);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        /* ImageView */

        ImageView myAnimation = (ImageView) findViewById(R.id.spin_animation_left);

        final AnimationDrawable myAnimationDrawable
                = (AnimationDrawable) myAnimation.getDrawable();

        myAnimation.post(
                new Runnable() {

                    @Override
                    public void run() {
                        myAnimationDrawable.start();
                    }
                });

    }

    /*Show Gameactivity always on fullscreen */

    @Override
    public void onWindowFocusChanged(boolean hasFocas) {
        super.onWindowFocusChanged(hasFocas);
        View decorView = getWindow().getDecorView();
        if (hasFocas) {
            decorView.setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
                            | View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
                            | View.SYSTEM_UI_FLAG_IMMERSIVE);
        }


    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent b) {
        if (b.getKeyCode() == KeyEvent.KEYCODE_DPAD_LEFT) {
            startActivity(new Intent(this,StartActivity.class));
        }
        return super.dispatchKeyEvent(b);
    };

}

这是我的 XML(其他持续时间,仅显示:"image pool 1" - "image pool 2",依此类推...):

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false"
    >
    <item
        android:drawable="@drawable/p1"
        android:duration="150"/>
    <item
        android:drawable="@drawable/t1"
        android:duration="200"/>
    <item
        android:drawable="@drawable/p36"
        android:duration="150"/>
    <item
        android:drawable="@drawable/t2"
        android:duration="200"/>
    <item
        android:drawable="@drawable/p28"
        android:duration="150"/>
    <item
        android:drawable="@drawable/t3"
        android:duration="200"/>
    <item
        android:drawable="@drawable/p52"
        android:duration="150"/>
    <item
        android:drawable="@drawable/t4"
        android:duration="200"/>
    <item
        android:drawable="@drawable/p49"
        android:duration="150"/>
    <item
        android:drawable="@drawable/t5"
        android:duration="200"/>
    <item
        android:drawable="@drawable/p30"
        android:duration="150"/>
    <item
        android:drawable="@drawable/t6"
        android:duration="200"/>
    <item
        android:drawable="@drawable/p5"
        android:duration="150"/>
    <item
        android:drawable="@drawable/t1"
        android:duration="200"/>
    <item
        android:drawable="@drawable/p24"
        android:duration="150"/>
    <item
        android:drawable="@drawable/t9"
        android:duration="200"/>
    <item
        android:drawable="@drawable/p19"
        android:duration="150"/>
    <item
        android:drawable="@drawable/t3"
        android:duration="200"/>
    <item
        android:drawable="@drawable/p28"
        android:duration="150"/>
    <item
        android:drawable="@drawable/t10"
        android:duration="200"/>
    <item
        android:drawable="@drawable/p31"
        android:duration="150"/>
    <item
        android:drawable="@drawable/t7"
        android:duration="200"/>
    <item
        android:drawable="@drawable/p23"
        android:duration="150"/>
    <item
        android:drawable="@drawable/t4"
        android:duration="200"/>
    <item
        android:drawable="@drawable/p35"
        android:duration="150"/>
    <item
        android:drawable="@drawable/t8"
        android:duration="200"/>
    <item
        android:drawable="@drawable/p6"
        android:duration="150"/>
    <item
        android:drawable="@drawable/t2"
        android:duration="200"/>
    <item
        android:drawable="@drawable/p24"
        android:duration="150"/>
    <item
        android:drawable="@drawable/t9"
        android:duration="200"/>
    <item
        android:drawable="@drawable/p35"
        android:duration="150"/>
    <item
        android:drawable="@drawable/t4"
        android:duration="200"/>
    <item
        android:drawable="@drawable/p5"
        android:duration="150"/>
    <item
        android:drawable="@drawable/t6"
        android:duration="200"/>
    <item
        android:drawable="@drawable/p6"
        android:duration="150"/>
    <item
        android:drawable="@drawable/t3"
        android:duration="200"/>
    <item
        android:drawable="@drawable/p21"
        android:duration="150"/>
    <item
        android:drawable="@drawable/t10"
        android:duration="200"/>
    <item
        android:drawable="@drawable/p32"
        android:duration="150"/>
    <item
        android:drawable="@drawable/t5"
        android:duration="200"/>
</animation-list>

对于任何感兴趣的人,这是我的解决方案:-)

    final ImageView imageViewMeasurement = (ImageView) findViewById(measurement_image_view);


    imageViewMeasurement.postDelayed(new Runnable(){
                                         @Override
                                         public void run() {
                                             imageViewMeasurement.setImageResource(R.color.colorGreyMeasuerementScreen);
                                             Log.d("QuantifyDesign", "1. MeasurementScreen Default");
                                         }}
            ,0);

    imageViewMeasurement.postDelayed(new Runnable(){
                                         @Override
                                         public void run() {
                                             TypedArray images = getResources().obtainTypedArray(R.array.images_primes_1);
                                             int chosenImageNumber = (int) (Math.random() * images.length());

                                             // setImageResource to the random chosenImageNumber
                                             imageViewMeasurement.setImageResource(images.getResourceId(chosenImageNumber, R.color.colorGreyMeasuerementScreen));
                                             images.recycle();

                                             // Confirmation if the random generator picked a Number from the array
                                             String chosenImageNumberTest = String.valueOf(chosenImageNumber);
                                             Log.d("QuantifyDesignPrime", chosenImageNumberTest);
                                             Log.d("QuantifyDesign", "2. MeasurementScreen Prime 16");
                                         }}
            ,5000);

    imageViewMeasurement.postDelayed(new Runnable(){
                                         @Override
                                         public void run() {
                                             imageViewMeasurement.setImageResource(R.color.colorBlackMeasurementScreen);
                                             Log.d("QuantifyDesign", "3. MeasurementScreen Black Screen");
                                         }}
            ,5750);

    imageViewMeasurement.postDelayed(new Runnable(){
                                         @Override
                                         public void run() {
                                             TypedArray images = getResources().obtainTypedArray(R.array.images_target);
                                             int chosenImageNumber = (int) (Math.random() * images.length());

                                             // setImageResource to the random chosenImageNumber
                                             imageViewMeasurement.setImageResource(images.getResourceId(chosenImageNumber, R.color.colorGreyMeasuerementScreen));
                                             images.recycle();

                                             // Confirmation if the random generator picked a Number from the array
                                             String chosenImageNumberTest = String.valueOf(chosenImageNumber);
                                             Log.d("QuantifyDesignTarget", chosenImageNumberTest);
                                             Log.d("QuantifyDesign", "4. MeasurementScreen Target 1");
                                         }}
            ,6000);

    imageViewMeasurement.postDelayed(new Runnable(){
                                         @Override
                                         public void run() {
                                             imageViewMeasurement.setImageResource(R.drawable.buttonklickmeasurement);
                                             Button resetButtonLike=(Button)findViewById(R.id.button_like);
                                             resetButtonLike.setVisibility(View.VISIBLE); //To set visible
                                             Button resetButtonDislike=(Button)findViewById(R.id.button_dislike);
                                             resetButtonDislike.setVisibility(View.VISIBLE); //To set visible
                                             Log.d("QuantifyDesign", "5. MeasurementScreen Apell");
                                         }}
            ,7000);