繁体   English   中英

Android编程庄稼背景图象

[英]Android Programming crop background image

我想在我的应用中将图像显示为背景。 现在我在<LineraLayout>使用了这个语句: android:background="@drawable/background" 这是.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>

现在我得到这个输出:

图片

但我希望这样:

如

也许有人可以帮助我。 谢谢。

BitmapDrawable ,允许这样做。 首先,您需要创建一个drawable资源,如下所示(让它成为项目资源res/drawable/mybg.xml ):

<bitmap
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/background"
    android:gravity="center" />

然后将其指定为布局中的后台资源:

<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>

您可以使用BitmapRegionDecoder完成您尝试执行的操作。

来自文档:

BitmapRegionDecoder可用于解码图像中的矩形区域。 当原始图像很大并且您只需要部分图像时,BitmapRegionDecoder特别有用。

它允许您相当容易地解码特定位图的Rect区域,您唯一需要做的就是计算您的Rect区域以相对于Bitmap进行解码。

你需要在photoshop或其他东西中改变图像的大小以达到预期的效果(由于Android智能手机的各种屏幕尺寸,这仍然非常困难)。 解决方法可以是将您的父布局更改为Relativelayout,然后使用imageview显示您的图片,如下所示:

 <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> 

有4-5个可用于android的选项:scaleType ...试验它们,直到你得到你想要的结果。 另请注意,您需要使用android:src而不是imageview的android:background属性

在android中,ImageView的位置由图像的左上角决定。 我假设x方向是从起点开始宽度的1/3。 现在我们可以设置枢轴了。 基本上,枢轴点的位置是视图将围绕其旋转和缩放的位置。

int displacementX = ImageView.getWidth() / 3;
int displacementY = 10;
imgview.setPivotX((float)displacementX);
imgview.setPivotY((float)displacementY);

现在我们已经设置了枢轴,我们可以使用缩放类型来适应图像。

imgview.setScaleType(ImageView.ScaleType.CENTER_CROP);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM