android随笔之大图加载

时间:2021-7-21 作者:qvyue

多的不说,少的不唠,上图:

android随笔之大图加载
1603334406103 (1).gif

实现思路:
局部加载,触摸拖动,双击缩放
开搞:
一些初始化

        mRect = new Rect();
        mOptions = new BitmapFactory.Options();
        //手势
        mGestureDetector = new GestureDetector(context, this);
        //滚动
        mScroller = new Scroller(context);
        // 添加缩放手势识别
        mScaleGestureDetector = new ScaleGestureDetector(context, new ScaleGesture());
        //touch
        setOnTouchListener(this);

设置图片资源的方法

    /**
     * @param
     * @return
     * @description 圖片拆分最終還是需要轉化成流,所以直接传inputstream
     * @author rtrs-renshiwu
     * @time 2020/10/15 10:46
     */
    public void setImage(InputStream is) {
        mOptions.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(is, null, mOptions);
        mImageWidth = mOptions.outWidth;
        mImageHeight = mOptions.outHeight;
        mOptions.inJustDecodeBounds = false;
        // 开启复用
        mOptions.inMutable = true;
        // 设置图片格式
        mOptions.inPreferredConfig = Bitmap.Config.RGB_565;
        // 图片由像素点组成;像素点由什么组成;argb 透明通道 红色 绿色 蓝色;
        // 创建区域解码器
        try {
            mDecoder = BitmapRegionDecoder.newInstance(is, false);
        } catch (IOException e) {
            e.printStackTrace();
        }
        requestLayout();
    }

算了算了,代码比较简单,就不多BB了(主要是今天有点懒,懒得写了(๑→ܫ←)),自己参考注释看下就明白了,相信你能看得懂,真的不懂的麻烦留言,我会一一解答

上完整代码:

public class RenBigImageView extends View implements View.OnTouchListener, GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener {
    private BitmapFactory.Options mOptions;
    private Rect mRect;
    private GestureDetector mGestureDetector;
    private Scroller mScroller;
    private ScaleGestureDetector mScaleGestureDetector;
    private int mImageWidth;
    private int mImageHeight;
    private BitmapRegionDecoder mDecoder;

    private int mViewWidth;
    private int mViewHeight;
    //缩放因子
    private float mScale;

    private Bitmap mBitmap;
    private Matrix matrix = new Matrix();

    private float originalScale;

    private float tempScale;

    public RenBigImageView(Context context) {
        this(context, null);
    }

    public RenBigImageView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RenBigImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mRect = new Rect();
        mOptions = new BitmapFactory.Options();
        //手势
        mGestureDetector = new GestureDetector(context, this);
        //滚动
        mScroller = new Scroller(context);
        // 添加缩放手势识别
        mScaleGestureDetector = new ScaleGestureDetector(context, new ScaleGesture());
        //touch
        setOnTouchListener(this);
    }

    /**
     * @param
     * @return
     * @description 圖片拆分最終還是需要轉化成流,所以直接传inputstream
     * @author rtrs-renshiwu
     * @time 2020/10/15 10:46
     */
    public void setImage(InputStream is) {
        mOptions.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(is, null, mOptions);
        mImageWidth = mOptions.outWidth;
        mImageHeight = mOptions.outHeight;
        mOptions.inJustDecodeBounds = false;
        // 开启复用
        mOptions.inMutable = true;
        // 设置图片格式
        mOptions.inPreferredConfig = Bitmap.Config.RGB_565;
        // 图片由像素点组成;像素点由什么组成;argb 透明通道 红色 绿色 蓝色;
        // 创建区域解码器
        try {
            mDecoder = BitmapRegionDecoder.newInstance(is, false);
        } catch (IOException e) {
            e.printStackTrace();
        }
        requestLayout();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mViewWidth = getMeasuredWidth();
        mViewHeight = getMeasuredHeight();
        mScale = mViewWidth / (float) mImageWidth;
        //圖片的加載區域
//        mRect.left = 0;
//        mRect.top = 0;
//        mRect.right = mImageWidth;
//        mRect.bottom = (int) (mViewHeight / mScale);

        // 加入缩放因子之后的逻辑
        mRect.left = 0;
        mRect.top = 0;
        mRect.right = Math.min(mImageWidth, mViewWidth);
        Log.e("xxxxxxx", mImageWidth + "  " + mViewWidth);
        mRect.bottom = Math.min(mImageHeight, mViewHeight);
        // 再定义一个缩放因子
        originalScale = mViewWidth / (float) mImageWidth;
        mScale = originalScale;
//        //縮放圖片到全屏可显示
//        matrix.setScale(mScale, mScale);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
//        if (mDecoder == null) {
//            return;
//        }
//        //内存復用
//        mOptions.inBitmap = mBitmap;
//        mBitmap = mDecoder.decodeRegion(mRect, mOptions);
//        canvas.drawBitmap(mBitmap, matrix, null);

        if (mDecoder == null) {
            return;
        }
        // 复用内存
        mOptions.inBitmap = mBitmap;
        mBitmap = mDecoder.decodeRegion(mRect, mOptions);
        // 缩放因子之前是固定的,现在不是;随时改变;
        tempScale = mViewWidth / (float) mRect.width();
        matrix.setScale(tempScale, tempScale);
        canvas.drawBitmap(mBitmap, matrix, null);
    }

    //将touch事件交给手势
    @Override
    public boolean onTouch(View view, MotionEvent event) {
        mGestureDetector.onTouchEvent(event);
        mScaleGestureDetector.onTouchEvent(event);
        return true;
    }

    @Override
    public boolean onDown(MotionEvent event) {
        if (!mScroller.isFinished()) {
            mScroller.forceFinished(true);
        }
        return true;
    }

    @Override
    public boolean onScroll(MotionEvent event, MotionEvent event1, float distanceX, float distanceY) {
//        mRect.offset(0, (int) v1);
//        if (mRect.bottom > mImageHeight) {
//            mRect.bottom = mImageHeight;
//            mRect.top = (int) (mImageHeight - mViewHeight / mScale);
//        }
//        if (mRect.top  mImageHeight) {
            mRect.bottom = mImageHeight;
            mRect.top = mImageHeight - (int) (mViewHeight / mScale);
        }
        if (mRect.top  mImageWidth) {
            mRect.right = mImageWidth;
            mRect.left = mImageWidth - (int) (mViewWidth / mScale);
        }
        if (mRect.left  mImageHeight) {
            mRect.bottom = mImageHeight;
            mRect.top = mImageHeight - (int) (mViewHeight / mScale);
        }
        if (mRect.top  mImageWidth) {
            mRect.right = mImageWidth;
            mRect.left = mImageWidth - (int) (mViewWidth / mScale);
        }
        if (mRect.left  originalScale * 2) {
                scale = originalScale * 2;
            }
            mRect.right = mRect.left + (int) (mViewWidth / scale);
            mRect.bottom = mRect.top + (int) (mViewHeight / scale);
            mScale = scale;
            invalidate();
            return super.onScale(detector);
        }
    }
}
使用:
        RenBigImageView renBigImageView = findViewById(R.id.renBigImageView);
        try {
            InputStream is = getAssets().open("aaa.jpg");
            renBigImageView.setImage(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
完事,结束!
声明:本文内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:qvyue@qq.com 进行举报,并提供相关证据,工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。