创建ToggleButton的一些建议

3

我需要创建一个像这个一样的ToggleButton (应该在任何设备上调整大小)。

然后在ViewGrouponLayout方法中对其进行布局。

你能给我提供如何创建它的任何想法吗?

enter image description here


我尝试以以下方式实现:

buttons.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"/>

menu_button_selector.xml

<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>

round_button_active.xml

我有一些背景知识,只需要旋转它即可。因为按钮应该位于已知的角度:0、45、90等。

<?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 方法中,我对其进行布局:
final View button = parentLayout.getChildAt(i);
button.getBackground().setLevel(i);
button.setOnClickListener(this);
button.layout(x1, y1, x2, y2);

我收到的内容:

enter image description here

在0度、90度、180度等方向上,按钮的样子看起来很清晰。

但是,在45度、135度等方向上,按钮被矩形画布截断了。即使我旋转它,我也只能在矩形画布范围内进行。


我会首先创建自己的自定义ViewGroup,然后创建一个自定义的ToggleButton。 - Tobrun
是的,你说得对,抱歉,我花了很多时间来创建它(一直都是错误的),这就是为什么我会这样回答你的原因 ;) - vetalitet
祝你好运!因为在Android上创建自定义组件并不是一件容易的事情。 - Tobrun
请告诉我是否正确理解了您的要求。红色圆圈应该从左上方移动到右上方,单击后沿着由两个黑色圆周界定的半圆路径行进/穿过。 - Kaustuv
@Kaustuv,看起来没错,这应该是一个按钮(具有像ToggleButton一样的所有事件),在半圆之间的任何位置都具有相同的视图。我更新了所需的图像。 - vetalitet
显示剩余2条评论
1个回答

0

据我所了解,使用图像实现此目的的一种可能方法如下:

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

红色圆形缩略图也是一个图像,其大小与上述设置相同,除了红色部分外,所有区域都是透明的。将其放入另一个图像视图中

现在将两个图像视图放在彼此上方,红色图像视图在顶部。向红色图像视图添加单击侦听器,并使用imageView.setRotate(degree)旋转它

为了给人以旋转运动的感觉,只需在非常短的时间间隔后调用上述方法,并每次增加degree的值即可。

我添加了一张图片以更好地理解。蓝色边框是黑色圆形图像视图的边界,绿色边框是红色圆形图像视图的边界。两者有一个共同的中心,因此当上面的图像视图旋转时,它将呈现出红色圆形沿着两个黑色圆形之间的路径移动的外观。

two image views one above the other


非常感谢你的回复。但是是否有可能创建一个这样的按钮背景(使用形状,而不是图片)并具有不同的尺寸?因为它必须适用于具有不同分辨率的设备。问题在于按钮的画布是一个矩形。当我旋转按钮的背景时,角度会变得明显可见。 - vetalitet
为什么要使用按钮?使用图像视图并在其上设置点击侦听器。用户不会注意到区别。在一个图像视图中,您可以设置两个可绘制对象,一个用于背景,一个用于src。这些将形成您的两个黑色圆圈。对于红色圆圈,请像我在上面的答案中提到的那样使用另一个图像视图。是的,您可以在xml中绘制圆形并放置在drawable文件夹中。对于不同的圆圈,请使用不同的xml。请注意,绿色和蓝色边界只是图像视图大小和形状的指示器,它们不会可见。 - Kaustuv

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接