ImageSwitcher 图片切换的控件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | package com.zhu.testandroid; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup.LayoutParams; import android.view.animation.AnimationUtils; import android.widget.Button; import android.widget.ImageSwitcher; import android.widget.ImageView; import android.widget.ViewSwitcher.ViewFactory; public class MyImageSwitcher extends Activity { private ImageSwitcher imageSwitcher; private Button btnPrevious; private Button btnNext; private int foot = 0 ; private int [] imgRes = new int [] { R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.e, }; public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); // 生命周期方法 super .setContentView(R.layout.activity_image_sw); // 设置要使用的布局管理器 imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher); btnPrevious = (Button) findViewById(R.id.btnPrevious); btnNext = (Button) findViewById(R.id.btnNext); imageSwitcher.setFactory( new ViewFactory() { // 设置转化工厂 @Override public View makeView() { ImageView imageView = new ImageView( MyImageSwitcher. this ); imageView.setBackgroundColor( 0xFFFFFFFF ); imageView.setScaleType(ImageView.ScaleType.CENTER); // 居中显示 imageView .setLayoutParams( new ImageSwitcher.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); // 定义组件 return imageView; } }); imageSwitcher.setImageResource(imgRes[foot++]); // 初始化时显示,必须放在工厂后面,否则会报NullPointerException imageSwitcher.setInAnimation(AnimationUtils.loadAnimation( this , android.R.anim.fade_in)); // 设置动画 imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation( this , android.R.anim.fade_out)); // 设置动画 btnPrevious.setOnClickListener( new OnClickListener() { public void onClick(View v) { MyImageSwitcher. this .imageSwitcher .setImageResource(imgRes[foot--]); MyImageSwitcher. this .checkBtnEnable(); } }); btnNext.setOnClickListener( new OnClickListener() { public void onClick(View v) { MyImageSwitcher. this .imageSwitcher .setImageResource(imgRes[foot++]); MyImageSwitcher. this .checkBtnEnable(); } }); } protected void checkBtnEnable() { // 判断按钮可用状态 if ( this .foot < this .imgRes.length - 1 ) { this .btnNext.setEnabled( true ); } else { this .btnNext.setEnabled( false ); } if ( this .foot == 0 ) { this .btnPrevious.setEnabled( false ); } else { this .btnPrevious.setEnabled( true ); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | <?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" > <ImageSwitcher android:id= "@+id/imageSwitcher" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_gravity= "center" /> <LinearLayout android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:orientation= "horizontal" > <Button android:id= "@+id/btnPrevious" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_weight= "1" android:enabled= "false" android:text= "上一张" /> <Button android:id= "@+id/btnNext" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_weight= "1" android:enabled= "true" android:text= "下一张" /> </LinearLayout> </LinearLayout> |
给工作留一个脚印,自己写一点工作必须,方便下次查询
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步