2016-12-18 5 views
-1

У меня есть приложение, где я планирую измерить несколько рисунков, показанных на экране. Этот процесс выглядит следующим образом:Android: Случайно выберите изображения из 2-х бассейнов

  1. Показать случайно выбранное изображение из пула 1 на 1 секунду;
  2. Показать черный экран (1сек)
  3. Показать случайно выбранное изображение из бассейна 2 в течение 1 секунды;
  4. Показать красный экран (подождать, пока не будет нажата кнопка

Повторить 1-4 для несколько раз Вопрос 1:.? Что является лучшей практике реализовать такое поведение, я пытался использовать " анимационная анимация », но насколько я понял, вы не можете случайно выбирать изображения из этого xml?

Вопрос 2: Как насчет эффективности? Попробовав его, как описано в вопросе 1, он показывает:« I/Choreographer: Skipped 59 кадров! Приложение может делать слишком много работы над своей основной нитью. »-> Мне нужна многопоточность здесь?

Best, tigercode

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