美文网首页我爱编程
[Android] How to create an arrow

[Android] How to create an arrow

作者: Seven是为了纪念赛虎 | 来源:发表于2018-04-20 01:56 被阅读0次

CSDN:https://blog.csdn.net/ws10052797/article/details/80013134

一、思路

通过layer-list中添加item,每个item代表箭头的一条边,每条边通过<shape>设置形状,然后通过<rotate>旋转45度,形成一个直角箭头的形状。

二、实现

1. xml实现一个黑色的向右的箭头

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

    <item android:bottom="20dp">
        <rotate android:fromDegrees="-45">
            <shape>
                <solid android:color="@android:color/black" />
                <corners
                    android:bottomLeftRadius="0dp"
                    android:bottomRightRadius="0dp"
                    android:radius="1dp" />
            </shape>
        </rotate>
    </item>

    <item android:top="20dp">
        <rotate android:fromDegrees="44.5">
            <shape>
                <solid android:color="@android:color/black" />
                <corners
                    android:radius="1dp"
                    android:topLeftRadius="0dp"
                    android:topRightRadius="0dp" />
            </shape>
        </rotate>
    </item>
</layer-list>

其中第一个<item>是用来限制下面两个<item>的范围,是一条竖直的直线;
第二个<item>逆时针旋转45度,对应了箭头的上半部分,是一条直线;
第三个<item>顺时针旋转45度,对应了箭头的下半部分,也是一条直线;
为了使得两根直接相交并形成一个对角,对第二个<item>也就是上半部分设置属性android:bottom=20dp,对第三个<item>也就是下半部分设置属性android:top=20dp,这样就会发现出现了直角,但是贴合的不是非常严谨,所以微调一下旋转的角度,我选择把第三个<item>改成了44.5度。

效果图

2. 引用shape

设置固定的宽高

  <ImageView
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:src="@drawable/shape_arrow_right_black" />

三、知识点

<solid>
用于填充形状的纯色。
属性:

android:color
颜色。应用于形状的颜色,以十六进制值或颜色资源表示。

<corners>
为形状产生圆角。仅当形状为矩形时适用。
属性:
android:radius
尺寸。所有角的半径,以尺寸值或尺寸资源表示。对于每个角,这会被以下属性覆盖。
android:topLeftRadius
尺寸。左上角的半径,以尺寸值或尺寸资源表示。
android:topRightRadius
尺寸。右上角的半径,以尺寸值或尺寸资源表示。
android:bottomLeftRadius
尺寸。左下角的半径,以尺寸值或尺寸资源表示。
android:bottomRightRadius
尺寸。右下角的半径,以尺寸值或尺寸资源表示。
注:(最初)必须为每个角提供大于 1 的角半径,否则无法产生圆角。如果希望特定角不要倒圆角,解决方法是使用 android:radius 设置大于 1 的默认角半径,然后使用实际所需的值替换每个角,为不希望倒圆角的角提供零(“0dp”)。

<item>

android:top
整型。顶部偏移(像素)。
android:bottom
整型。底部偏移(像素)。

<rotate>

android:fromDegrees
Float. Starting angular position, in degrees.
浮点型。开始的角度位置,以度数为单位。

四、学习资料

1. 学习Layer List图层列表

首先学习layer-list的用法,根据Layer List的官方指南,知道<layer-list>是根元素,<item>是子元素。绘制的逻辑是:

Each drawable in the list is drawn in the order of the list—the last drawable in the list is drawn on top.
Each drawable is represented by an <item> element inside a single <layer-list> element.
列表中的每个可绘制对象按照列表的顺序绘制,列表中的最后一个可绘制对象绘于顶部。每个可绘制对象由单一<layer-list>元素内的<item>元素表示

<item>的说明和属性也可以在指南中查阅,其中需要理解的一句话:

All drawable items are scaled to fit the size of the containing View, by default. Thus, placing your images in a layer list at different positions might increase the size of the View and some images scale as appropriate.
默认情况下,所有可绘制项都会缩放以适应包括视图的大小。因此,将图像放在图层列表中的不同位置可能会增大视图的大小,并且有些图像会相应地缩放。

指南中提到避免缩放的方法,用<bitmap>+对不需要缩放的项目(例如"center")定义重力。

2. 学习Shape Drawable形状可绘制对象

官方指南Shape Drawable,可以查阅到<shape>这个元素的各种属性。

3. 学习Animation Resources中的<rotate>元素

官方指南Tween animation中可以查阅<rotate>的属性。

五、总结

一顿操作学习了一些知识,然并卵,终极解决方案或许是切个图。

六、延伸


参考资料

[1]: how to create a right facing arrow (chevron) using XML shapes in android?


有任何问题,欢迎留言反馈与建议。

相关文章

网友评论

    本文标题:[Android] How to create an arrow

    本文链接:https://www.haomeiwen.com/subject/xkinkftx.html