我正在制作一个自定义视图,它会给我带来意想不到的效果。我想要做的是将图片填充到视图的宽度并按比例缩放它,保持其纵横比,但它正在做一些奇怪的事情......
这是我生成的日志吐出的内容。
现在让我解释一下日志根据代码说的内容。首先,我们让视图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 :(得分:1)
这里的问题是图像比率计算。 Bitmap.getHeight()和getWidth()
返回整数,这是
(this.mOriginalBitmap.getHeight()
/ this.mOriginalBitmap.getWidth())
对于1
的图片,为960x1280
。
这有几种选择。您可以通过转换float
和getHeight()
的返回值来进行getWidth()
除法:
((float) this.mOriginalBitmap.getHeight()
/ (float) this.mOriginalBitmap.getWidth())
或者你可以简单地从乘法开始:
reqHeight = (reqWidth * this.mOriginalBitmap.getHeight()) / this.mOriginalBitmap.getWidth();
答案 1 :(得分: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);
}
}
}