public class ImageViewSwitcherAcvitity extends Activity implements ViewFactory,OnClickListener{ private Button button1; private Button button2; private Button button3; private ImageSwitcher iSwitcher; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.imageview_switcher); button1 = (Button) findViewById(R.id.button1); button2 =(Button) findViewById(R.id.button2); button3 = (Button) findViewById(R.id.button3); iSwitcher = (ImageSwitcher) findViewById(R.id.is_witcher); iSwitcher.setFactory(this); iSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in)); iSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out)); button1.setOnClickListener(this); button2.setOnClickListener(this); button3.setOnClickListener(this); } @Override public View makeView() { ImageView i = new ImageView(this); i.setBackgroundColor(0xFF000000); i.setScaleType(ImageView.ScaleType.FIT_CENTER); i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); return i; } @Override public void onClick(View v) { switch (v.getId()) { case R.id.button1: iSwitcher.setImageResource(R.drawable.big1); break; case R.id.button2: iSwitcher.setImageResource(R.drawable.big2); break; case R.id.button3: iSwitcher.setImageResource(R.drawable.big3); break; default: break; } } }
xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> </LinearLayout> <ImageSwitcher android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/is_witcher"> </ImageSwitcher> </LinearLayout>