Android:shape、XML绘图

1.线line:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="line">
    <stroke android:width="8dp"
        android:color="#ff00ff00"
        android:dashWidth="6dp"
        android:dashGap="2dp"/>
</shape>


2.椭圆oval:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="#ff345678"/>
    <stroke android:width="20dp" android:color="#ff876543"/>
</shape>


3.长方形rectangle:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <size android:width="200dp"
        android:height="300dp"/>
    <solid android:color="#ff665544"/>
    <stroke android:width="20dp"
        android:color="#445566"
        android:dashWidth="40dp"
        android:dashGap="5dp"/>
</shape>


4.填充颜色solid、渐变gradient、边角corners:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <size android:width="200dp"
        android:height="300dp"/>
    <solid android:color="#ff665544"/>
    <stroke android:width="20dp"
        android:color="#445566"
        android:dashWidth="40dp"
        android:dashGap="5dp"/>
        <gradient android:startColor="#ff5533aa"
            android:centerColor="#ff996633"
            android:endColor="#ff114477"
            android:angle="135"
        />
        <corners
            android:topLeftRadius="45dp"
            android:topRightRadius="45dp"
            android:bottomLeftRadius="135dp"
            android:bottomRightRadius="135dp"/>
                              
            <!-- android:radius="45dp" -->
            <!--  渐变角度 只能被45整除  -->
</shape>




5.代码编写

//白色背景shape
int strokeWidth = 5; // 3dp
int roundRadius = 15; // 8dp
int strokeColor = Color.parseColor("#2E3135");
int fillColor = Color.parseColor("#DFDFE0");
GradientDrawable gd = new GradientDrawable();
gd.setColor(fillColor);
gd.setCornerRadius(roundRadius);
gd.setStroke(strokeWidth, strokeColor);







本文出自 “天空没有痕迹但我飞过” 博客,转载请与作者联系!

你可能感兴趣的:(shape,XML绘图)