Android uses Gallery to realize the special effects of photo dragging

  • 2021-12-11 08:56:10
  • OfStack

Today, I want to share a very simple function:

Using Android native control Gallery to realize the special effect of photo dragging

The implementation idea is as follows:

Define 1 Gallery control in the layout file Because I want to display multiple pictures, I directly refer to the native picture resources of Android for convenience Gallery is only a control. In order to bind the picture data with the control, a custom adapter that inherits BaseAdapter is needed

The source code is as follows:

1. The main activity and the custom inner class ImageAdapter:


import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import com.example.memorydemo.R;

public class SimpleGallery extends Activity {

  private static final String TAG = "SimpleGallery";

  @Override
  protected void onCreate(Bundle onSavedInstance) {
    super.onCreate(onSavedInstance);
    setContentView(R.layout.simple_gallery_layout);

    Gallery gallery = findViewById(R.id.gallery);
    gallery.setAdapter(new ImageAdapter(this));
  }

  private class ImageAdapter extends BaseAdapter {

    //  Here we use Android Native resource icon 
    private int[] imageIds = {
        android.R.drawable.btn_minus,
        android.R.drawable.btn_radio,
        android.R.drawable.ic_lock_idle_low_battery,
        android.R.drawable.ic_menu_camera };

    private Context mContext;

    public ImageAdapter(Context context) {
      mContext = context;
    }

    @Override
    public int getCount() {
      return imageIds.length;
    }

    @Override
    public Object getItem(int position) {
      return imageIds[position];
    }

    @Override
    public long getItemId(int position) {
      return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
      ImageView imageView;
      if (convertView == null) {
        Log.i(TAG, "convertView is null, create new imageview");
        imageView = new ImageView(mContext);
      } else {
        Log.i(TAG, "Cast convertView to ImageView");
        imageView = (ImageView) convertView;
      }

      imageView.setImageResource(imageIds[position]);
      imageView.setScaleType(ImageView.ScaleType.FIT_XY);
						
		  //  Pay attention to the use here Gallery.LayoutParams As the layout parameter type, the source code gives suggestions ( Views given to the Gallery should use 
			// Gallery.LayoutParams s their ayout parameters type ) 
			//  Due to Android The native image is very small, so I set the height to  500 It is convenient to see the effect 
      imageView.setLayoutParams(new Gallery.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 500));
      return imageView;
    }
  }
}

2. The layout file simple_gallery_layout. xml is as follows:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="match_parent"
       android:layout_height="match_parent">

  <Gallery
      android:id="@+id/gallery"
      android:layout_width="match_parent"
      android:layout_height="match_parent" />

</LinearLayout>

Note:

Gallery control in fact has been discarded, it is recommended to use HorizontalScrollView and ViewPager instead, the source code is as follows:

@deprecated This widget is no longer supported. Other horizontally scrolling widgets include {@link HorizontalScrollView} and {@link android.support.v4.view.ViewPager} from the support library.

How the HorizontalScrollView and ViewPager controls are used will be shared later.

The above is Android using Gallery photo dragging effect details, more about Android photo dragging effect information please pay attention to other related articles on this site!


Related articles: