This article shares the specific code for Android loading long pictures for your reference. The specific content is as follows
Solution steps
1. Scale the image to the same width as the control
2. Determine the height of the zoomed image. If the height is greater than the height of the control (the setting here is 1.5 times), it is considered a long image and can be viewed by sliding it.
|-If the height is less than 1.5 times the height of the control, rescaling the picture based on the height of the control
package org.wandcf_ces.; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; /** * Created by sunrui on 2017/3/8. * Loading long pictures * Solution steps * 1. Scale the image to the same width as the control * 2. Determine the height of the zoomed image. If the height is greater than the height of the control (the setting here is 1.5 times), it is considered a long image and can be viewed by sliding the image. * |-If the height is less than 1.5 times the height of the control, rescaling the image based on the height of the control * */ public class LongImageView extends View { private int width, height; //Bitmap that needs to be drawn private Bitmap bitmap; /** * Area of the picture to be drawn */ private Rect srcRect; /** * The drawn area */ private RectF dstRectF; /** * Paintbrush */ private Paint paint; /** * Is it necessary to slide */ private boolean isNeedSlide; /** * Distance that has been sliding */ private float slideLength; /** * Drawn Bitmap */ private Bitmap drawBitmap; { srcRect = new Rect(); dstRectF = new RectF(); paint = new Paint(); (true); (); (1.0f); } public LongImageView(Context context) { super(context); } public LongImageView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); } public LongImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @TargetApi(Build.VERSION_CODES.LOLLIPOP) public LongImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); } /** * Setting up Bitmap * * @param bitmap * Bitmap to be drawn */ public void setBitmap(Bitmap bitmap) { = bitmap; } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int specSize = (widthMeasureSpec); width = getPaddingLeft() + getPaddingRight() + specSize; specSize = (heightMeasureSpec); height = getPaddingTop() + getPaddingBottom() + specSize; if (drawBitmap == null) { drawBitmap = resizeImage(bitmap, width); if (() > 1.5 * height) { //Swipe required setNeedSlide(true); } else { //No need to slide setNeedSlide(false); = 0; = 0; = (); = (); if (() > height) { drawBitmap = resizeImageH(drawBitmap, height - 20); } else { float space = (height - ()); = 0; = space; = width; = height - space; } } } setMeasuredDimension(width, height); } @Override protected void onDraw(Canvas canvas) { (drawBitmap, (width - ()) / 2, slideLength, paint); } /** * Set whether sliding is required * * @param needSlide * true or false */ public void setNeedSlide(boolean needSlide) { isNeedSlide = needSlide; } /** * Coordinates for touch operation */ private float lastX; private float lastY; @Override public boolean onTouchEvent(MotionEvent event) { if (!isNeedSlide) { return (event); } int action = (); switch (action) { case MotionEvent.ACTION_DOWN: //Press lastX = (); lastY = (); break; case MotionEvent.ACTION_MOVE: float moveX = (); if (moveX - lastX > 50) { //It is determined to be sliding left and right return (event); } float moveY = (); float distance = moveY - lastY; lastY = moveY; slideLength += distance; if (slideLength >= 0) { slideLength = 0; } if (slideLength <= (-1) * (() - height)) { slideLength = (-1) * (() - height); } postInvalidate(); break; default: break; } return true; } public Bitmap resizeImage(Bitmap bitmap, int w) { int width = (); int height = (); float scaleWidth = ((float) w) / width; Matrix matrix = new Matrix(); (scaleWidth, scaleWidth); return (bitmap, 0, 0, width, height, matrix, true); } public Bitmap resizeImageH(Bitmap bitmap, int h) { int width = (); int height = (); float scaleWidth = ((float) h) / height; Matrix matrix = new Matrix(); (scaleWidth, scaleWidth); return (bitmap, 0, 0, width, height, matrix, true); } }
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.