2014-09-25 62 views
0

我正在处理自定义视图,这给我带来了意想不到的结果。我试图发生的事情是填充图片的宽度和视图的比例,并保持其纵横比,但它做了一些奇怪的事情...将图像缩放为自定义视图的宽度

这是我生成的日志正在吐出。

  • 的大小:768x942
  • PIC是肖像
  • 原稿尺寸:960x1280缩放:768x768
  • 缩放位图:768x768

现在让我解释什么日志根据代码说。首先我们让视图onMeasure本身,一旦完成,我们可以抓住视图的宽度和高度。接下来,我们检查图片是横向还是纵向。然后,我们只是做数学来找到我们需要缩放的大小。数学完成后,我们用结果创建一个新的缩放位图。宽度是正确的,但高度应该是1024不是768.我看不到它在哪里搞乱了。

public void setBitmap(Bitmap bmp) { 
this.mOriginalBitmap = bmp; 

if(mHasMeasured) { 
    //Make sure the view has measured itself so we can grab the width and height 
Log.d("", "View size: " + String.valueOf(this.mViewWidth) 
+ "x" + String.valueOf(this.mViewHeight)); 

int reqWidth, reqHeight; //The required sizes we need 

//Get the new sizes for the pic to fit in the view and keep aspect ratio 
if(this.mOriginalBitmap.getWidth() > this.mOriginalBitmap.getHeight()) { 
//Landscape :/ 
Log.d("", "Pic is Landscape"); 


reqHeight = this.mViewHeight; 

reqWidth = (this.mOriginalBitmap.getWidth() 
      /this.mOriginalBitmap.getHeight()) * reqHeight; 

} else { 
//Portrait :) 
Log.d("", "Pic is portrait"); 

reqWidth = this.mViewWidth; 

reqHeight = (this.mOriginalBitmap.getHeight() 
      /this.mOriginalBitmap.getWidth()) * reqWidth; 

} 




Log.d("", "Original Size: " 
      + String.valueOf(mOriginalBitmap.getWidth()) + "x" 
      + String.valueOf(mOriginalBitmap.getHeight()) 
      + " Scaled: " + String.valueOf(reqWidth) 
      + "x" + String.valueOf(reqHeight)); 

this.mBmpScaledForView = Bitmap.createScaledBitmap(mOriginalBitmap, 
     reqWidth, reqHeight, false); 

this.mSrc.top = 0; 
this.mSrc.left = 0; 
this.mSrc.right = this.mBmpScaledForView.getWidth(); 
this.mSrc.bottom = this.mBmpScaledForView.getHeight(); 
this.mDst = this.mSrc; 

Log.d("", "Scaled bitmap : " 
      + String.valueOf(this.mBmpScaledForView.getWidth()) 
      + "x" + String.valueOf(this.mBmpScaledForView.getHeight())); 

} 
} 
+0

问题与整数除法。 'Bitmap.getHeight'返回一个int,所以'getHeight/getWidth'为1,这会导致'reqWidth == reqHeight' – njzk2 2014-09-25 15:06:48

+0

啊!好的,那么用float吧? – bwoogie 2014-09-25 15:07:36

+0

或简单地反转你的操作:'reqHeight =(reqWidth * this.mOriginalBitmap.getHeight())/ this.mOriginalBitmap.getWidth();' – njzk2 2014-09-25 15:08:41

回答

1

这里的问题是图像比例计算。 Bitmap.getHeight()getWidth()返回整数,这使得

(this.mOriginalBitmap.getHeight() 
     /this.mOriginalBitmap.getWidth()) 

结果是1因为这是960x1280的图像。

有几个选项。您可以通过转换的getHeight()getWidth()返回值进行float师:

((float) this.mOriginalBitmap.getHeight() 
     /(float) this.mOriginalBitmap.getWidth()) 

或者你可以简单地通过乘法开始:

reqHeight = (reqWidth * this.mOriginalBitmap.getHeight())/this.mOriginalBitmap.getWidth(); 
0
package com.riktamtech.app.utils; 

import android.content.Context; 
import android.graphics.drawable.Drawable; 
import android.util.AttributeSet; 
import android.widget.ImageView; 

/** 
* ImageView that keeps aspect ratio when scaled 
*/ 

public class CustomImageView extends ImageView { 

    public CustomImageView(Context context) { 
     super(context); 
    } 

    public CustomImageView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public CustomImageView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     try { 
      Drawable drawable = getDrawable(); 
      if (drawable == null) { 
       setMeasuredDimension(0, 0); 
      } else { 
       int measuredWidth = MeasureSpec.getSize(widthMeasureSpec); 
       int measuredHeight = MeasureSpec.getSize(heightMeasureSpec); 
       if (measuredHeight == 0 && measuredWidth == 0) { // Height and 
                    // width set 
                    // to 
                    // wrap_content 
        setMeasuredDimension(measuredWidth, measuredHeight); 
       } else if (measuredHeight == 0) { // Height set to wrap_content 
        int width = measuredWidth; 
        int height = width * drawable.getIntrinsicHeight() 
          /drawable.getIntrinsicWidth(); 
        setMeasuredDimension(width, height); 
       } else if (measuredWidth == 0) { // Width set to wrap_content 
        int height = measuredHeight; 
        int width = height * drawable.getIntrinsicWidth() 
          /drawable.getIntrinsicHeight(); 
        setMeasuredDimension(width, height); 
       } else { // Width and height are explicitly set (either to 
          // match_parent or to exact value) 
        setMeasuredDimension(measuredWidth, measuredHeight); 
       } 
      } 
     } catch (Exception e) { 
      super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
     } 
    } 

}