Android之Gallery和ImageSwitcher结合的用法

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

              程序截图:

                Android之Gallery和ImageSwitcher结合的用法_第1张图片

             程序代码:

             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; } }

 

你可能感兴趣的:(android,object,layout,Integer,Class,encoding)