【问题标题】:how to Display Image automatically after a random time如何在随机时间后自动显示图像
【发布时间】:2014-02-28 19:56:13
【问题描述】:

我构建了一个 android 应用程序,我希望在随机时间后一张一张地显示图像。 意思是

图片 1 展示

10 秒后

图片2展示

30 秒后

图 3 显示

50 秒后

图 4 显示

我有显示图像的代码,但它每 10 秒连续显示一次图像,而我希望在随机时间后显示图像。

public class MainActivity extends Activity {
  ImageView imageView;


 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
     imageView = (ImageView) findViewById(R.id.imageView1);
     final int []imageArray=
       {R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d,R.drawable.e};


       final Handler handler = new Handler();
        Runnable runnable = new Runnable() {
        int i=0;
        public void run() {
            imageView.setImageResource(imageArray[i]);
            i++;
            if(i>imageArray.length-1)
            {
            i=0;    
            }
            handler.postDelayed(this, 10000);  //for interval...
         }

       };
      handler.postDelayed(runnable, 10000); //for initial delay..

      }

xml文件是-

     <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

  </RelativeLayout>

【问题讨论】:

    标签: android timer handler


    【解决方案1】:

    我们还可以使用模运算概念来重复图像。

    int cimi;    
    int img[] ={R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d,R.drawable.e,R.drawable.f};
    // inside onCreate()..
    i = (ImageView)findViewById(R.id.iv1);
    Runnable runn = new Runnable() {
            @Override
            public void run() {
                i.setImageResource(img[cimi]);
                cimi++;
                cimi = cimi%img.length;
                i.postDelayed(this,2000);
            }
        };
        i.postDelayed(runn,2000);
    

    【讨论】:

      【解决方案2】:

      设置最小和最大间隔:

      static final float MIN_INTERVAL = 10000;
      static final float MAX_INTERVAL = 20000;
      

      那么对于区间:

      handler.postDelayed(this, Math.rand()*(MAX_INTERVAL-MIN_INTERVAL) + MIN_INTERVAL);
      

      【讨论】:

      • 先生,我试过了,但是当我运行应用程序时显示黑屏,没有显示图像。
      • 所以第一张图片显示正常,但第一次改变时,它只是变成黑色?
      【解决方案3】:

      如果您希望图像之间有随机的时间量,则需要使用随机数生成器。例如:

      Random r = new Random();
      int timer = r.nextInt(60000-30000) + 30000;
      

      然后你可以在你的运行函数中使用handler.postDelayed(this, timer);,并且任何时候你想生成一个新的随机数,只需再次调用r.nextInt

      这里有更多关于随机数的信息:How can I generate random number in specific range in Android?

      【讨论】:

        【解决方案4】:

        尝试如下:

        AnimationDrawable animation = new AnimationDrawable();
        animation.addFrame(getResources().getDrawable(R.mipmap.download), 1000);
        animation.addFrame(getResources().getDrawable(R.mipmap.downloada), 5000);
        animation.addFrame(getResources().getDrawable(R.mipmap.ic_launcher), 3000);
        animation.setOneShot(false);
        
        ImageView imageAnim =  (ImageView) findViewById(R.id.img);
        imageAnim.setBackgroundDrawable(animation);
        
        // start the animation!
        animation.start();
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-04-20
          • 1970-01-01
          • 1970-01-01
          • 2018-01-18
          • 2010-11-08
          • 1970-01-01
          • 1970-01-01
          • 2012-01-01
          相关资源
          最近更新 更多