Android:从2个池中随机选择图像

时间:2016-12-18 20:05:35

标签: android image performance android-animation

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

  1. 从池1中随机选择图像1秒;
  2. 显示黑屏(1秒)
  3. 从池2中随机选择图像1秒钟;
  4. 显示红色屏幕(等到按下按钮
  5. 多次重复1-4次。 问题1:实现这种行为的最佳做法是什么?我尝试使用“动画可绘制”,但据我所知,你不能从该xml中随机选择图像?

    问题2:效率怎么样?在按照问题1中的描述尝试之后,它显示:“我/编舞:跳过了59帧!应用程序可能在其主线程上做了太多工作。” - >我需要多线程吗?

    最佳, tigercode

2 个答案:

答案 0 :(得分:1)

这就是乐趣:

我的课程(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(其他持续时间,仅显示:"图像池1" - "图像池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>

答案 1 :(得分:0)

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

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