이미지 랜덤하게 출력하기 (ImageView)

프로그래밍/Android (Java)|2015. 10. 15. 13:50
반응형

activity_main.xml

 

<ImageView 
    android:id="@+id/SYSDOCU"
    android:layout_width="350dp"
    android:layout_height="160dp" />

 

 

MainActivity.java

 

package com.tistory.sysdocu;
import android.os.Bundle;
import android.app.Activity;
import android.widget.ImageView;
import android.graphics.drawable.Drawable;


public class MainActivity extends Activity {


        int[] images = new int[] {R.drawable.beautifulindonesia1, R.drawable.beautifulindonesia2, R.drawable.beautifulindonesia3, R.drawable.beautifulindonesia4, R.drawable.beautifulindonesia5};


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


        ImageView mImageView = (ImageView)findViewById(R.id.SYSDOCU);
        int imageId = (int)(Math.random() * images.length);
        mImageView.setBackgroundResource(images[imageId]);


        }


}

 

 

 

 

반응형

댓글()