MainActivity
public class MainActivity extends AppCompatActivity {
Handler mHandler= new Handler ( ) ;
Thread myThread;
Boolean isStop= false ;
private final int NEXT= 0x01 ;
ImageView img_show;
int position;
int [ ] photos;
@Override
protected void onCreate ( Bundle savedInstanceState) {
super . onCreate ( savedInstanceState) ;
setContentView ( R. layout. activity_main) ;
img_show= findViewById ( R. id. img) ;
photos= new int [ ] {
R. drawable. timg,
} ;
position= 0 ;
img_show. setImageResource ( photos[ position] ) ;
myThread= new Thread ( new Runnable ( ) {
@Override
public void run ( ) {
while ( ! isStop) {
try {
Thread. sleep ( 2000 ) ;
} catch ( Exception e) {
Log. e ( "Error" , e. toString ( ) ) ;
}
Runnable mCallback= new Runnable ( ) {
@Override
public void run ( ) {
position= ( position+ 1 ) % photos. length;
img_show. setImageResource ( photos[ position] ) ;
}
} ;
mHandler. post ( mCallback) ;
}
}
} ) ;
}
@Override
protected void onStart ( ) {
super . onStart ( ) ;
if ( myThread!= null) {
myThread. start ( ) ;
}
}
@Override
protected void onStop ( ) {
super . onStop ( ) ;
if ( myThread!= null) {
isStop= true ;
}
}
}
activity_main.xml
< LinearLayout
android:layout_width = "match_parent"
android:layout_height = "match_parent" >
< ImageView
android:id = "@+id/img"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:scaleType = "fitCenter" />
< / LinearLayout >