一些建议来创建ToggleButton

时间:2014-06-25 09:49:18

标签: android togglebutton

我必须像这样创建ToggleButton(应该在任何设备上调整大小)。

然后在ViewGroup的{​​{1}}方法中进行布局。

您能否提供一些如何创建它的想法?

enter image description here


我试着这样做:

buttons.xml

onLayout

menu_button_selector.xml

<ToggleButton
        android:id="@id/add_favorites_button_id"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@drawable/menu_button_selector"
        android:drawableTop="@drawable/star"
        android:textOff=""
        android:textOn=""
        android:contentDescription="Add Favorites"/>

round_button_active.xml

我有一些背景,只是旋转它。因为按钮应该以已知的度数定位:0,45,90,......

<level-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:maxLevel="0">
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_checked="true">
            <rotate
                    android:fromDegrees="90"
                    android:toDegrees="90"
                    android:pivotX="50%"
                    android:pivotY="50%"
                    android:drawable="@drawable/round_button_active"/>
        </item>
        <item android:state_checked="false">
            <rotate
                    android:fromDegrees="90"
                    android:toDegrees="90"
                    android:pivotX="50%"
                    android:pivotY="50%"
                    android:drawable="@drawable/round_button"/>
        </item>

    </selector>
</item>
.....
</level-list>

然后在<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <corners android:topLeftRadius="50dp" android:topRightRadius="50dp"/> <solid android:color="@color/tb_red_button"/> <size android:width="60dp" android:height="60dp"/> </shape> </item> </layer-list> 方法中我布置它:

onLayout

我收到了什么:

enter image description here

在0,90,180等度数按钮看起来相当清楚它们应该是什么

on 45,135等度按钮由矩形画布切割。即使我旋转它,我也会在矩形画布的范围内进行旋转。

1 个答案:

答案 0 :(得分:0)

根据我的理解,实现这一目标的一种可能方式(使用图像)就像

将此处的图像视为半圆(两个黑色边框半圆),这是整圆的一部分。这个圆圈的上半部分是透明的。整个设置将是图像视图内的图像。

红色圆圈拇指也是与上述设置尺寸相同的图像,除红色部分外,所有区域都是透明的。把它放在另一个图像视图

现在将两个图像视图放在另一个上面,红色一个放在顶部。将单击侦听器添加到红色图像视图并使用imageView.setRotate(degree)

旋转它

为了给出像感觉一样的旋转运动,只需在很短的间隔后调用上面的方法并每次递增度数值。

我正在添加图片以便更好地理解。蓝色边框是黑色圆圈图像视图的边界,绿色边框是红色圆圈图像视图的边界。两者都有一个共同的中心,所以当上面的图像视图旋转时,它会在两个黑色圆圈之间的路径后面出现红色圆圈。

two image views one above the other

相关问题