SoFunction
Updated on 2025-03-11

Android uses Photoview to realize the left and right sliding and zooming of pictures

I think no matter what kind of app you make. Generally, it involves the method of clicking pictures and the function of sliding left and right. Today I will share my implementation. Picture loading uses the picture loading tool of picasso.

first step:Inject dependency

compile ':picasso:2.5.2'
compile ':PhotoView:1.3.0'

Step 2,Let's implement this function. Multiple images slide left and right, which requires the ViewPager class.
Let's look at the layout:

<
 xmlns:andro
 android:
 android:layout_width="match_parent"
 android:layout_height="match_parent"/>

Custom ViewPager is used here

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 (ev);
 } catch (IllegalArgumentException e) {
  ();
  return false;
 }
 }
}

This is the activity that jumps to the large image after clicking to display the large image. The most important and simple step:

package ;

import ;
import .;
import .;
import .;
import ;
import ;
import ;

import ;
import ;

import ;
import ;

public class PicassoSampleActivity extends AppCompatActivity {

 ViewPager mViewPager;
 //String position;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 (savedInstanceState);
 setContentView(.activity_simple);
 mViewPager = (HackyViewPager) findViewById(.iv_photo);
 setContentView(mViewPager);
 (new SamplePagerAdapter());
 //position=getIntent().getStringExtra("position");

 //Set the default coordinates. Don’t write the actual situation to death. Just click on the position of which image and pass it over to accept it. The large image of the clicked image will be displayed by default. //((position));
 (2);
 }

 static class SamplePagerAdapter extends PagerAdapter {
 //This is temporarily written to death. In actual situation, you need to obtain the image address from the server and send it to you private static final String[] url = {"/media/", "/album/whcrop%3D657%2C370%3Bq%3D90/sign=2c994e578a82b9013df895711cfd9441/",
  "/d/file/2013-11-14/", "/f5/63/d/"};

 @Override
 public int getCount() {
  return ;
 }

 @Override
 public View instantiateItem(ViewGroup container, int position) {
  PhotoView photoView = new PhotoView(());
  final PhotoViewAttacher attacher = new PhotoViewAttacher(photoView);
  (())
   .load(url[position])
   .into(photoView, new Callback() {
   @Override
   public void onSuccess() {
    ();
   }

   @Override
   public void onError() {

   }
   });

  (photoView, .MATCH_PARENT, .MATCH_PARENT);

  return photoView;
 }

 @Override
 public void destroyItem(ViewGroup container, int position, Object object) {
  ((View) object);
 }

 @Override
 public boolean isViewFromObject(View view, Object object) {
  return view == object;
 }

 }
}

With this tool, we can easily realize the functions of clicking and zooming in pictures, activities on the left and right, and zooming.