关于这个问题 本人也是困惑已久 终于得到实现办法 通过在Viewpager中添加点击事件 这里就是图片的缩放功能 通过list<image> 定义imageadapeter 来继承pageradapter
本文链接来自边强http://write.blog.csdn.net/postedit?ref=toolbar
还要运用到library.jar包 包的下载地址是http://download.csdn.net/detail/siliyu/7038547
下面开始写代码
<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="match_parent" android:background="#E3E2E2" tools:context=".MainActivity" > <com.elife.tools.MyViewPager android:id="@+id/my_pager" android:layout_width="match_parent" android:layout_below="@+id/voice_heade" android:layout_height="match_parent" /> </RelativeLayout>
package com.elife.tools; import android.content.Context; import android.graphics.Bitmap; import uk.co.senab.photoview.PhotoView; public class MyPhotoView extends PhotoView{ public MyPhotoView(Context context) { super(context); } /** * 初始化ImageView图片 * @param bitmap */ public void setImage(Bitmap bitmap) { super.setImageBitmap(bitmap); if (null != mAttacher) { mAttacher.update(); } } }
package com.elife.tools; import android.content.Context; import android.support.v4.view.ViewPager; import android.util.AttributeSet; import android.view.MotionEvent; public class MyViewPager extends ViewPager { public MyViewPager(Context context) { super(context); // TODO Auto-generated constructor stub } //这个构造方法必须有要不然xml中无法加载 会报错 public MyViewPager(Context context, AttributeSet attrs) { super(context, attrs); } //这个方法是为了避免手势滑动的时候产生异常 @Override public boolean onInterceptTouchEvent(MotionEvent ev) { try { return super.onInterceptTouchEvent(ev); } catch (IllegalArgumentException e) { e.printStackTrace(); return false; } } }
package com.elife.property; import java.util.ArrayList; import java.util.List; import com.elife.app.R; import com.elife.tools.Image; import com.elife.tools.MyPhotoView; import com.elife.tools.MyViewPager; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.drawable.BitmapDrawable; import android.os.Bundle; import android.support.v4.view.PagerAdapter; import android.view.View; import android.view.ViewGroup; import android.view.ViewGroup.LayoutParams; public class Property_Viewpager extends Activity { private MyViewPager pager; private List<Image> images; private ImageAdapter adapter; private static int[] sDrawables = {R.drawable.demo, R.drawable.demo, R.drawable.demo, R.drawable.demo}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.view_pager); pager = (MyViewPager) findViewById(R.id.my_pager); System.out.println("pager:"+pager); images = new ArrayList<Image>(); init(); pager.setAdapter(new ImageAdapter(images)); } private void init(){ for(int i = 0; i < sDrawables.length; i++){ BitmapDrawable bd = (BitmapDrawable) getResources().getDrawable(sDrawables[i]); Bitmap bm = bd.getBitmap(); Image image = new Image(); image.setBitmap(bm); images.add(image); } } public class ImageAdapter extends PagerAdapter{ private List<Image> list; public ImageAdapter(List<Image> list){ this.list = list; } @Override public View instantiateItem(ViewGroup container, int position) { Image image = list.get(position); MyPhotoView photoView = null; if(null != image){ System.out.println("context:"+container.getContext()); photoView = new MyPhotoView(container.getContext()); photoView.setImage(image.getBitmap()); } container.addView(photoView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); return photoView; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View) object); } @Override public boolean isViewFromObject(View view, Object object) { return view == object; } @Override public int getCount() { return images.size(); } } }