java Android 编程裁剪背景图片
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34909808/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Programming crop background image
提问by MyNewName
I want to display an image as background in my app. Now I used this statement: android:background="@drawable/background"
in <LineraLayout>
. Here is the .xml
Code:
我想在我的应用程序中显示图像作为背景。现在我使用了这个语句:android:background="@drawable/background"
在<LineraLayout>
. 这是.xml
代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="overbit.user.MainActivity"
android:background="@drawable/background">
</LinearLayout>
Now I get this output:
现在我得到这个输出:
But I want it like this:
但我想要这样:
Maybe someone of you can help me. Thanks.
也许你们中有人可以帮助我。谢谢。
回答by The Dreams Wind
There is BitmapDrawable, which allows to do it. First you need to create a drawable resource as follows (let it be a file in the project resources res/drawable/mybg.xml
):
有BitmapDrawable,它允许这样做。首先需要创建一个drawable资源如下(让它成为项目resources中的一个文件res/drawable/mybg.xml
):
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/background"
android:gravity="center" />
And then specify it as a background resource in your layout:
然后将其指定为布局中的背景资源:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="overbit.user.MainActivity"
android:background="@drawable/mybg">
</LinearLayout>
回答by Submersed
You can accomplish what you're trying to do by using BitmapRegionDecoder.
您可以使用BitmapRegionDecoder完成您想要做的事情。
From the docs:
从文档:
BitmapRegionDecoder can be used to decode a rectangle region from an image. BitmapRegionDecoder is particularly useful when an original image is large and you only need parts of the image.
BitmapRegionDecoder 可用于解码图像中的矩形区域。当原始图像很大并且您只需要图像的一部分时,BitmapRegionDecoder 特别有用。
It allows you to decode a Rect area of a particular Bitmap fairly easily, the only thing you need to do is calculate your Rect region to decode relative to the Bitmap.
它允许您相当容易地解码特定位图的 Rect 区域,您唯一需要做的就是计算您的 Rect 区域以相对于位图进行解码。
回答by drWisdom
You need to change the image's size itself in photoshop or something to achieve the desired result (still this can be very difficult because of various screen sizes of android smartphones). A workaround can be to change your parent layout to Relativelayout and then use a an imageview to show your picture like this:
您需要在 photoshop 或其他东西中更改图像的大小以达到所需的结果(由于 android 智能手机的各种屏幕尺寸,这仍然非常困难)。一种解决方法是将您的父布局更改为相对布局,然后使用图像视图来显示您的图片,如下所示:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" xmlns:ads="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:layout_width="match_parent" //make sure its match parent android:layout_height="match_parent"// and this one also android:src="@drawable/yourBackgroundImage" android:scaleType="..."/> <LinearLayout>put your childviews here</LinearLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" xmlns:ads="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:layout_width="match_parent" //make sure its match parent android:layout_height="match_parent"// and this one also android:src="@drawable/yourBackgroundImage" android:scaleType="..."/> <LinearLayout>put your childviews here</LinearLayout>
there are 4-5 options available for android:scaleType... experiment with them, till you get your desired result. Also note that you need to use android:srcrather than android:backgroundattribute of the imageview
有 4-5 个选项可用于android:scaleType... 试验它们,直到你得到你想要的结果。另请注意,您需要使用android:src而不是android:background图像视图的属性
回答by Vaibhav Sharma
In android the position of an ImageView is determined by the top left side of the image. I am assuming that the x direction is 1/3rd of the width from the start point. Now we can set the pivot. Basically the location of the pivot point, is a location around which the view will rotate and scale.
在android中,ImageView的位置由图像的左上角决定。我假设 x 方向是起点宽度的 1/3。现在我们可以设置枢轴了。基本上,枢轴点的位置是视图将围绕其旋转和缩放的位置。
int displacementX = ImageView.getWidth() / 3;
int displacementY = 10;
imgview.setPivotX((float)displacementX);
imgview.setPivotY((float)displacementY);
Now that we have set the pivot we can use a scale type to fit the image.
现在我们已经设置了枢轴,我们可以使用缩放类型来适应图像。
imgview.setScaleType(ImageView.ScaleType.CENTER_CROP);