Photoview实现图片左右滑动及缩放功能 Android使用Photoview实现图片左右滑动及缩放功能
kuaizilanqiu 人气:0想了解Android使用Photoview实现图片左右滑动及缩放功能的相关内容吗,kuaizilanqiu在本文为您仔细讲解Photoview实现图片左右滑动及缩放功能的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Photoview图片左右滑动,Photoview图片缩放,Android图片左右滑动,下面大家一起来学习吧。
我想,不管是做什么样的app。一般都会涉及到图片点击方法,和左右滑动的功能吧。今天我就把自己的实现拿来分享一下。图片的加载用到了picasso的图片加载工具。
第一步:注入依赖
compile 'com.squareup.picasso:picasso:2.5.2' compile 'com.github.chrisbanes:PhotoView:1.3.0'
第二步,我们就来实现这个功能。多张图片左右滑动,这需要用到ViewPager这个类。
且看布局:
<uk.co.senab.photoview.sample.HackyViewPager xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/iv_photo" android:layout_width="match_parent" android:layout_height="match_parent"/>
这里用到了自定义的ViewPager
public class HackyViewPager extends ViewPager { public HackyViewPager(Context context) { super(context); } public HackyViewPager(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; } } }
这个是点击之后跳转到大图的Activity,来显示大图。最重要也是很简单的一步:
package uk.co.senab.photoview.sample; import android.os.Bundle; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.view.ViewGroup; import com.squareup.picasso.Callback; import com.squareup.picasso.Picasso; import uk.co.senab.photoview.PhotoView; import uk.co.senab.photoview.PhotoViewAttacher; public class PicassoSampleActivity extends AppCompatActivity { ViewPager mViewPager; //String position; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_simple); mViewPager = (HackyViewPager) findViewById(R.id.iv_photo); setContentView(mViewPager); mViewPager.setAdapter(new SamplePagerAdapter()); //position=getIntent().getStringExtra("position"); //设置默认的坐标,实际情况不要写死,只需点击哪张图片的position,并传过来接受就可以了,就默认显示点击图片的大图。 //mViewPager.setCurrentItem(Integer.parseInt(position)); mViewPager.setCurrentItem(2); } static class SamplePagerAdapter extends PagerAdapter { //这里暂时写死了,实际情况中要从服务端获取图片地址结合,传过来 private static final String[] url = {"/media/Bist9mvIYAAeAyQ.jpg", "/album/whcrop%3D657%2C370%3Bq%3D90/sign=2c994e578a82b9013df895711cfd9441/09fa513d269759eede0805bbb2fb43166d22df62.jpg", "/d/file/2013-11-14/13631d143b7076dc3257b1e516a2a9a2.jpg", "/f5/63/d/7.jpg"}; @Override public int getCount() { return url.length; } @Override public View instantiateItem(ViewGroup container, int position) { PhotoView photoView = new PhotoView(container.getContext()); final PhotoViewAttacher attacher = new PhotoViewAttacher(photoView); Picasso.with(container.getContext()) .load(url[position]) .into(photoView, new Callback() { @Override public void onSuccess() { attacher.update(); } @Override public void onError() { } }); container.addView(photoView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.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; } } }
有了这个工具我们很轻松就实现了图片点击放大,左右活动,缩放等功能。
加载全部内容