Android to achieve long picture loading effect

A key class bitmapregiondoder is used for long graph loading. Bitmap memory reuse is used for long graph loading. For example, the view size is 440 * 654 and the width and height of the picture is 440 * 12000. At this time, the width and height of the picture should be obtained. Compared with the width and height of the view, a reduction ratio will be obtained. Then, a width ratio and a height ratio will be obtained, Use the large scale as the zoom factor, and then slide the long graph with the gesture

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapRegionDecoder;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Scroller;

import java.io.IOException;
import java.io.InputStream;

public class BigView extends View implements GestureDetector.OnGestureListener,View.OnTouchListener {
  private static final String TAG = "BigView";
  private Scroller mScroller;
  private GestureDetector mGestureDetector;
  private BitmapFactory.Options mOptions;
  private Rect mRect;
  private int mImageWidth;
  private int mImageHeight;
  private BitmapRegionDecoder mDecoder;
  private int mViewWidth;
  private int mViewHeight;
  private float mScale;
  private Bitmap bitmap;

  public BigView(Context context) {
    this(context,null,0);
  }

  public BigView(Context context,@Nullable AttributeSet attrs) {
    this(context,attrs,@Nullable AttributeSet attrs,int defStyleAttr) {
    super(context,defStyleAttr);
    //指定要加载的矩形区域
    mRect = new Rect();
    //解码图片的配置
    mOptions = new BitmapFactory.Options();
    //手势
    mGestureDetector = new GestureDetector(context,this);
    setOnTouchListener(this);
    // 滑动帮助
    mScroller = new Scroller(context);
  }

  /**
   * 由使用者输入一张图片 输入流
   *
   * @param is
   */
  public void setImage(InputStream is) {
    //先读取原图片的 宽、高
    mOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeStream(is,mOptions);
    mImageWidth = mOptions.outWidth;
    mImageHeight = mOptions.outHeight;
    //复用 内存复用
    mOptions.inMutable = true;
    //设置像素格式为 rgb565
    mOptions.inPreferredConfig = Bitmap.Config.RGB_565;
    mOptions.inJustDecodeBounds = false;
    //创建区域解码器 用于区域解码图片
    try {
      mDecoder = BitmapRegionDecoder.newInstance(is,false);
    } catch (IOException e) {
      e.printStackTrace();
    }
    requestLayout();
  }

  /**
   * 测量 view的大小
   *
   * @param widthMeasureSpec
   * @param heightMeasureSpec
   */
  @Override
  protected void onMeasure(int widthMeasureSpec,int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec,heightMeasureSpec);
    //获得测量的view的大小
    mViewWidth = getMeasuredWidth();
    mViewHeight = getMeasuredHeight();
    //如果解码器是null 表示没有设置过要现实的图片
    if (null == mDecoder) {
      return;
    }
    //确定要加载的图片的区域
    mRect.left = 0;
    mRect.top = 0;
    mRect.right = mImageWidth;
//    Log.e(TAG,"缩放因子="+(mViewWidth*1.0f/mImageWidth*1.0f));
//    Log.e(TAG,"缩放因子="+(mViewHeight*1.0f/mImageHeight*1.0f));
    //获得缩放因子
    mScale = mViewWidth / (float) mImageWidth;

    // 需要加载的高 * 缩放因子 = 视图view的高
    // x * mScale = mViewHeight
    mRect.bottom = (int) (mViewHeight / mScale);
    Log.e(TAG,"l="+mRect.left);
    Log.e(TAG,"t="+mRect.top);
    Log.e(TAG,"r="+mRect.right);
    Log.e(TAG,"b="+mRect.bottom);
  }

  /**
   * 把图片画上去
   *
   * @param canvas
   */
  @Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    // 如果解码器是null 表示没有设置过要现实的图片
    if (null == mDecoder) {
      return;
    }
    //复用上一张bitmap
    Log.e(TAG,"复用上一张bitmap="+bitmap);
    mOptions.inBitmap = bitmap;
    //解码指定区域
    bitmap = mDecoder.decodeRegion(mRect,mOptions);
    //使用矩阵 对图片进行 缩放
    Matrix matrix = new Matrix();
    matrix.setScale(mScale,mScale);
    //画出来

    canvas.drawBitmap(bitmap,matrix,null);
  }

  /**
   * 手指按下屏幕的回调
   * @param e
   * @return
   */
  @Override
  public boolean onDown(MotionEvent e) {
    //如果滑动还没有停止 强制停止
    if (!mScroller.isFinished()){
      mScroller.forceFinished(true);
    }
    //继续接收后续事件
    return true;
  }

  @Override
  public void onShowPress(MotionEvent e) {

  }

  @Override
  public boolean onSingleTapUp(MotionEvent e) {
    return false;
  }

  @Override
  public void onLongPress(MotionEvent e) {

  }

  /**
   * 手指 不离开屏幕 拖动
   * @param e1 手指按下去 的事件 -- 获取开始的坐标
   * @param e2 当前手势事件 -- 获取当前的坐标
   * @param distanceX x轴 方向移动的距离
   * @param distanceY y方向移动的距离
   * @return
   */
  @Override
  public boolean onScroll(MotionEvent e1,MotionEvent e2,float distanceX,float distanceY) {
    // 手指从下往上 图片也要往上 distanceY是负数,top 和 bottom 在减
    // 手指从上往下 图片也要往下 distanceY是正数,top 和 bottom 在加
    //改变加载图片的区域
    mRect.offset(0,(int) distanceY);
    //bottom大于图片高了, 或者 top小于0了
    if (mRect.bottom > mImageHeight){
      mRect.bottom = mImageHeight;
      mRect.top = mImageHeight-(int) (mViewHeight / mScale);
    }
    if (mRect.top < 0){
      mRect.top = 0;
      mRect.bottom = (int) (mViewHeight / mScale);
    }
    //重绘
    invalidate();
    return false;
  }

  /**
   * 手指离开屏幕 滑动 惯性
   * @param e1
   * @param e2
   * @param veLocityX 速度 每秒x方向 移动的像素
   * @param veLocityY y
   * @return
   */
  @Override
  public boolean onFling(MotionEvent e1,float veLocityX,float veLocityY) {
    /**
     * startX: 滑动开始的x坐标
     * startY: 滑动开始的y坐标
     * 两个速度
     * minX: x方向的最小值
     * max 最大
     * y
     */
    //计算器
    mScroller.fling(0,mRect.top,(int)-veLocityY,mImageHeight - (int) (mViewHeight / mScale));
    return false;
  }

  //获取计算结果并且重绘
  @Override
  public void computeScroll() {
    //已经计算结束 return
    if (mScroller.isFinished()){
      return;
    }
    //true 表示当前动画未结束
    if (mScroller.computeScrollOffset()){
      //
      mRect.top = mScroller.getCurrY();
      mRect.bottom = mRect.top+ (int) (mViewHeight / mScale);
      invalidate();
    }
  }

  @Override
  public boolean onTouch(View v,MotionEvent event) {
    //交由手势处理
    return mGestureDetector.onTouchEvent(event);
  }
}

If it is the second key point of the interview, the first one is to say this class, and the second one is to know that memory reuse is used

The above is the whole content of this article. I hope it will help you in your study, and I hope you will support us a lot.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>