[英]Android, ImageView, unwanted padding
我正在使用類似的方式來展示我的徽標或用戶個人資料圖片:
<ImageView
android:id="@+id/logo"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@drawable/circle_bg"
android:src="@drawable/account_circle_grey"
android:layout_gravity="center_horizontal" />
@drawable/circle_bg
:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid
android:color="@color/colorWhiteOpacity"/>
<size
android:width="100dp"
android:height="100dp"/>
</shape>
@drawable/account_circle_grey
只是我從MaterialUI (黑色192x192)拍攝的,並使用了Final-Android-Resizer的圖像 。
問題是我得到了這樣的東西:
基本上,問題是當用戶單擊並選擇圖片時,我使用glide加載圖片以替換默認的@drawable/account_circle_gray
但是您可以看到填充已消失:
圖片占據了全部空間,應該padding="5dp"
!我想添加的是padding="5dp"
,這會在背景和Glide
加載的用戶圖片之間提供一些填充, 問題是已經很奇怪了 padding到@drawable/account_circle_grey
,我注意到但是並不是所有的drawable都可以填充,當我選擇用作整頁背景的舊圖片時我沒有得到padding,也許我可以強迫android選擇更大的圖片,為什么我得到這個填充?
編輯:為清楚起見,我使用本教程嘗試了所有可能的(scaleType + adjustViewBounds)值,但沒有任何效果。
設定scaleType
您的ImageView
到centerFit
或者您也可以只添加高DPI繪制您可繪制
當您使用物理上小於ImageView大小的圖像時,可能會發生這種情況。 您可以縮放圖像以填充空間或使用更大的圖像。 兩者的混合可能是最好的解決方案。
要實現兩者中的前者,請嘗試以下操作:
<ImageView
android:id="@+id/logo"
android:layout_width="90dp"
android:layout_height="90dp"
android:background="@drawable/circle_bg"
android:src="@drawable/account_circle_grey"
android:layout_gravity="center_horizontal"
android:scaleType="fitXY"
android:padding="5dp" />
請注意,我將寬度和高度減少了2倍。 這是因為在視圖中添加填充會增加整個視圖的大小,其大小取決於填充的大小。 在這種情況下,我們在左側,右側,頂部和左側添加5dp。 這意味着我們必須從寬度中減去左右,從高度中減去頂部和底部。
感謝您的回答,問題非常明顯,但是我使其變得復雜,盡管我使用了許多“ account_circle”圖像,但我不認為它們都帶有透明填充,因此,例如,如果圖像為192x192
,實際大小約為182x182
,每邊都有5px
透明填充,無論如何,簡單的解決方案是在photoshop上打開它,轉到“圖像”>“修剪”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.