我正在从互联网上下载一个位图,然后我将此位图转换为Drawable,将其设置为我的LinearLayout中的背景。
我看到不幸的是图像被缩放以填充视图,我如何缩放图像直到最小尺寸到达边界(因此它没有被拉伸)? (并且可能居中)
我尝试过这样的事情但没有任何成功。 我在路上吗?
LinearLayout layout = (LinearLayout) context.findViewById(R.id.main);
Drawable picture = new BitmapDrawable(context.getResources(), bitmap);
Display display = context.getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
picture.setBounds(0, 0, size.x, size.y);
layout.setBackgroundDrawable(picture);
答案 0 :(得分:1)
我认为你需要这样的东西?
setScaleType(ScaleType.CENTER_CROP);
请参阅http://developer.android.com/reference/android/widget/ImageView.ScaleType.html
答案 1 :(得分:1)
我认为,不建议在应用上使用缩放图像。最佳实践是使用重复模式。所以,我会尝试这样的事情:
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/background-image-repeatable-pattern.png"
android:tileMode="repeat" />
将其保存到drawable / background.xml文件(例如)