Android学习笔记 ImageSwitcher图片切换组件的使用
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <ImageSwitcher android:id="@+id/myImageSwitcher" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal"> <Button android:id="@+id/Btn_last" android:layout_width="wrap_content" android:layout_height="wrap_content" android:enabled="false" android:text="上一张图片" /> <Button android:id="@+id/Btn_next" android:layout_width="wrap_content" android:layout_height="wrap_content" android:enabled="true" android:text="下一张图片" /> </LinearLayout> </LinearLayout>
MainActivity.java
public class MainActivity extends Activity { private Button btnLast=null; private Button btnNext=null; private ImageSwitcher myImageSwitcher=null; private int[] imgRes=new int[] { R.drawable.png_01, R.drawable.png_02, R.drawable.png_03, R.drawable.png_04, R.drawable.png_05, R.drawable.png_06, R.drawable.png_07, R.drawable.png_08 }; //定义资源文件数组 private int foot=0; //表示当前图片的标记 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.myImageSwitcher=(ImageSwitcher)super.findViewById(R.id.myImageSwitcher); this.btnLast=(Button)super.findViewById(R.id.Btn_last); this.btnNext=(Button)super.findViewById(R.id.Btn_next); this.btnLast.setOnClickListener(new OnClickListenerImpl()); this.btnNext.setOnClickListener(new OnClickListenerImpl()); this.myImageSwitcher.setFactory(new ViewFactoryImpl()); this.myImageSwitcher.setImageResource(imgRes[this.foot++]); this.myImageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in)); //设置图片切换效果 this.myImageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out)); //设置图片切换效果 } private class OnClickListenerImpl implements OnClickListener{ @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.Btn_last: MainActivity.this.myImageSwitcher.setImageResource(MainActivity.this.imgRes[MainActivity.this.foot--]); checkBtnEnable(); break; case R.id.Btn_next: MainActivity.this.myImageSwitcher.setImageResource(MainActivity.this.imgRes[MainActivity.this.foot++]); checkBtnEnable(); break; default: break; } } } //按钮检测 private void checkBtnEnable(){ if(this.foot<this.imgRes.length-1){ //检查是否为最后一张图片 this.btnNext.setEnabled(true); }else{ this.btnNext.setEnabled(false); } if(this.foot==0){ //检查是否为第一张图片 this.btnLast.setEnabled(false); }else{ this.btnLast.setEnabled(true); } } //工厂 private class ViewFactoryImpl implements ViewFactory{ @Override public View makeView() { ImageView img=new ImageView(MainActivity.this); img.setBackgroundColor(0xFFFFFFFF);//设置背景色 img.setScaleType(ImageView.ScaleType.CENTER);//居中 img.setLayoutParams( new ImageSwitcher.LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT )); //定义组件的显示 return img; //反回img } }
量的积累到质的飞越