简体   繁体   English

定制 xml 形状 android

[英]make custom xml shape android

I need to make custom shape like below:我需要制作如下自定义形状: 设计形象

in order to do that I've wrote below xml:为了做到这一点,我在下面写了 xml:

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

    >
    <item android:id="@+id/ring1">
        <shape
            android:innerRadiusRatio=""
            android:shape="ring"
            android:tint="@color/white"
            android:useLevel="false"></shape>

    </item>

    <item>
        <shape
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:innerRadius="0dp"
            android:shape="ring"
            android:thickness="140dp"
            android:useLevel="false">
            <gradient
                android:centerColor="#00F913"
                android:endColor="#006A10"
                android:startColor="#006A10" />

        </shape>

    </item>
    

</layer-list>

which it's result is:结果是:

结果图像

And there is 3 problems:并且有3个问题:

  1. 1st: when I give it to any views background the outer white line wouldn't appear第一:当我将它提供给任何视图背景时,外部白线不会出现
  2. 2nd: the shape is not a ring when i give it to views background第二:当我把它给视图背景时,形状不是一个环
  3. 3rd: the original view has a color at center and it has other color around but with gradient I can add color to start, center, end and in the middle the middle gradient is extends to borders第三:原始视图在中心有一种颜色,周围有其他颜色,但使用渐变我可以将颜色添加到开始、中心、结束和中间,中间渐变延伸到边框

1st: when I give it to any views background the outer white line wouldn't appear第一:当我将它提供给任何视图背景时,外部白线不会出现

You can use oval shape instead of using ring for the white part., and add padding as much you need the radius of the outer white part as modified below您可以使用oval形而不是使用ring作为白色部分,并根据需要添加填充,如下所示修改外部白色部分的半径

<?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/white" />
            <padding
                android:bottom="7dp"
                android:left="7dp"
                android:right="7dp"
                android:top="7dp" />
        </shape>

    </item>

    <item>
        <shape android:shape="oval">
            <size
                android:width="36dp"
                android:height="36dp" />
            <gradient
                android:centerColor="#00F913"
                android:endColor="#006A10"
                android:startColor="#006A10" />
        </shape>

    </item>

</layer-list>

2nd: the shape is not a ring when i give it to views background第二:当我把它给视图背景时,形状不是一个环

You can use ShapeableImageView您可以使用ShapeableImageView

First: add below dependency in gradle module level首先:在 gradle 模块级别添加以下依赖项

implementation 'com.google.android.material:material:1.3.0-alpha04'

Second: create below style in style.xml第二:在style.xml中创建下面的样式

<style name="circled">
    <item name="cornerSize">50%</item>
</style>

Third: apply the style to a ShapeableImageView using app:shapeAppearanceOverlay attribute第三:使用app:shapeAppearanceOverlay属性将样式应用于ShapeableImageView

3rd: the original view has a color at center and it has other color around but with gradient I can add color to start, center, end and in the middle the middle gradient is extends to borders第三:原始视图在中心有一种颜色,周围有其他颜色,但使用渐变我可以将颜色添加到开始、中心、结束和中间,中间渐变延伸到边框

This should be solved by using ShapeableImageView , please check below result after running the app这应该通过使用ShapeableImageView来解决,请在运行应用程序后检查以下结果

在此处输入图像描述

Sample test样品测试

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:background="@color/colorAccent"
    android:layout_height="match_parent">

    <com.google.android.material.imageview.ShapeableImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:shapeAppearanceOverlay="@style/circled"
        app:srcCompat="@drawable/some_circle" />

</androidx.constraintlayout.widget.ConstraintLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM