Skip to content

Commit 04af0b7

Browse files
committed
1、优化显示
1 parent 2c92fdc commit 04af0b7

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

OpenImageLib/src/main/java/com/flyjingfish/openimagelib/photoview/PhotoViewSuperBigImageHelper.java

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import android.os.Handler;
1111
import android.os.Looper;
1212
import android.os.Message;
13+
import android.util.Log;
1314
import android.view.View;
1415
import android.view.ViewTreeObserver;
1516

@@ -324,12 +325,16 @@ protected DecoderBitmap doInBackground(Void... params) {
324325

325326
// Rect subsamplingRect = new Rect((int) (left/scale), (int) (top/scale), (int) (right/scale), (int) (bottom/scale));
326327
// showRect = new Rect(left1,top1,right1,bottom1);
327-
Rect subsamplingRect = new Rect((int) ((left - cacheLengthLeft) / scale), (int) ((top - cacheLengthTop) / scale), (int) ((right + cacheLengthRight) / scale), (int) ((bottom + cacheLengthBottom) / scale));
328-
RectF showViewRect = new RectF((left1 - cacheLengthLeft), (top1 - cacheLengthTop), (right1 + cacheLengthRight), (bottom1 + cacheLengthBottom));
329-
// int inSampleSize = BitmapUtils.getMaxInSampleSize(subsamplingRect.width(), subsamplingRect.height());
330-
int scaleH = (int) (subsamplingRect.height()*1f/viewHeight);
331-
int scaleW = (int) (subsamplingRect.width()*1f/viewWidth);
332-
int inSampleSize = Math.max(Math.min(scaleH,scaleW),1);
328+
// Rect subsamplingRect = new Rect((int) ((left - cacheLengthLeft) / scale), (int) ((top - cacheLengthTop) / scale), (int) ((right + cacheLengthRight) / scale), (int) ((bottom + cacheLengthBottom) / scale));
329+
// RectF showViewRect = new RectF((left1 - cacheLengthLeft), (top1 - cacheLengthTop), (right1 + cacheLengthRight), (bottom1 + cacheLengthBottom));
330+
Rect subsamplingRect = new Rect((int) ((left ) / scale), (int) ((top ) / scale), (int) ((right ) / scale), (int) ((bottom ) / scale));
331+
RectF showViewRect = new RectF((left1 ), (top1), (right1 ), (bottom1 ));
332+
// int inSampleSize2 = BitmapUtils.getMaxInSampleSize(subsamplingRect.width(), subsamplingRect.height());
333+
// int scaleH = (int) (subsamplingRect.height()*1f/viewHeight);
334+
// int scaleW = (int) (subsamplingRect.width()*1f/viewWidth);
335+
// int inSampleSize = Math.max(Math.min(scaleH,scaleW),1);
336+
int inSampleSize = calculateInSampleSize(subsamplingRect, (int) showViewRect.width(), (int) showViewRect.height());
337+
// int inSampleSize = calculateInSampleSize(subsamplingRect, viewWidth, viewHeight);
333338
RectF subsamplingRectF= new RectF(subsamplingRect.left,subsamplingRect.top,subsamplingRect.right,subsamplingRect.bottom);
334339
rotateRect(subsamplingRectF,rotate,originalImageSize);
335340
Bitmap bitmap = null;
@@ -339,6 +344,7 @@ protected DecoderBitmap doInBackground(Void... params) {
339344
int pivotY = bitmap.getHeight() / 2; // 旋转中心的Y坐标
340345

341346
bitmap = rotateBitmap(bitmap, rotate, pivotX, pivotY);
347+
// OpenImageLogUtils.logE("inSampleSize",inSampleSize+"=="+"==="+bitmap.getWidth()+"=="+bitmap.getHeight()+"==="+subsamplingRect.width()+"=="+subsamplingRect.height());
342348
} catch (Exception e) {
343349
throw new RuntimeException(e);
344350
}
@@ -354,6 +360,21 @@ protected DecoderBitmap doInBackground(Void... params) {
354360
}
355361

356362

363+
public static int calculateInSampleSize(Rect region, int viewWidth, int viewHeight) {
364+
int regionWidth = region.width();
365+
int regionHeight = region.height();
366+
367+
int inSampleSize = 1;
368+
369+
// 确保至少比 View 大一点,防止模糊
370+
while ((regionWidth / (inSampleSize * 2)) >= viewWidth &&
371+
(regionHeight / (inSampleSize * 2)) >= viewHeight) {
372+
inSampleSize *= 2;
373+
}
374+
375+
return inSampleSize; // 不能太大,防止模糊
376+
}
377+
357378

358379
public static Bitmap rotateBitmap(Bitmap source, float angle, int pivotX, int pivotY) {
359380
Matrix matrix = new Matrix();

0 commit comments

Comments
 (0)