2016-12-18 41 views
-1

我有一個應用程序,我計劃在屏幕上顯示一些設計。過程如下:Android:隨機從2個池中挑選圖像

  1. 顯示從池1中隨機選取的圖像1秒;
  2. 顯示黑屏(1秒)
  3. 顯示從池2中隨機選取的圖像1秒;
  4. 顯示一個紅屏(等到按鈕被按下

重複1-4多次 問題1:什麼是實現這樣的行爲,最好的做法我試圖用「動畫可繪製「,但據我所知,你不能從該xml中隨機選擇圖像?

問題2:效率如何?按照問題1中的描述進行試驗後,它顯示:」I/Choreographer:跳過59幀!應用程序可能在其主線程上做了太多工作。「 - >我需要多線程嗎?

最佳, 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>