@TryLoveCatch
2022-05-05T08:40:56.000000Z
字数 1323
阅读 2136
Android知识体系
需要显示一个横向长图,可以左右滑动显示图片其他部分
BitmapRegionDecoder这个可以用来解决超大图片显示OOM的问题,decodeRegion(Rect rect, BitmapFactory.Options options)这个方法可以选择加载图片的某一区域,根据rect范围来裁剪图片,并加载进来,利用这个方法可以将一张图片切割成很多小图。
public void moveBy(int pMoveX, int pMoveY){mRect.offset(pMoveX, pMoveY);checkHeight();checkWidth();invalidate();}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);Bitmap tBmp = mDecoder.decodeRegion(mRect, mOptions);Log.e("largeimagedemo", "===========" + mRect.left + ", " + mRect.right);canvas.drawBitmap(tBmp, 0, 0, null);}
传入需要移动的距离,动态调整rect的值,然后刷新当前View,在onDraw()里面调用BitmapRegionDecoder.decodeRegion截取需要显示的图片区域。
mLargeImageView.setOnTouchListener(new View.OnTouchListener() {int mLastX;int mLastY;@Overridepublic boolean onTouch(View v, MotionEvent event) {switch (event.getAction()){case MotionEvent.ACTION_DOWN:mLastX = (int)event.getX();mLastY = (int)event.getY();break;case MotionEvent.ACTION_MOVE:int tMoveX = mLastX - (int)event.getX();int tMoveY = mLastY - (int)event.getY();mLargeImageView.moveBy(tMoveX - mLastMoveX, tMoveY - mLastMoveY);mLastMoveX = tMoveX;mLastMoveY = tMoveY;break;case MotionEvent.ACTION_UP:mLastMoveX = 0;mLastMoveY = 0;break;}return true;}});
重写View的setOnTouchListener()方法,计算手指移动的距离,调用上面的moveBy(),来实现Image的移动。

Android 高清加载巨图方案 拒绝压缩图片
LargeImage
Android多点触控技术,实现对图片的放大缩小平移,惯性滑动等功能
