在Android中显示图像序列

时间:2014-09-07 17:10:01

标签: android android-layout android-activity

我有5张1.png到5.png的图像并存储在res / drawable文件夹中。现在,我想制作一个应用程序来随机播放这些图像。首先,我将从1到5创建一个随机数,例如

Random r = new Random();
int ii = r.nextInt(max - min + 1) + min;//min=1 and max=5

之后,我会检查号码,如果号码是1,我会显示1.png。等等... 我想问你一些关于如何在android中实现它的东西。

//XML file
<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
    <RelativeLayout
        android:id="@+id/seq_image"
        android:layout_width="120dp"
        android:layout_height="180dp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="18dp"
        android:background="@drawable/1" >
    </RelativeLayout>
</RelativeLayout>

2 个答案:

答案 0 :(得分:1)

  1. 将所有图片的可见性设置为不可见
  2. 使用线程来表达:

          Thread.sleep(1000);
    
  3. 3.使用开关或条件选择根据生成的随机数显示哪个图像,并使该图像可见,同时使所有其他图像不可见

    I am suggesting something like this take it as algorithm:
    
        ImageView img1........img5;
    
        img1.setVisibility(View.INVISIBLE);
    
        img2.setVisibility(View.INVISIBLE); 
        ....
        img5.setVisibility(View.INVISIBLE); 
    
        for(int i=0;i<5;i++){
        Random r = new Random();
        int ii = r.nextInt(5 - 1 + 1) + 1;
    
        switch(ii){
    
        case 1:
            // display first image and hide others
        case 2:
            // display second image and hide others
    
        }
    
       Thread.sleep(1000);
    
        }
    

答案 1 :(得分:0)

生成随机数后,您将组合int和文件名,然后您可以将Bitmap附加到ImageView。

public Bitmap getBitmapFromAssets(int yourRandomNumber, String fileName) throws IOException {

        AssetManager assetManager = getAssets();

        InputStream istr = assetManager.open(yourRandomNumber + fileName);
        Bitmap bitmap = BitmapFactory.decodeStream(istr);

        return bitmap;
    }