ImageSwitch是图片切换器,其实可以把它当做Gallery,只不过它只能一次显示一张图片,而Gallery则能显示多张。

程序截图:

程序代码:

1、布局文件main.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <!-- Gallery --> <LinearLayout android:id="@+id/linearLayout2" android:layout_width="fill_parent" android:layout_height="100dp" android:gravity="top|left" > <Gallery android:id="@+id/gallery" android:layout_width="fill_parent" android:spacing="16dp" android:layout_height="100dp"/> </LinearLayout> <!-- ImageSwitch --> <LinearLayout android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center"> <ImageSwitcher android:id="@+id/imageSwitcher" android:layout_width="fill_parent" android:layout_height="wrap_content"/> </LinearLayout> </LinearLayout>

2、代码

Activity文件:

package com.myandroid.test; import java.io.File; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Iterator; import android.app.Activity; import android.graphics.Color; import android.graphics.SweepGradient; import android.os.Bundle; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.view.animation.AnimationUtils; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.Gallery.LayoutParams; import android.widget.Gallery; import android.widget.ImageSwitcher; import android.widget.ImageView; import android.widget.ViewSwitcher.ViewFactory; public class GallyAndImageSwitch extends Activity { private Gallery gallery; private ImageSwitcher imageSwitcher; private ArrayList<Integer> imageIndexs; private int downX, upX; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); getWidget(); } /** * 初始化控件和事件响应 */ private void getWidget() { //获取控件对象 gallery = (Gallery)findViewById(R.id.gallery); imageSwitcher = (ImageSwitcher)findViewById(R.id.imageSwitcher); imageIndexs = getImageIndexs();//获取文件drawable下的图片ID MyImageAdapter imageAdapter = new MyImageAdapter(this, imageIndexs);//设置Gallery gallery.setAdapter(imageAdapter); gallery.setOnItemSelectedListener(new OnItemSelectedListener() {//设置Item选中处理 @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { Log.e("Gallery", "onItemSelected"); arg1.setBackgroundColor(Color.LTGRAY); imageSwitcher.setImageResource(imageIndexs.get(arg2)); } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }); //设置图片切换器 imageSwitcher.setFactory(new MyViewFactory()); imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, //设置View进入屏幕时候使用的动画 android.R.anim.fade_in)); imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,//设置View消失时候使用的动画 android.R.anim.fade_out)); imageSwitcher.setOnTouchListener(new OnTouchListener(){ //设置ImageView触摸事件响应处理 @Override public boolean onTouch(View v, MotionEvent event) { Log.e("ImageSwitcher", "onTouch"); int index = 0; if(event.getAction() == MotionEvent.ACTION_DOWN) { downX=(int) event.getX();//取得按下时的坐标 return true; }else if(event.getAction() == MotionEvent.ACTION_UP) { upX = (int)event.getX();//获得松开时的坐标 if(downX - upX > 100) {//从右向左,下一幅图 index = (gallery.getSelectedItemPosition() + 1) % gallery.getCount(); }else if(upX - downX > 100) { //从左向右,上一幅图 index = (gallery.getSelectedItemPosition()) == 0? gallery.getCount() : gallery.getSelectedItemPosition() - 1; }else { index = gallery.getSelectedItemPosition(); } gallery.setSelection(index, true); ////改变gallery图片所选,自动触发gallery的onItemSelected return true; } return false; } }); } /** * 获取Drawable文件夹下的图片ID * @return 放回图片ID集合 */ private ArrayList<Integer> getImageIndexs() { ArrayList<Integer> imageIndexs = new ArrayList<Integer>(); //采用反射获取文件 Field[] iamgeFiles = R.drawable.class.getDeclaredFields(); //遍历文件 for (Field image : iamgeFiles) { if(!"icon".equals(image.getName())) { //排除名为“icon”的系统图片 int index = 0; try { index = image.getInt(R.drawable.class); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } imageIndexs.add(index); } } return imageIndexs; } /** * 用于ImageSwitch的图片加载 * @author Kobi * */ private class MyViewFactory implements ViewFactory { @Override public View makeView() { Log.e("ImageSwitcher", "makeView"); ImageView imageView = new ImageView(GallyAndImageSwitch.this); imageView.setBackgroundColor(0xFF000000); imageView.setScaleType(ImageView.ScaleType.CENTER);//居中 imageView.setLayoutParams(new ImageSwitcher.LayoutParams(//自适应图片大小 LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); return imageView; } } }

ImageAdapter文件:

package com.myandroid.test; import java.util.ArrayList; import java.util.List; import android.content.Context; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.view.ViewGroup.LayoutParams; import android.widget.BaseAdapter; import android.widget.Gallery; import android.widget.ImageView; public class MyImageAdapter extends BaseAdapter { private Context context; private ArrayList<Integer> imageIndexs; public MyImageAdapter(Context context, ArrayList<Integer> imageIndexs) { this.context = context; this.imageIndexs = imageIndexs; } @Override public int getCount() { // TODO Auto-generated method stub return imageIndexs.size(); } @Override public Object getItem(int arg0) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { Log.e("ImageAdapter", "getView"); ImageView imageView = new ImageView(context); imageView.setImageResource((Integer)imageIndexs.get(position)); imageView.setAdjustViewBounds(true); imageView.setLayoutParams(new Gallery.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); return imageView; } }

更多相关文章

  1. 一款常用的 Squid 日志分析工具
  2. GitHub 标星 8K+!一款开源替代 ls 的工具你值得拥有!
  3. RHEL 6 下 DHCP+TFTP+FTP+PXE+Kickstart 实现无人值守安装
  4. Linux 环境下实战 Rsync 备份工具及配置 rsync+inotify 实时同步
  5. 如何把公司要求的一些文件预到android 用户空间系统空间
  6. Android实现图片叠加效果的两种方法
  7. Android(安卓)sdk manager 显示 “Done loading packages”,该怎
  8. Android简易实战教程--第九话《短信备份~二》
  9. font 在xml使用

随机推荐

  1. Android 数据存储02之文件读写
  2. Android原生的TTS(语音播报功能)
  3. Android3.2运行报错:[2011-09-09 14:50:21
  4. 如何完全卸载Android Studio并进行重新安
  5. Android 4.0 external下功能库说明
  6. Android报错之.android/repositories.cfg
  7. Android使用FFmpeg(一)--编译ffmpeg
  8. android中求区域内两个坐标之间的距离的
  9. 《Gradle 权威指南》读书笔记——第八章
  10. Android Studio(七):项目从Eclipse到Andro