【问题标题】:Add a background image to shape in XML Android在 XML Android 中为形状添加背景图像
【发布时间】:2014-01-08 17:14:55
【问题描述】:

如何为形状添加背景图片?我在下面尝试但没有成功的代码:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
//here is where i need to set the image
<solid android:color="@drawable/button_image"/>
    <corners
     android:bottomRightRadius="5dp"
     android:bottomLeftRadius="5dp"
     android:topLeftRadius="5dp"
     android:topRightRadius="5dp"/>
 </shape>

【问题讨论】:

  • 您需要扩展并创建一个自定义形状的类。以 RoundedImageView 为例。它会创建一个圆形图像视图,因此无论您设置什么 iamge 作为它的背景,它都会显示为圆形

标签: android android-xml


【解决方案1】:

使用以下layerlist

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <shape android:shape="rectangle" android:padding="10dp">
            <corners
                 android:bottomRightRadius="5dp"
                 android:bottomLeftRadius="5dp"
                 android:topLeftRadius="5dp"
                 android:topRightRadius="5dp"/>
         </shape>
   </item>
   <item android:drawable="@drawable/image_name_here" />
</layer-list>

【讨论】:

  • 谢谢,这确实有效,但是边缘不再是圆形的?
  • 你想要圆边的图片吗?因为看起来你的形状没有任何颜色
  • 我的形状确实有白色和圆形边缘。我认为如果我移除颜色并改用图像,圆边会保留。如果这是不可能的,那没关系
  • 那么圆角的图片,不可能吗?
  • 这是可能的,但您需要以编程方式对图像的角进行圆角处理。看到这个stackoverflow.com/questions/2459916/…
【解决方案2】:

我将以下内容用于具有圆形背景的可绘制图像。

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="oval">
            <solid android:color="@color/colorAccent"/>
        </shape>
    </item>
    <item
        android:drawable="@drawable/ic_select"
        android:bottom="20dp"
        android:left="20dp"
        android:right="20dp"
        android:top="20dp"/>
</layer-list>

这就是它的样子

希望对某人有所帮助。

【讨论】:

  • 有没有什么东西可以设置图片的重力。
  • @SagarDevanga 用于上述代码中的可绘制对象或当您在布局中包含图像时?
  • 首先,我尝试使用高度和宽度将内部图标设置为 24dp(其实际大小)。这在 AS 中的预览上完美地调整了大小,但对平板电脑没有影响。所以我用了你的方法。我将形状的高度和宽度设置为 40dp,然后将图标项的顶部、底部、左侧和右侧分别设置为 8dp。 (40-24)/2=8。但是,生成的图像看起来大小合适但模糊,我怀疑这是由于对 PNG 进行了某种调整。有什么建议吗?
  • TLDR;您如何在不损失图像质量的情况下创建上面带有 40dp 直径圆和 24dp 图标的图像?
  • @LukeAllison 您如何将图标添加到您的 UI 以及您放置在椭圆形中的图像有多大?
【解决方案3】:

这是一个圆形,里面有图标:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/ok_icon"/>
    <item>
        <shape
                android:shape="oval">
            <solid android:color="@color/transparent"/>
            <stroke android:width="2dp" android:color="@color/button_grey"/>
        </shape>
    </item>
</layer-list>

【讨论】:

  • 更适合想要显示插入符号图像和边框的人
【解决方案4】:

这是一个角落图像

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:drawable="@drawable/img_main_blue"
        android:bottom="5dp"
        android:left="5dp"
        android:right="5dp"
        android:top="5dp" />

    <item>
        <shape
            android:padding="10dp"
            android:shape="rectangle">
            <corners android:radius="10dp" />
            <stroke
                android:width="5dp"
                android:color="@color/white" />
        </shape>

    </item>
</layer-list>

【讨论】:

  • 这只是颜色覆盖可绘制项目。对于需要中风图像的人来说,这是完美的解决方案
【解决方案5】:

我将以下内容用于带有边框的可绘制图像。

首先在drawable文件夹中用这段代码制作一个.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape android:shape="oval">
        <solid android:color="@color/orange"/>
    </shape>
</item>
<item
    android:top="2dp"
    android:bottom="2dp"
    android:left="2dp"
    android:right="2dp">
    <shape android:shape="oval">
        <solid android:color="@color/white"/>
    </shape>
</item>
<item
    android:drawable="@drawable/messages" //here messages is my image name, please give here your image name.
    android:bottom="15dp"
    android:left="15dp"
    android:right="15dp"
    android:top="15dp"/>

第二次在layout文件夹中创建一个view .xml文件,这样调用上面的.xml文件

<ImageView
   android:id="@+id/imageView2"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:src="@drawable/merchant_circle" />  // here merchant_circle will be your first .xml file name

【讨论】:

    【解决方案6】:

    这是为您的图像(图像视图)获取自定义形状的另一种最简单的方法。它可能对某人有帮助。这只是一行代码。

    首先你需要添加一个依赖:

    dependencies {
        compile 'com.mafstech.libs:mafs-image-shape:1.0.4'   
    }
    

    然后就写一行这样的代码:

    Shaper.shape(context, 
           R.drawable.your_original_image_which_will_be_displayed, 
           R.drawable.shaped_image_your_original_image_will_get_this_images_shape,
           imageView,
           height, 
           weight);   
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多