我正在使用毕加索来调整背景图像的大小以填充屏幕宽度(不调整图像高度)。
问题是,即使我将图像宽度设置为与屏幕宽度相同,图像也不会填充到水平边缘,但是存在间隙。在800dp时,差距为35dp。
这是我的代码
ImageView imgBackground = (ImageView) v.findViewById(R.id.imgBackground);
Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.morn_blur_bg);
WindowManager wm = (WindowManager) v.getContext().getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int displayWidth = size.x;
int height = size.y;
int width = 0;
//Log.d("CP", "Screen width="+displayWidth);
//Log.d("CP", "Image width=" + bMap.getWidth());
if (bMap.getWidth() < displayWidth) {
width = displayWidth;
} else {
width = bMap.getWidth();
}
Log.d("CP", "New width=" + width);
Picasso.with(v.getContext())
.load(R.drawable.morn_blur_bg)
.resize(width, height)
.into(imgBackground);
这是我的xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/layRoot"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<!--Background image-->
<ImageView
android:id="@+id/imgBackground"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" />
如果我将宽度硬编码为835dp,则图像会填满显示屏,但当然这不会在设备上运行。 : - )
欢呼声
答案 0 :(得分:0)
在ImageView的地方使用自定义图像视图:
public class DynamicImageView extends ImageView {
public DynamicImageView(final Context context, final AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) {
final Drawable d = this.getDrawable();
if (d != null) {
// ceil not round - avoid thin vertical gaps along the left/right edges
final int width = MeasureSpec.getSize(widthMeasureSpec);
final int height = (int) Math.ceil(width * (float) d.getIntrinsicHeight() / d.getIntrinsicWidth());
this.setMeasuredDimension(width, height);
} else {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
}
这可能会解决您的问题!
答案 1 :(得分:0)
试试这个 Picasso.with(v.getContext()) .load(R.drawable.morn_blur_bg) .resize(宽度,高度) 。适合() .into(imgBackground);
答案 2 :(得分:0)
尝试使用 .CenterCrop
Picasso.with(v.getContext())
.load(R.drawable.morn_blur_bg)
.resize(width, height)
.centerCrop()
.into(imgBackground);