1010import android .os .Handler ;
1111import android .os .Looper ;
1212import android .os .Message ;
13+ import android .util .Log ;
1314import android .view .View ;
1415import 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