Android编程庄稼背景图象

时间:2016-01-20 20:25:43

标签: java android xml

我想在我的应用中将图片显示为背景。现在我在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>

现在我得到了这个输出:

image

但我想这样:

as

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

4 个答案:

答案 0 :(得分:12)

BitmapDrawable,允许这样做。首先,您需要创建一个可绘制资源,如下所示(让它成为项目资源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>

答案 1 :(得分:4)

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

来自文档:

  

BitmapRegionDecoder可用于解码矩形区域   图片。 BitmapRegionDecoder在原始时特别有用   图像很大,你只需要部分图像。

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

答案 2 :(得分:3)

你需要在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>

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

答案 3 :(得分:0)

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

duplicated()

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

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