如何在圆形imageView中设置背景

时间:2017-12-19 19:04:33

标签: android android-vectordrawable

我有一个圆形的ImageView,你可以在下面看到,这引用了一个矢量drawable,我想用另一个视图填充圆圈的内部,没有圆形外面的那个矩形,这有可能吗?< / p>

<FrameLayout
    android:layout_gravity="center_horizontal"
    android:layout_width="150dp"
    android:layout_height="150dp"
    android:background="@android:color/black"
    >

    <com.myapp.MySpecialBackground 
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/icon"
        android:tint="@color/blue"/>
</FrameLayout>

Image

1 个答案:

答案 0 :(得分:0)

您可以使用ShapeDrawable作为后台资源。将可绘制的资源文件添加到项目中并设置&#34; padding&#34;所以圆形不会显示在你的蓝色圆圈之外:

<?xml version="1.0" encoding="utf-8"?>
<shape
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:shape="oval">
     <padding
         android:left="30dp"
         android:top="30dp"
         android:right="30dp"
         android:bottom="30dp" />
     <size
         android:width="150dp"
         android:height="150dp" />

     <solid android:color="#ff0000"/>
</shape>

我使用了30dp,但很难从屏幕截图中看出来,所以很可能你不得不进行实验。请记住,如果您的ImageView是正方形并且所有填充值都相等,那么您将获得一个圆圈。

(对于较旧的Android版本:size标记相对较新,在早期版本中,width和height属性进入shape标记)

相关问题