--------------------------------------------Layoutactivity_main.xml--------------------------

<LinearLayoutxmlns: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">

<ImageSwitcher

android:id="@+id/imageSwitcher"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="0.2">

</ImageSwitcher>

<Gallery

android:id="@+id/gallery"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="0.8"android:spacing="10sp"/>

</LinearLayout>

--------------------------------------------Layoutgallery_item.xml---------------------------

<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/RelativeLayout1"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

<ImageView

android:id="@+id/image"

android:layout_width="wrap_content"

android:layout_height="wrap_content"android:src="@drawable/ic_launcher"/>

</RelativeLayout>

--------------------------------------------AdapterImageAdapter.xml------------------------

packagecom.ch11;

importandroid.content.Context;

importandroid.view.LayoutInflater;

importandroid.view.View;

importandroid.view.ViewGroup;

importandroid.view.ViewGroup.LayoutParams;

importandroid.widget.BaseAdapter;

importandroid.widget.Gallery;

importandroid.widget.ImageView;

/**

*

*项目名称:com.ch11

*类名称:ImageAdapter

*类描述:自定义适配器,为Gallery提供数据

*创建人:方勇

*创建时间:2012-11-16下午4:08:11

*Copyright(c)方勇-版权所有

*/

publicclassImageAdapterextendsBaseAdapter{

/*上下文对象*/

privateContextmContext;

/*布局管理对象*/

privateLayoutInflaterlayoutInfalter;

/*图片资源集合*/

publicstaticInteger[]images={R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d,R.drawable.e};

publicImageAdapter(Contextcontext){

super();

this.mContext=context;

layoutInfalter=LayoutInflater.from(mContext);

}

/*Gallery对应的图片个数*/

@Override

publicintgetCount(){

//TODOAuto-generatedmethodstub

returnimages.length;

}

@Override

publicObjectgetItem(intposition){

//TODOAuto-generatedmethodstub

returnposition;

}

@Override

publiclonggetItemId(intposition){

//TODOAuto-generatedmethodstub

returnposition;

}

@Override

publicViewgetView(intposition,ViewconvertView,ViewGroupparent){

ImageViewimageView;

/*实例化ImageView对象*/

if(null==convertView){

/*获取根视图对象*/

Viewview=layoutInfalter.inflate(R.layout.gallery_item,null);

/*图片对象*/

imageView=(ImageView)view.findViewById(R.id.image);

}else{

imageView=(ImageView)convertView;

}

/*图片源*/

imageView.setImageResource(images[position]);

/*Gallery图片布局类型*/

imageView.setLayoutParams(newGallery.LayoutParams(100,LayoutParams.FILL_PARENT));

returnimageView;

}

}

--------------------------------------------MainActivity.java-----------------------------------

packagecom.ch11;

importandroid.app.Activity;

importandroid.os.Bundle;

importandroid.util.Log;

importandroid.view.MotionEvent;

importandroid.view.View;

importandroid.view.View.OnTouchListener;

importandroid.view.ViewGroup.LayoutParams;

importandroid.widget.AdapterView;

importandroid.widget.AdapterView.OnItemSelectedListener;

importandroid.widget.Gallery;

importandroid.widget.ImageSwitcher;

importandroid.widget.ImageView;

importandroid.widget.ViewSwitcher.ViewFactory;

/**

*

*项目名称:com.ch11

*类名称:MainActivity

*类描述:Galley,ImageSwitch,自定义适配器

*创建人:方勇

*创建时间:2012-11-16上午9:04:16

*Copyright(c)方勇-版权所有

*/

publicclassMainActivityextendsActivityimplementsOnItemSelectedListener,ViewFactory{

/*相册控件*/

privateGallerygallery;

/*图片切换控件*/

privateImageSwitcherimageSwitcher;

//选中的图片索引

privateintselectedTag=0;

//松开时的坐标x

privateintupX;

//按下时的坐标x

privateintdownX;

@Override

publicvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

findViews();

setListeners();

init();

}

/*初始化UI*/

privatevoidfindViews(){

gallery=(Gallery)findViewById(R.id.gallery);

imageSwitcher=(ImageSwitcher)findViewById(R.id.imageSwitcher);

}

/*监听事件*/

privatevoidsetListeners(){

gallery.setOnItemSelectedListener(this);

imageSwitcher.setOnTouchListener(onTouchListener);

}

/*设置参数*/

privatevoidinit(){

/*绑定图片适配器*/

ImageAdapterimageAdapter=newImageAdapter(this);

gallery.setAdapter(imageAdapter);

/*SetsthefactoryusedtocreatethetwoviewsbetweenwhichtheViewSwitcherwillflip*/

/*创建两个视图,手指滑动时,相互切换*/

imageSwitcher.setFactory(this);

}

/*选中事件*/

@Override

publicvoidonItemSelected(AdapterView<?>parent,Viewview,intposition,longid){

imageSwitcher.setImageResource(ImageAdapter.images[position]);

selectedTag=position;

}

/*ImageSwitcher的触摸事件*/

privateOnTouchListeneronTouchListener=newOnTouchListener(){

@Override

publicbooleanonTouch(Viewv,MotionEventevent){

if(event.getAction()==MotionEvent.ACTION_DOWN){

downX=(int)event.getX();//取得按下时的坐标x

returntrue;

}elseif(event.getAction()==MotionEvent.ACTION_UP){

upX=(int)event.getX();//取得松开时的坐标x

}

if(upX-downX>100){//从左拖到右,即看前一张

//如果是第一,则去到尾部

if(gallery.getSelectedItemPosition()==0)

selectedTag=gallery.getCount()-1;

else

selectedTag=gallery.getSelectedItemPosition()-1;

}

if(downX-upX>100)//从右拖到左,即看后一张

{

//如果是最后,则去到第一

if(gallery.getSelectedItemPosition()==(gallery.getCount()-1))

selectedTag=0;

else

selectedTag=gallery.getSelectedItemPosition()+1;

}

//Jumpdirectlytoaspecificitemintheadapterdata.

//直接跳到数据适配器中指定项。

//改变gallery图片所选,自动触发ImageSwitcher的setOnItemSelectedListener

gallery.setSelection(selectedTag,true);

returntrue;

}

};

@Override

publicvoidonNothingSelected(AdapterView<?>parent){

//TODOAuto-generatedmethodstub

}

@Override

publicViewmakeView(){

Log.v("fy","makeViewstart...");

ImageViewimageView=newImageView(this);

//伸缩比例与图片位置

imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);

//布局

imageView.setLayoutParams(newImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));

Log.v("fy","makeViewend...");

returnimageView;

}

}

--------------------------------------------效果图-----------------------------------------------

<!--EndFragment-->

更多相关文章

  1. Android中ListView以及数组适配器(ArrayAdapter)的使用
  2. Android内核开发:图解Android系统的启动过程
  3. android ImageView scaleType属性
  4. Android(安卓)异步加载图片
  5. Android(安卓)studio图片ERROR: 9-patch image xx .9.png malfor
  6. android手机中图片的拖拉及浏览功能
  7. Android(安卓)TextView属性大全
  8. Android(安卓)RelativeLayout布局详解
  9. Android上的Notification

随机推荐

  1. Android 中的WiFi学习笔记
  2. GridLayout 网格布局
  3. Android7.0 MTK方案 静默安装和卸载
  4. android 焦点问题
  5. android用DroidDraw实现可视化UI编程
  6. Android 如何保持横竖屏切换不变以及Frag
  7. Android自适应拉伸图片
  8. Android(安卓): 控制图片如何resized/mov
  9. Android驱动使用JNI调用
  10. Android使用HttpURLConnection和HttpClie