自定义Android开关轨道“动画”

8

我创建了一个基本的自定义 Switch,如下所定义。

<Switch
        android:id="@+id/availSwitch"
        android:layout_width="wrap_content"
        android:switchMinWidth="110dp"
        android:layout_height="wrap_content"
        android:track="@drawable/switch_track"
        android:thumb="@drawable/thumb"/>

@drawable/thumb 是一个简单的PNG图片,使用效果很好。

@drawable/switch_track 定义如下。 @drawable/trackon@drawable/trackoff 是PNG图片。

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="false" android:drawable="@drawable/trackoff" />
    <item android:state_checked="true"  android:drawable="@drawable/trackon" />
    <item                               android:drawable="@drawable/trackoff" />

</selector>

这个开关在大部分情况下看起来和使用起来都是预期的,但是有没有一种方法可以在用户拖动时“动画”轨道,使其更加生动?可以在选中和未选中之间淡入淡出,或者更好的是在拇指“后面”进行更改。

当前行为如下所示。

Current behaviour


1
最好使用自定义的SeekBar,或者使用Toggle。 - Aditya Vyas-Lakhan
https://dev59.com/IWkw5IYBdhLWcg3woMFD - IntelliJ Amiya
https://github.com/tuesda/SwitchBox - IntelliJ Amiya
@Lakhan,这是一个有趣的想法,将最大值设置为1并设置一个onClick监听器。虽然https://github.com/pellucide/Android-Switch-Demo-pre-4.0也能顺利完成任务,但你的想法很好。 - BlitzKraig
@BlitzKraig 很高兴能帮助你。 - Aditya Vyas-Lakhan
3个回答

5

曾经有一段时间,当我需要一个类似于原生iOS的开关按钮功能,可以拖动到开/关状态时,我也在寻找同样的东西,用于我的一个项目。那时候我很努力地搜索,最终找到了这个库。

https://github.com/pellucide/Android-Switch-Demo-pre-4.0

希望这也是您在寻找的。


这看起来就是我想要的东西,真惊讶在搜索与安卓开关相关的内容时有多么困难...谢谢,我会去试试! - BlitzKraig
玩了一下这个库,成功地实现了一个不错的效果。有点麻烦,特别是在使用较新的API版本时,不得不添加一些巧妙的代码来使其运行良好。但它很好地完成了任务,非常感谢你的帮助。 - BlitzKraig
是的,定制有点棘手,但很高兴它对你有用 :) - Mukesh Rana

1

Switch 是一个两状态的切换开关小部件,可以在两个选项之间进行选择。用户可以拖动“拇指”来选择所选选项,或者只需轻触即可像复选框一样切换。text 属性控制开关标签中显示的文本,而 off 和 on 文本控制拇指上的文本。

为此要求,您需要自定义您的 Switch 按钮功能。

您可以访问以下演示:

  1. Android 幻灯片切换

  2. Android-Switch-Demo-pre-4.0

  3. Android 应用程序自定义切换按钮

您需要理解以下两行。

        android:thumb="@drawable/customswitchselector"
        android:track="@drawable/custom_track"

它有两个标签:android:thumbandroid:track。当我们滑动或更改状态时,Thumb将绘制实际外观。

0

你需要自定义切换按钮

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/darkGray"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.gisinc.androidexamples.myapplication.MyActivity">

    <com.gisinc.androidexamples.androidtogglebutton.SettingsToggle
        xmlns:widget="http://schemas.android.com/apk/res-auto"
        android:id="@+id/settings1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        widget:prefName="useMyLocation"
        widget:text="Use My Location" />

</RelativeLayout>

请查看this


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