Page carousel jump to large image carousel, carousel jump to large image
Page carousel jump to large image carousel
Public class ImageScaleActivity extends AppCompatActivity {private ArrayList
ImageUrls; private int position; private ViewPager iamge_scale_pager; private TextView image_scale_text; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_image_scale); iamge_scale_pager = findViewById (R. id. iamge_scale_pager); image_scale_text = findViewById (R. id. image_scale_text); // receives data Intent intent = getIntent (); ImageUrls = intent. getStringArrayListExtra ("imageUrls"); position = intent. getIntExtra ("position",-1); // judge whether the set is not null. The length of the null set is greater than 0. position is not-1. // set the adapter if (imageUrls! = Null & imageUrls. size ()> 0 & position! =-1) {// 1. textView: Specifies the image image_scale_text.setText (position + 1) + "/" + imageUrls. size (); // 2. set the adapter ImageScaleAdapter imageScaleAdapter = new ImageScaleAdapter (); iamge_scale_pager.setAdapter (imageScaleAdapter); // 3. set the image to be clicked .. setCurrentItem () // boolean smoothScroll... whether to scroll iamge_scale_pager.setCurrentItem (position, false); // set the listener iamge_scale_pager.addOnPageChangeListener (new ViewPager. onPageChangeListener () {@ Override public void onPageScrolled (int position, float positionOffset, int positionOffsetPixels) {}@ Override public void onPageSelected (int position) {// set the text display image_scale_text.setText (position + 1) + "/" + imageUrls. size () ;}@ Override public void onPageScrollStateChanged (int state) {}}) ;}private class ImageScaleAdapter extends PagerAdapter {@ Override public int getCount () {return imageUrls. size () ;}@ Override public boolean isViewFromObject (@ NonNull View view, @ NonNull Object object) {return view = object ;} @ NonNull @ Override public Object instantiateItem (@ NonNull ViewGroup container, int position) {// create an imageView (iamgeView that can be zoomed in and out... custom) ZoomImageView zoomImageView = new ZoomImageView (ImageScaleActivity. this); // use glide to load the image Glide. with (ImageScaleActivity. this ). load (imageUrls. get (position )). into (zoomImageView); // Add to container. addView (zoomImageView); // return this view return zoomImageView;} @ Override public void destroyItem (@ NonNull ViewGroup container, int position, @ NonNull Object object) {// remove this view container from the container. removeView (View) object) ;}} this is the layout
You need to customize the View/*** Created by Dash on 2018/1/25. * // @ SuppressLint ("AppCompatCustomView") public class ZoomImageView extends ImageView implements ViewTreeObserver. onGlobalLayoutListener, ScaleGestureDetector. onScaleGestureListener, View. onTouchListener {private boolean mOnce;/*** the scaled value at initialization */private float mInitScale;/*** double-click the enlarged value */private float mMidScale; /* Maximum amplification */private float mMaxScale; private Matrix mScaleMatrix;/* capture the scaling ratio of the user's multi-finger touch */private ScaleGestureDetector mScaleGestureDetector; // ***********************/***** record the number of last multi-touch */ private int mLastPointerCount; private float mLastX; private float mLastY; private int mTouchSlop; private boolean isCanDrag; private boolean isCheckLeftAndRight; private boolean isCheckTopAndBottom; // ********* double-click to zoom in and out ********* private GestureDetector mGestureDetector; private boolean isAutoScale; public ZoomImageView (Context context) {this (context, null);} public ZoomImageView (Context context, AttributeSet attrs) {this (context, attrs, 0);} public ZoomImageView (Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle); // init mScaleMatrix = new Matrix (); setScaleType (ScaleType. MATRIX); setOnTouchListener (this); mTouchSlop = ViewConfiguration. get (context ). getScaledTouchSlop (); mScaleGestureDetector = new ScaleGestureDetector (context, this); mGestureDetector = new GestureDetector (context, new GestureDetector. simpleOnGestureListener () {@ Override public boolean onDoubleTap (MotionEvent e) {if (isAutoScale) {return true;} float x = e. getX (); float y = e. getY (); if (getScale () <mMidScale) {postDelayed (new AutoScaleRunnable (mMidScale, x, y), 16); isAutoScale = true ;} else {postDelayed (new AutoScaleRunnable (mInitScale, x, y), 16); isAutoScale = true ;}return true ;}});} /*** auto zoom in and out ** @ author zhangyan@lzt.com.cn **/private class AutoScaleRunnable implements Runnable {/*** scale the target value */private float mTargetScale; // The scaled center point private float x; private float y; private final float BIGGER = 1.07f; private final float SMALL = 0.93f; private float tmpScale; /*** @ param mTargetScale * @ param x * @ param y */public AutoScaleRunnable (float mTargetScale, float x, float y) {this. mTargetScale = mTargetScale; this. x = x; this. y = y; if (getScale () <mTargetScale) {tmpScale = BIGGER;} if (getScale ()> mTargetScale) {tmpScale = SMALL ;}@ Override public void run () {// scale the mScaleMatrix. postScale (tmpScale, tmpScale, x, y); checkBorderAndCenterWhenScale (); setImageMatrix (mScaleMatrix); float currentScale = getScale (); if (tmpScale> 1.0f & currentScale
MTargetScale) {// this method is to re-call the run () method postDelayed (this, 16);} else {// set to our target value float scale = mTargetScale/currentScale; mScaleMatrix. postScale (scale, scale, x, y); checkBorderAndCenterWhenScale (); setImageMatrix (mScaleMatrix); isAutoScale = false ;}}} /*** get the image loaded by ImageView */@ Override public void onGlobalLayout () {if (! MOnce) {// obtain the width and height of the Control. int width = getWidth (); int height = getHeight (); // obtain our image, and the width and height of Drawable drawable = getDrawable (); if (drawable = null) {return;} int dh = drawable. getIntrinsicHeight (); int dw = drawable. getIntrinsicWidth (); float scale = 0.6f; // The image width is greater than the control width and the Image Height is smaller than the space height, we can narrow it down if (dw> width & dh