[英]ImageView with rounded corners via another Image as a background
我有兩個圖像 - 1)矩形圖像,顯示實際內容和2)帶圓角透明角的白色圖像
是否可以將圖像1放在圖像2中,保持其大小,但使其形狀與2相同?
基本上我希望Image 2成為圖像1的容器。
我已經嘗試了圖層和插圖drawables但是圖像1一直重疊圖像2。
提前致謝!
更新1:
這是我的ImageView xml部分:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="72dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/avatar"
android:src="@drawable/mainImg"
android:background="@drawable/backgroundImg"
android:layout_width="56dp"
android:layout_height="56dp"
android:layout_gravity="center"
android:contentDescription="@string/desc" />
</LinearLayout>
更新2:
下面是三個圖像的鏈接1)背景2)主圖像3)預期結果(帶圓角)
一個簡單的解決方案是使用一個ImageView
和android:background
為你的image2,你說它是容器,而android:src
用於image1,這是實際的圖像:
<ImageView
...
android:background="@drawable/image2"
android:src="@drawable/image1"
android:padding="2dp" />
只需添加一個填充,以指定要在“框架”和實際“圖片”之間留出多少空白空間。
我不得不用我最近制作的應用來處理這個問題。 請注意,在第一個和第二個屏幕截圖中,縮略圖是如何構圖的。
為了實現這一點,我在FrameLayout中將圖像和框架堆疊在一起。 首先,我布置實際圖像(@ id / thumbnail),然后是框架(@ id / frame)。
需要注意的重要事項是縮略圖使用的是scaleType“fitXY”,並且略有邊距,因此角落不會在框架的圓角后面伸出。
這僅適用於框架邊框不透明的情況,因此您可能必須使框架邊緣與背景顏色相同。
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="@dimen/thumbnail_size"
android:layout_height="@dimen/thumbnail_size"
android:layout_margin="5dp"
android:gravity="center"
android:orientation="vertical"
>
<ImageView
android:id="@+id/thumbnail"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="4dp"
android:scaleType="fitXY" />
<ImageView
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/pixel_frame"
android:scaleType="fitXY" />
</FrameLayout>
使用ImageButton ..將Background設置為Image1,將Image src設置為Image2
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/image2"
android:src="@drawable/image1" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.