ImageSwitcher exercise (android)

Source: Internet
Author: User

ImageSwitch requires a ViewFactory interface method to return an ImageView object and specify the setFactory method.

It tried to use ImageView to implement ImageSwitcher. There was no difference, but what was the difference?

 

The Code is as follows:

Xml

 

<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">

<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello"/>
<ImageView
Android: id = "@ + id/iv"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
/>
<Button
Android: id = "@ + id/btn"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "switching images"
/>

<ImageSwitcher
Android: id = "@ + id/imageswitch"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
> </ImageSwitcher>
<Button
Android: id = "@ + id/btn2"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "switching Image 2"
/>
</LinearLayout>

 

Java code

Package zziss. android. imageswitchtest;

Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. View;
Import android. widget. Button;
Import android. widget. ImageSwitcher;
Import android. widget. ImageView;
Import android. widget. ViewSwitcher. ViewFactory;

Public class ImageSwitchTestActivity extends Activity implements ViewFactory {
/** Called when the activity is first created .*/
Private ImageView iv;
Private ImageSwitcher imw;
Private Button btn;
Private Button btn2;
Private int I;
Private int index;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Iv = (ImageView) this. findViewById (R. id. iv );
Btn = (Button) this. findViewById (R. id. btn );
Btn2 = (Button) this. findViewById (R. id. btn2 );

Imw = (ImageSwitcher) this. findViewById (R. id. imageswitch );
Imw. setFactory (this );
I = 0;
Index = 0;
Btn. setOnClickListener (new View. OnClickListener (){

@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
/* If (I = 0)
{
Iv.setimageresource( r.drawable.png 1410 );
I = 1;
}
Else
{
Iv.setimageresource( r.drawable.png 1411 );
I = 0;
}*/
If (index> = 3)
Index = 0;
Iv. setImageResource (imagelist [index]);
Index ++;
}
});

Btn2.setOnClickListener (new View. OnClickListener (){

@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
If (index> = 3)
Index = 0;
Imw. setImageResource (imagelist [index]);
Index ++;
}
});

}

Private static int [] imagelist =
{
R.drawtable.png 1410,
R.drawtable.png 1411,
R.drawtable.png 1412
};
@ Override
Public View makeView (){
// TODO Auto-generated method stub
Return new ImageView (this );
}
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.