Android 用基本形状画香烟

Android 用基本形状画香烟,android,android-drawable,Android,Android Drawable,我想画一支烟,烟灰视图是“可吸烟”视图的一个百分比。它需要是一个百分比,因为我将使用一个滑块来改变“灰烬”视图的百分比 以下是我想要的: 这是我的密码: <LinearLayout android:id="@+id/cigarette" android:layout_width="match_parent" android:layout_height="20dp" android:layout_marginBottom="20dp" android

我想画一支烟,烟灰视图是“可吸烟”视图的一个百分比。它需要是一个百分比,因为我将使用一个滑块来改变“灰烬”视图的百分比

以下是我想要的:

这是我的密码:

<LinearLayout
    android:id="@+id/cigarette"
    android:layout_width="match_parent"
    android:layout_height="20dp"
    android:layout_marginBottom="20dp"
    android:orientation="horizontal">

    <View
        android:id="@+id/butt"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight=".25"
        android:background="#ff7700" />

    <LinearLayout
        android:id="@+id/smokeable"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight=".75"
        android:background="#cccccc"
        android:orientation="horizontal">

        <View
            android:id="@+id/ash"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight=".33"
            android:background="#777777"
            android:gravity="right" />

    </LinearLayout>

</LinearLayout>


在本例中,我希望灰烬为可吸烟视图的33%。我不明白这为什么不起作用。任何帮助都将不胜感激。

鉴于所有三个元素都是一致的,您只需要一个
LinearLayout
parent:

<LinearLayout
    android:id="@+id/cigarette"
    android:layout_width="match_parent"
    android:layout_height="20dp"
    android:orientation="horizontal">

    <View
        android:id="@+id/butt"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="25"
        android:background="#ff7700" />

    <View
        android:id="@+id/smokeable"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="65"
        android:background="#cccccc" />

    <View
        android:id="@+id/ash"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="10"
        android:background="#777777" />

</LinearLayout>

鉴于所有三个元素都在一条直线上,您只需要一个
LinearLayout
父元素:

<LinearLayout
    android:id="@+id/cigarette"
    android:layout_width="match_parent"
    android:layout_height="20dp"
    android:orientation="horizontal">

    <View
        android:id="@+id/butt"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="25"
        android:background="#ff7700" />

    <View
        android:id="@+id/smokeable"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="65"
        android:background="#cccccc" />

    <View
        android:id="@+id/ash"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="10"
        android:background="#777777" />

</LinearLayout>

我认为最简单、最干净的方法是为你的“香烟”制作一个定制的
视图,它将由滤嘴/可吸烟/烟灰组成,每个部分都有不同的颜色

因此,在
attrs.xml

<resources>
    <declare-styleable name="CigaretteView">
        <attr name="filterColor" format="color" />
        <attr name="smokeableColor" format="color" />
        <attr name="ashColor" format="color" />
        <attr name="filterWidth" format="dimension" />
    </declare-styleable>
</resources>
您的xml布局将简化为

<com.example.CigaretteView
    android:id="@+id/cigaretteView"
    android:layout_width="match_parent"
    android:layout_height="48dp"
    app:filterWidth="48dp"
    app:filterColor="#ffbbaa00"
    app:smokeableColor="#ffffffff"
    app:ashColor="#ff888888"/>
如果
百分比
从0(香烟完好无损)变为1(香烟已完全吸干)。

我认为最简单的清洁方法是为您的“香烟”创建一个自定义
视图,它将由过滤器/可吸干/烟灰组成,每个部分都有不同的颜色

因此,在
attrs.xml

<resources>
    <declare-styleable name="CigaretteView">
        <attr name="filterColor" format="color" />
        <attr name="smokeableColor" format="color" />
        <attr name="ashColor" format="color" />
        <attr name="filterWidth" format="dimension" />
    </declare-styleable>
</resources>
您的xml布局将简化为

<com.example.CigaretteView
    android:id="@+id/cigaretteView"
    android:layout_width="match_parent"
    android:layout_height="48dp"
    app:filterWidth="48dp"
    app:filterColor="#ffbbaa00"
    app:smokeableColor="#ffffffff"
    app:ashColor="#ff888888"/>

其中,
百分比
从0(香烟完好无损)变为1(香烟已完全吸干)。

内部的
线性布局
需要另一个子视图
来查看香烟的未消费部分。在您的示例中,其
layout_权重必须为1-0.33=0.67(两个兄弟的权重之和始终必须为1)


请注意,虽然这将修复代码,但“嵌套加权线性布局”方法并不是一个好方法,即使现代设备不应该遇到性能问题


包含自定义
视图
的视图更好,因为您只需要一个
视图
,而不是两个
视图组
s加上三个
视图
s。您有一种方法可以轻松更改百分比,而且所有计算和绘图部分都被很好地封装

内部
线性布局
需要另一个子视图
来查看香烟尚未消费的部分。在您的示例中,其
layout_权重必须为1-0.33=0.67(两个兄弟的权重之和始终必须为1)


请注意,虽然这将修复代码,但“嵌套加权线性布局”方法并不是一个好方法,即使现代设备不应该遇到性能问题


包含自定义
视图
的视图更好,因为您只需要一个
视图
,而不是两个
视图组
s加上三个
视图
s。您有一种方法可以轻松更改百分比,而且所有计算和绘图部分都被很好地封装

这里是我能想到的最简单的事情,它能满足您的需要:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="20dp"
    android:background="#777777"
    android:orientation="horizontal">

    <View
        android:id="@+id/butt"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.25"
        android:background="#ff7700"/>

    <View
        android:id="@+id/smokeable"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.75"
        android:background="#cccccc"
        android:transformPivotX="0px"/>

</LinearLayout>


现在,您可以使用任意百分比调用
smokeable.setScaleX()
,以获得所需的结果。

以下是我能想到的最简单的方法,可以满足您的需要:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="20dp"
    android:background="#777777"
    android:orientation="horizontal">

    <View
        android:id="@+id/butt"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.25"
        android:background="#ff7700"/>

    <View
        android:id="@+id/smokeable"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.75"
        android:background="#cccccc"
        android:transformPivotX="0px"/>

</LinearLayout>


现在,您可以使用任意百分比调用
smokeable.setScaleX()
,以获得所需的结果。

因为我将使用一个滑块,用户可以在其中选择吸烟量。滑块值的范围可以是1/4、1/3、1/2等。当用户更改滑块时,它将自动更改视图中的烟灰宽度。因为我将使用一个滑块,用户可以在其中选择吸烟量。滑块值的范围可以是1/4、1/3、1/2等。当他们更改滑块时,它将自动更改视图中的灰烬宽度。我希望灰烬占可吸烟视图的百分比,以便可以通过滑块以编程方式轻松更改。我希望灰烬占可吸烟视图的百分比,这可能不是“最简单”的方法,但我非常喜欢:)我的意思是这是最简单的方法,因为使用虚拟
视图
s对我来说似乎不是一个可行的选项,它会在代码审查中被忽略,无论如何,谢谢:)这可能不是“最简单”但是我非常喜欢:)我的意思是这是最简单的方法,因为使用dummy
View
s对我来说不是一个可行的选择,它会在代码审查中被忽略,无论如何,谢谢:)