SoFunction
Updated on 2025-03-11

Android realizes image click to enlarge

This article shares the specific code for Android to click to enlarge the image for your reference. The specific content is as follows

In my project, there is a function of zooming in after clicking on the image banner. My approach is to create a special image display activity, use ViewPage in the layout, so that the image can be controlled to slide left and right, and to control which image to display first.
The function is OK, and the display is normal. But I spent several days implementing and perfecting this function.

ShowMoreImageActivity

/**
  * Picture enlargement
  */
public class ShowMoreImageActivity extends BaseActivity {

 @FindId()
 private ViewPager vp;
 @FindId(.ll_point)
 private LinearLayout ll_point;

 private List<String> imgs;
 @FindId(.btn_save)
 private ImageView btn_save;

 private int index;

 public static int type;
 private Activity activity;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  (savedInstanceState);
  setContentView(.activity_show_more_image);
  initViews();
 activity = this;
 }

 private void initViews() {
  (context);

  imgs = (ArrayList<String>) getIntent().getSerializableExtra("img");
  index = getIntent().getIntExtra("index", 0);

  type = getIntent().getIntExtra("type", 0);
  (new MoreImgPagerAdapter(context, imgs));


  (new OnPageChangeListener() {
   @Override
   public void onPageSelected(int arg0) {
    index = arg0;
    setUpPoint((), arg0);
   }

   @Override
   public void onPageScrolled(int arg0, float arg1, int arg2) {
   }

   @Override
   public void onPageScrollStateChanged(int arg0) {
   }
  });
  setUpPoint((), 0);

  (index);
 }

 protected void downLoad(final String urls) {
  String[] split = ("\\?");
  final String url = split[0];
  if (("file")) {
   (context, "This is a local image, no need to download it, the path is" + ("file://", ""));
   return;
  }

  if ((context)) {
   (context);
   (new Runnable() {
    @Override
    public void run() {
     try {
      File file = new File(());
      if (!()) {
       ();
      }
      File jpg = new File(() + (url));
      // If it already exists, no download is required      if (jpg != null && ()) {
       ();

       (context,
         "The file has been downloaded" + () + ().getString());
       return;
      }
      // Find it from the cache first      File tmpFile = ().getBitmapFileFromDiskCache(url);
      if (tmpFile != null && ()) {
       ("---Find the picture from the cache----");
       Bitmap bm = (());

       FileOutputStream fos = new FileOutputStream(jpg);
       (, 100, fos);
       ();
       ();

       // Notify gallery update       (context, jpg);

       (context, ().getString()
         + () + ().getString());
       return;
      }

      // Download and save from the Internet      Bitmap bm = (new URL(url).openStream());

      FileOutputStream fos = new FileOutputStream(jpg);
      (, 100, fos);
      ();
      ();

      // Notify gallery update      (context, jpg);
      (context, "You can view this image in the gallery now");

     } catch (Exception e) {
      ();
      ();

      (context, ().getString());

      File jpg = new File(() + (url));
      if (jpg != null && ()) {
       ();
      }
     }
    }
   });
  }

 }

 private void setUpPoint(int size, int choose) {
  ll_point.removeAllViews();
  if (size <= 1) {
   return;
  }

  for (int i = 0; i < size; i++) {
   ImageView point = new ImageView(context);
   (new (DensityUtil.dip2px(context, 15), -2));
   (ScaleType.FIT_CENTER);
   if (i == choose) {
    (.white_choosed);
   } else {
    (.white_no_choosed);
   }
   ll_point.addView(point);
  }
 }

 public void doClcik(View view) {
  switch (()){
 case .btn_save:
    PermissionUtils permissionUtils = new PermissionUtils();
    (this, "storage", "Save Picture", new () {
     @Override
     public void doNext() {
      downLoad((index));
     }
    },.WRITE_EXTERNAL_STORAGE);
 break;
 }
 }
}

Corresponding layout:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:andro
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="@android:color/black"
 android:orientation="vertical">

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

 <LinearLayout
  android:layout_width="match_parent"
  android:layout_height="40dp"
  android:layout_marginTop="25dp">

  <LinearLayout
   android:layout_width="50dp"
   android:layout_height="match_parent"
   android:onClick="onFinish">

   <ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:layout_marginLeft="10dp"
    android:background="@drawable/nav_back" />

  </LinearLayout>

  <View
   android:layout_width="0dp"
   android:layout_height="0dp"
   android:layout_weight="1" />

  <ImageView
   android:
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_gravity="center_vertical"
   android:layout_marginRight="10dp"
   android:onClick="doClcik"
   android:src="@drawable/download_img" />
 </LinearLayout>

 <LinearLayout
  android:
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:layout_gravity="bottom"
  android:layout_marginBottom="40dp"
  android:gravity="center"
  android:orientation="horizontal"/>

</FrameLayout>

MoreImgPagerAdapter

public class MoreImgPagerAdapter extends PagerAdapter {

 private Context context;
 private List<String> images;

 private SparseArray<SoftReference<View>> ivs;

 public MoreImgPagerAdapter(Context context, List<String> images) {
  = context;
  = images;
 ivs = new SparseArray<SoftReference<View>>();
 }

 @Override
 public int getCount() {
 return ();
 }

 @Override
 public void destroyItem(ViewGroup arg0, int arg1, Object arg2) {
 SoftReference<View> reference = (arg1);
 if (reference != null && () != null) {
 (());
 }
 }

 @Override
 public Object instantiateItem(ViewGroup arg0, final int arg1) {
 SoftReference<View> reference = (arg1);
 if (reference == null || () == null) {
 View v = (context).inflate(.item_show_more_image, null);
 reference = new SoftReference<View>(v);
 (arg1, reference);
 }

 View v = ();

 final ViewHolder holder = new ViewHolder(v);
 (context).asBitmap().load((arg1)).into();
 (v);

 return v;

 }



 @Override
 public boolean isViewFromObject(View arg0, Object arg1) {
 return (arg1);
 }

 class ViewHolder {
 @FindId()
 private ImageView image;

 @FindId(.rl_percent)
 private RelativeLayout rl_percent;
 @FindId(.tv_percent)
 private TextView tv_percent;
 @FindId(.iv_top)
 private ImageView iv_top;
 
 public ViewHolder(View v) {
 (this, v);
 }
 }
}

Corresponding layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:andro
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="@android:color/black"
 android:orientation="vertical" >

 <FrameLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:layout_marginBottom="50dp"
  android:layout_marginTop="70dp" >

  <ImageView
   android:layout_gravity="center"
   android:
   android:layout_width="match_parent"
   android:layout_height="match_parent" />

  <ImageView
   android:
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_gravity="top|right"
   android:visibility="gone"
   android:background="@drawable/shuiyin" />
 </FrameLayout>

 <RelativeLayout
  android:visibility="gone"
  android:
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_centerInParent="true" >

  <ProgressBar
   android:layout_width="40dp"
   android:layout_height="40dp" />

  <TextView
   android:
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_centerInParent="true"
   android:textColor="@android:color/white"
   android:textSize="12sp" />
 </RelativeLayout>

</RelativeLayout>

All above are secondary, because I found a simpler wheel.

github address

In my project, I only need two steps to complete this function.

first step:

// View larger imageimplementation ':BigImageViewPager:v4_6.1.1'

Step 2:

Called in the click image event:

ImagePreview
  .getInstance()
  // The context must be an activity, and there is no need to worry about memory leaks. This framework has been handled;  .setContext(context)
  // Set the number of pictures to view (index starts from 0)  .setIndex(position)
  // There are three ways to set data sets, and choose one of them according to your needs:  // 1: The imageInfo List generated in the first step  //.setImageInfoList(imageInfoList)
  // 2: Directly pass url List  .setImageList(imageList)
  // 3: If there is only one picture, you can directly pass the URL of this picture  //.setImage(String image)
  // Turn on preview .start();

In this way, the functions of enlarging and browsing and downloading of pictures are completed, and recorded here.

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.