Android如何以编程方式创建三角形和矩形?

标签 android shapedrawable

我们如何创建如下所示的可绘制气球形状。我们可以动态改变它的颜色。 enter image description here

最佳答案

这里是 XML 用于 trianglerectangle。将其保存在可绘制文件夹中。

triangle.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item >
        <rotate
            android:fromDegrees="45"
            android:toDegrees="45"
            android:pivotX="-40%"
            android:pivotY="87%" >
            <shape
                android:shape="rectangle"  >
                <stroke android:color="@android:color/transparent" android:width="10dp"/>
                <solid
                    android:color="#000000"  />
            </shape>
        </rotate>
    </item>
</layer-list>

rectangle.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item> 
    <shape android:shape="rectangle">
      <solid android:color="#B2E3FA" /> 
    </shape>
  </item>
</layer-list>

布局您需要的形状。

<RelativeLayout
        android:id="@+id/rlv1"
        android:layout_width="150dp"
        android:layout_height="50dp"
        android:background="@drawable/rectangle" />

    <RelativeLayout
        android:id="@+id/rlv2"
        android:layout_width="50dp"
        android:layout_height="50dp"  
        android:layout_below="@+id/rlv1"
        android:background="@drawable/triangle"
        android:rotation="180" />

enter image description here

根据需要设置边距。

Source

关于Android如何以编程方式创建三角形和矩形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22042603/

相关文章:

android - android 中的 GeoPoint 创建

android - 在 Android 应用程序中获取 Firebase 引用的最佳位置

java - 为 Vitamio 播放器创建自定义播放按钮

java - 使用网络资源进行货币转换器

android - 从包管理器中检索应用程序信息

android - GradientDrawable绘制不同颜色的setColor方法

android:shape ="line"不可见

android - 在Android上以编程方式绘制带有边框(角半径)的椭圆形

android - 使用 ShapeDrawable 获取 textView 背景颜色

android - 将 ShapeDrawable 中的单一(黑色)颜色替换为 Android 中的另一种颜色