自定义Android切换轨道“动画” [英] Custom Android switch track 'animation'

查看:166
本文介绍了自定义Android切换轨道“动画”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've created a basic custom Switch, as defined below.

<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可以正常工作。

The @drawable/thumb is a simple PNG which works fine.

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

The @drawable/switch_track is defined below. @drawable/trackon and @drawable/trackoff are PNG's.

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

此开关的外观和工作大部分都按预期进行,但是有某种方式可以动画化拇指在用户拖动时在轨道上移动?在选中和未选中之间淡入淡出,或者最好在拇指的后面改变。

This switch looks and works as intended for the most part, but is there some way to 'animate' the track as the thumb travels over it on user drag? Either fade between checked and unchecked, or preferably change 'behind' the thumb.

当前行为如下所示。

推荐答案

曾经有一段时间我也在寻找相同的东西需要类似于本机iOS的切换按钮功能,可以将其拖到我的项目之一的开/关中。那时我努力搜索,发现了这个库。

There was a time I was also looking for the same when I needed a toggle button functionality similar to native iOS which can be dragged to on/off for one of my projects. At that time I searched hard and I found this library.

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

所以希望这是您也正在寻找什么。

So hope this is what you are looking for too.

这篇关于自定义Android切换轨道“动画”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆