Android使用ImageSwitcher+Gallery实现图片切换

   Android应用开发中少不了同图片数据打交道,无论是头像还是图片相册之类的等等。有些时候在图片较多的时候,我们就要通过切换的方式来查看图片,实现方式很多,要以用ViewPager实现切换,这也是目前用得比较普遍的,也可以用viewflipper还实现,也可以自定义工具类来实现.....,今天我们用的是Android自带的两个原生控件来实现图片切换效果。本想贴图的,但是发现发布CSDN博客贴图真的有些不方便,所以就分偷懒了~~

   --------------------------------------界面布局文件:activity_image_viewer.xml-------------------------------

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageSwitcher
        android:id="@+id/imageSwitcher"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="4" >
    </ImageSwitcher>

    <Gallery
        android:id="@+id/gallery"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:spacing="10sp" />

</LinearLayout>

---------------------------------------图片item布局image_viewer_item.xml-------------------------------------

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

-----------------------------------------------------------------------ImageViewerActivity 相关代码-----------------------------------------------------------

public class ImageViewerActivity extends Activity implements OnItemSelectedListener, ViewFactory {
/**Gallery控件 */
private Gallery mGallery;
/**ImageSwitcher控件 */
private ImageSwitcher mImageSwitcher;
/**选中图片的索引*/
private int selectedIndex = 0;
/**松开手指时坐标*/
private int upX;
/**按下手指时坐标*/
private int downX;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_viewer);
findViews();
init();
}


/**初始化UI */
private void findViews() {
mGallery = (Gallery) findViewById(R.id.gallery);
mImageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher);
mGallery.setOnItemSelectedListener(this);
mImageSwitcher.setOnTouchListener(onTouchListener);
}


/**设置参数 */
private void init() {
/**图片适配器 */
ImageViewerAdapter imageAdapter = new ImageViewerAdapter(this);
mGallery.setAdapter(imageAdapter);
/**手指滑动时,相互切换 */
mImageSwitcher.setFactory(this);
}


@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
mImageSwitcher.setImageResource(ImageViewerAdapter.images[position]);
selectedIndex = position;
}


/**ImageSwitcher手机滑动监听事件 */
private OnTouchListener onTouchListener = new OnTouchListener() {


@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
downX = (int) event.getX();
return true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
upX = (int) event.getX();
}
if (upX - downX > 100) {
/**从左拖到右,即看前一张,如果是第一,则去到尾部*/
if (mGallery.getSelectedItemPosition() == 0)
selectedIndex = mGallery.getCount() - 1;
else selectedIndex = mGallery.getSelectedItemPosition() - 1;
}
if (downX - upX > 100) {
/**从右拖到左,即看后一张,如果是最后,则去到第一*/
if (mGallery.getSelectedItemPosition() == (mGallery.getCount() - 1))
selectedIndex = 0;
else selectedIndex = mGallery.getSelectedItemPosition() + 1;
}
mGallery.setSelection(selectedIndex, true);
return true;
}


};


@Override
public void onNothingSelected(AdapterView<?> parent) {}


@Override
public View makeView() {
ImageView imageView = new ImageView(this);
/**设置伸缩比例*/
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
return imageView;
}

}

-----------------------------------------------------------------------数据适配器ImageViewerAdapter相关代码-------------------------------------------------------

public class ImageViewerAdapter extends BaseAdapter {
/**上下文Context*/
private Context mContext;
/**布局管理器 */
private LayoutInflater layoutInfalter;
/**图片(自己定义的6张) */
public static Integer[] images = { R.drawable.image01, R.drawable.image02, R.drawable.image03, R.drawable.image04, R.drawable.image5,R.drawable.image06 };


public ImageViewerAdapter(Context context) {
super();
this.mContext = context;
layoutInfalter = LayoutInflater.from(mContext);
}

@Override
public int getCount() {
return images.length;
}


@Override
public Object getItem(int position) {
return position;
}


@Override
public long getItemId(int position) {
return position;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (null == convertView) {
viewHolder = new ViewHolder();
convertView = (ImageView) layoutInfalter.inflate(R.layout.image_viewer_item, null);
viewHolder.imageView = (ImageView) convertView.findViewById(R.id.image);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.imageView.setImageResource(images[position]);
viewHolder.imageView.setLayoutParams(new Gallery.LayoutParams(100, LayoutParams.FILL_PARENT));
return convertView;
}


private static class ViewHolder {
private ImageView imageView;
}
}

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。