在布局中,我想将背景图片按比例缩放以适应页面创建时分配的空间。有没有人有这样做的想法?
我正在使用layout.setBackgroundDrawable()
和BitmapDrawable()
来设置gravity
以进行裁剪和填充,但是没有看到任何缩放选项。
在布局中,我想将背景图片按比例缩放以适应页面创建时分配的空间。有没有人有这样做的想法?
我正在使用layout.setBackgroundDrawable()
和BitmapDrawable()
来设置gravity
以进行裁剪和填充,但是没有看到任何缩放选项。
要自定义背景图像的缩放,请创建如下资源:
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center"
android:src="@drawable/list_bkgnd" />
如果将其用作背景,则它将居中于视图中。还有其他标志:http://developer.android.com/guide/topics/resources/drawable-resource.html
我没有尝试过完全按照你想要的方式进行操作,但是你可以使用 android:scaleType="fitXY"
来缩放 ImageView ,
然后将其大小调整到 ImageView 给定的任何大小。
因此,您可以为布局创建一个 FrameLayout,将 ImageView 放在其中,然后将其他需要添加到 FrameLayout 的内容也放置在其中,设置为透明背景。
<FrameLayout
android:layout_width="fill_parent" android:layout_height="fill_parent">
<ImageView
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:src="@drawable/back" android:scaleType="fitXY" />
<LinearLayout>your views</LinearLayout>
</FrameLayout>
从drawable中有一种简单的方法可以实现这一点:
your_drawable.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@color/bg_color"/>
<item>
<bitmap
android:gravity="center|bottom|clip_vertical"
android:src="@drawable/your_image" />
</item>
</layer-list>
唯一的缺点是,如果没有足够的空间,您的图像将不会完全显示,而是被裁剪,我找不到一种直接从可绘制对象中实现这一点的方法。但从我做的测试来看,它效果还不错,并且不会裁剪太多的图像。您可以尝试更多重力选项。错误:<item>必须具有“名称”属性。
- Dmitry使用图像作为背景并根据布局大小进行调整:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/imgPlaylistItemBg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:maxHeight="0dp"
android:scaleType="fitXY"
android:src="@drawable/img_dsh" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</FrameLayout>
android:scaleType="centerCrop"
也可以使用。 - CoolMindandroid:scaleType=fitCenter
或fitStart
等。使用fitXY
将无法保持图像的原始纵横比!请注意,此方法仅适用于具有src
属性的图片,而不适用于背景图片。当您使用setBackgroundDrawable
方法设置ImageView的Drawable时,图像将始终被缩放。参数如adjustViewBounds
或不同的ScaleTypes
将被忽略。我发现保持宽高比的唯一解决方案是在加载Drawable后调整ImageView
的大小。以下是我使用的代码片段:
// bmp is your Bitmap object
int imgHeight = bmp.getHeight();
int imgWidth = bmp.getWidth();
int containerHeight = imageView.getHeight();
int containerWidth = imageView.getWidth();
boolean ch2cw = containerHeight > containerWidth;
float h2w = (float) imgHeight / (float) imgWidth;
float newContainerHeight, newContainerWidth;
if (h2w > 1) {
// height is greater than width
if (ch2cw) {
newContainerWidth = (float) containerWidth;
newContainerHeight = newContainerWidth * h2w;
} else {
newContainerHeight = (float) containerHeight;
newContainerWidth = newContainerHeight / h2w;
}
} else {
// width is greater than height
if (ch2cw) {
newContainerWidth = (float) containerWidth;
newContainerHeight = newContainerWidth / h2w;
} else {
newContainerWidth = (float) containerHeight;
newContainerHeight = newContainerWidth * h2w;
}
}
Bitmap copy = Bitmap.createScaledBitmap(bmp, (int) newContainerWidth, (int) newContainerHeight, false);
imageView.setBackgroundDrawable(new BitmapDrawable(copy));
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
imageView.setLayoutParams(params);
imageView.setMaxHeight((int) newContainerHeight);
imageView.setMaxWidth((int) newContainerWidth);
ImageView
对象。setMaxHeight
和setMaxWidth
只有在宽度和高度被定义为包裹内容时才会起作用,而不是填充父容器。另一方面,填充父容器是最初的期望设置,否则containerWidth
和containerHeight
都将等于0。...
<ImageView android:id="@+id/my_image_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
...
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">
<ImageView
android:id="@+id/ivBackground"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:scaleType="fitStart"
android:src="@drawable/menu_icon_exit" />
<Button
android:id="@+id/bSomeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="61dp"
android:layout_marginTop="122dp"
android:text="Button" />
</RelativeLayout>
public class ImageViewScaleTypeTopCrop extends ImageView {
public ImageViewScaleTypeTopCrop(Context context) {
super(context);
setup();
}
public ImageViewScaleTypeTopCrop(Context context, AttributeSet attrs) {
super(context, attrs);
setup();
}
public ImageViewScaleTypeTopCrop(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setup();
}
private void setup() {
setScaleType(ScaleType.MATRIX);
}
@Override
protected boolean setFrame(int frameLeft, int frameTop, int frameRight, int frameBottom) {
float frameWidth = frameRight - frameLeft;
float frameHeight = frameBottom - frameTop;
if (getDrawable() != null) {
Matrix matrix = getImageMatrix();
float scaleFactor, scaleFactorWidth, scaleFactorHeight;
scaleFactorWidth = (float) frameWidth / (float) getDrawable().getIntrinsicWidth();
scaleFactorHeight = (float) frameHeight / (float) getDrawable().getIntrinsicHeight();
if (scaleFactorHeight > scaleFactorWidth) {
scaleFactor = scaleFactorHeight;
} else {
scaleFactor = scaleFactorWidth;
}
matrix.setScale(scaleFactor, scaleFactor, 0, 0);
setImageMatrix(matrix);
}
return super.setFrame(frameLeft, frameTop, frameRight, frameBottom);
}
}
https://dev59.com/q2w15IYBdhLWcg3w4_7k#14815588
现在,如果有人编写自定义Drawable来缩放图像,那么IMHO就会非常完美。然后它可以用作背景参数。Reflog建议在使用drawable之前对其进行预缩放。以下是如何执行此操作的说明: Java(Android):如何在不使用位图的情况下缩放可绘制对象? 尽管它有缺点,即放大的drawable /位图将使用更多RAM,而ImageView使用的即时缩放不需要更多内存。优点可能是较少的处理器负载。
int bwidth=bitMap1.getWidth();
int bheight=bitMap1.getHeight();
int swidth=imageView_location.getWidth();
int sheight=imageView_location.getHeight();
new_width=swidth;
new_height = (int) Math.floor((double) bheight *( (double) new_width / (double) bwidth));
Bitmap newbitMap = Bitmap.createScaledBitmap(bitMap1,new_width,new_height, true);
imageView_location.setImageBitmap(newbitMap)
< RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/black">
<LinearLayout
android:layout_width="500dip"
android:layout_height="450dip"
android:layout_centerInParent="true"
android:background="@drawable/my_drawable"
android:orientation="vertical"
android:padding="30dip"
>
...
</LinearLayout>
< / RelativeLayout>
尝试的一个选项是将图像放置在drawable-nodpi
文件夹中,并将布局的背景设置为可绘制资源ID。
这肯定适用于缩小,但我还没有测试过放大。