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