多的不说,少的不唠,上图:
实现思路:
局部加载,触摸拖动,双击缩放
开搞:
一些初始化
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 < 0) {
// mRect.top = 0;
// mRect.bottom = (int) (mViewHeight / mScale);
// }
// invalidate();
mRect.offset((int) distanceX, (int) distanceY);
// 移动时,处理到达顶部和底部的情况
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);
}
if (mRect.right > mImageWidth) {
mRect.right = mImageWidth;
mRect.left = mImageWidth - (int) (mViewWidth / mScale);
}
if (mRect.left < 0) {
mRect.left = 0;
mRect.right = (int) (mViewWidth / mScale);
}
invalidate();
return false;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
// mScroller.fling(0, mRect.top, 0, (int) -velocityY, 0, 0, 0, mImageHeight - (int) (mViewHeight / mScale));
mScroller.fling(mRect.left, mRect.top, (int) velocityX, (int) -velocityY, 0,
mImageWidth - (int) (mViewWidth / mScale), 0,
mImageHeight - (int) (mViewHeight / mScale));
return false;
}
@Override
public void computeScroll() {
super.computeScroll();
if (mScroller.isFinished()) {
return;
}
if (mScroller.computeScrollOffset()) {
mRect.top = mScroller.getCurrY();
mRect.bottom = mRect.top + (int) (mViewHeight / mScale);
invalidate();
}
}
@Override
public boolean onDoubleTap(MotionEvent event) {
//双击放大图片
if (mScale < originalScale * 2) {
mScale = originalScale * 2;
} else {
mScale = originalScale;
}
Log.e("xxxxx mScale ", "mScale = " + mScale + " originalScale = " + originalScale);
mRect.right = mRect.left + (int) (mViewWidth / mScale);
mRect.bottom = mRect.top + (int) (mViewHeight / mScale);
// 移动时,处理到达顶部和底部的情况
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);
}
if (mRect.right > mImageWidth) {
mRect.right = mImageWidth;
mRect.left = mImageWidth - (int) (mViewWidth / mScale);
}
if (mRect.left < 0) {
mRect.left = 0;
mRect.right = (int) (mViewWidth / mScale);
}
Log.e("xxxxx Rect ", "mRect.Left = " + mRect.left +
" mRect.right = " + mRect.right +
" mRect.top = " + mRect.top +
" mRect.bottom = " + mRect.bottom);
invalidate();
return false;
}
@Override
public void onShowPress(MotionEvent event) {
}
@Override
public boolean onSingleTapUp(MotionEvent event) {
return false;
}
@Override
public void onLongPress(MotionEvent event) {
}
@Override
public boolean onSingleTapConfirmed(MotionEvent event) {
return false;
}
@Override
public boolean onDoubleTapEvent(MotionEvent event) {
return false;
}
// 处理缩放的回调事件
class ScaleGesture extends ScaleGestureDetector.SimpleOnScaleGestureListener {
@Override
public boolean onScale(ScaleGestureDetector detector) {
float scale = mScale;
scale += detector.getScaleFactor() - 1;// getScaleFactor()跟上一次事件相比得到的事件因子;
// getScaleFactor初始值=1;
// 以下是日志打印结果,mScale它是在变化的,而getScaleFactor()的值也是实时变化的,还需要将变化的值即时赋给mScale;
// detector = 1.0
// detector = 1.0241059
// detector = 1.0649118
// detector = 1.0915182
// detector = 1.1195749
Log.e("xxxxxx", "detector = " + detector.getScaleFactor());
if (scale <= originalScale) {
scale = originalScale;
} else if (scale > 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();
}