利用Radiogroup Radiobutton 实现滑动效果菜单

2023-05-16

第一次在满世界大侠的地方撰写博客,所以不免紧张,怕自己写出让人消掉大牙的文章。本着学习的态度,最后我还是决定把自己的学习感想记录下来。

首先我要感谢一个哥们,大部分的内容都是他的杰作,我只是稍作修改,我看到的地方时http://www.myexception.cn/mobile/418248.html,大家可以看看原创的代码。

效果图:

这里是“1111”,“2222”,“3333”是三个Radiobutton,当前如果是“1111”,点击“2222”的时候,背景的蓝色背景框会从左移动到“2222”这个地方。这里主要是重写了Radiogroup,代码如下,在此感谢原作者:


public class NewsRadioGroup extends RadioGroup implements
        OnCheckedChangeListener {

    private final Transformation mTransformation = new Transformation();
    private Animation mAnimation;
    private OnCheckedChangeListener mOnCheckedChangeListener;
    private int mLastCheckedId = -1;
    private Drawable mDummy;
    private Drawable mDrawableNormal, mDrawableChecked;
    private boolean mAminDoing = false;

    public NewsRadioGroup(Context context) {
        super(context);
        init();
    }

    public NewsRadioGroup(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        super.setOnCheckedChangeListener(this);
        mLastCheckedId = super.getCheckedRadioButtonId();
        mDummy = getResources().getDrawable(R.drawable.rb_checked);
        mDrawableNormal = getResources().getDrawable(
                android.R.color.transparent);        
    }

    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (mLastCheckedId != -1) {
            doAmin(checkedId);
        } else {
            mLastCheckedId = checkedId;
        }
        if (mOnCheckedChangeListener != null) {
            mOnCheckedChangeListener.onCheckedChanged(group, checkedId);
        }
    }

    private void doAmin(int checkedId) {
        RadioButton rbCurChecked = (RadioButton) super.findViewById(checkedId), rbLastChecked = (RadioButton) super
                .findViewById(mLastCheckedId);
        if (rbCurChecked == null || rbLastChecked == null) {
            mLastCheckedId = checkedId;
            return;
        }
        int X1 = rbLastChecked.getLeft(), X2 = rbCurChecked.getLeft();
        if (X1 <= 0 && X2 <= 0) {
            mLastCheckedId = checkedId;
            return;
        }

        if (mDrawableChecked == null) {
            mDrawableChecked = rbCurChecked.getBackground();
            mDummy.setBounds(0, 0, rbCurChecked.getWidth(),
                    rbCurChecked.getHeight());
        }
        rbCurChecked.setBackgroundDrawable(mDrawableNormal);
//        rbCurChecked.setBackgroundResource(R.drawable.rb_checked);

        if (mAminDoing && mAnimation != null) {
            mAnimation.reset();
        }
        mAnimation = new TranslateAnimation(X1, X2, rbCurChecked.getTop(),
                rbCurChecked.getTop());
        mAnimation.setDuration(500);
        mAnimation.initialize(0, 0, 0, 0);
        mAminDoing = true;
        mAnimation.startNow();
        invalidate();
    }

    @Override
    public void setOnCheckedChangeListener(OnCheckedChangeListener listener) {
        mOnCheckedChangeListener = listener;
    }

    protected void onDraw(Canvas canvas) {
        if (mAnimation == null || !mAminDoing) {
            return;
        }
        if (!mAnimation.hasEnded()) {
            int sc = canvas.save();
            mAnimation.getTransformation(
                    AnimationUtils.currentAnimationTimeMillis(),
                    mTransformation);
            canvas.concat(mTransformation.getMatrix());
            mDummy.draw(canvas);
            canvas.restoreToCount(sc);
//            ((RadioButton)findViewById(getCheckedRadioButtonId())).setBackgroundResource(R.color.transparent);
            invalidate();
//            setReallyCheck();
        } else {
            mAminDoing = false;
            setReallyCheck();
//            ((RadioButton)findViewById(getCheckedRadioButtonId())).setBackgroundResource(R.drawable.sl_tab);
        }
    }

    private void setReallyCheck() {
        if (mLastCheckedId != -1) {
            super.findViewById(mLastCheckedId).setBackgroundDrawable(
                    mDrawableNormal);
        }

        mLastCheckedId = super.getCheckedRadioButtonId();
        if (mLastCheckedId != -1) {
            super.findViewById(mLastCheckedId).setBackgroundDrawable(
                    mDrawableChecked);
        }
    }
}// end class NesRadioGroup 


这里初始化init()方法中,第一步获得三个变量mLastCheckedId,mDummy,mDrawableNormal,分别是当前选中id和两个Drawable,这里的RB_checked是用9pach做的图片,这里最重要的是三点。第一点,在onCheckedChanged中提前捕捉事件,然后启动动画效果,产生滑动,在动画结束后在运行自己的onCheckedChanged方法,这里文字的颜色就是动画之后处理的。第二点在doAmin中设置动画。第三点使用setReallyCheck()方法。

资源文件:

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/topbar_select" android:state_enabled="true" android:state_focused="true" android:state_pressed="false"/>
   <!--   <item android:drawable="@drawable/topbar_select" android:state_enabled="true" android:state_pressed="true"/> -->
    <item android:drawable="@drawable/topbar_select" android:state_checked="true" android:state_enabled="true"/>
    <item android:drawable="@drawable/transparent"/>

</selector>

这里是一个selector,每一个Radiobutton做以它作为背景,看下我注释的那行,如果不注释,会怎么样,大家可以试试。这里的topbar_select,就是前边的rb_checked图片。

以下是layout资源文件:

    <com.app.view.NewsRadioGroup
        android:id="@+id/rg"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:background="@drawable/sl_n"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:visibility="visible" >

        <RadioButton
            android:id="@+id/rb1"
            style="@style/sl_style"
            android:checked="true" />

        <RadioButton
            android:id="@+id/rb2"
            style="@style/sl_style" />

        <RadioButton
            android:id="@+id/rb3"
            style="@style/sl_style" />
     </com.app.view.NewsRadioGroup>

大家可以稍微改一改,例如滑动过程中的背景具有一定的透明效果等等。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

利用Radiogroup Radiobutton 实现滑动效果菜单 的相关文章

随机推荐

  • 针对初学者的android Unresolved reference: *

    本文主要是针对Android初学者出现的android Unresolved reference xml代码kt的代码 xml代码 lt Button android id 61 34 64 43 id btn album 34 andro
  • python中跨平台文件操作

    众所周知 xff0c Windows下的路径分隔符为反斜杠 34 34 而UNIX like系统下的路径分隔符为正斜杠 34 34 xff0c 这常导致代码跨平台移植时的问题 Python设计为一门跨平台的语言 xff0c 当然可以轻松解决
  • Vxworks 学习(一)介绍

    Vxworks 学习 xff08 一 xff09 介绍 该系列文章是我根据多个博主以及相关书上内容整理的学习笔记 xff0c 许多内容非原创 实时操作系统 定义 实时操作系统 xff08 Real Time Operating System
  • 8-Cython依赖Visual Studio

    文章目录 前言一 vs 2015安装提示错误二 使用步骤1 下载安装vs高版本版本 二 环境配置三 测试模块编译安装四 测试Cython 前言 前面再crypto用于加解密时使用2005版本提供编译支撑 xff1b 最近2005编译环境安装
  • MongoDB的特点及概念

    MongoDB 的特点及概念 MongoDB 是一个介于关系数据库和非关系数据库之间的产品 xff0c 是非关系数据库当中功能最丰富 xff0c 最像关系数据库的 它是一个基于分布式文件存储的开源数据库系统 在高负载的情况下 xff0c 添
  • 【四足机器人】强化学习实现minitaur运动控制(决策模型篇)

    文章目录 模型概要1 状态 决策空间 xff08 略 xff09 2 奖励函数3 决策模型 模型概要 1 状态 决策空间 xff08 略 xff09 状态空间 xff1a roll xff08 X轴 xff09 pitch xff08 Y轴
  • 解决windows下安装Anaconda后python pip不可用的情况

    在windows系统下通过安装Anaconda的方式安装的python使用中发现不能再通过pip安装python包 只能通过conda install packname 的方法 xff0c 导致很多conda不支持的包无法安装 我遇到的事d
  • Spring-为什么要使用Spring?为什么要使用依赖注入(DI)?

    为什么要使用Spring xff1f 使用Spring框架最主要的原因是为了简化Java开发 xff08 大多数框架都是为了简化开发 xff09 xff0c 它帮我们封装了很多完善的功能 xff0c 而且Spring的生态圈非常的庞大 基于
  • Shell Limits设置问题导致用户不能登录

    故障现象 前几天 xff0c 突然间某数据库主机不能su切换到grid用户 发生故障的环境为 xff1a RHEL 6 7 xff0c ORACLE 11gR2 RAC xff0c 其中集群节点1发生此故障 xff0c 而节点2状态正常 故
  • shell脚本通过ftp获取文件

    shell脚本通过ftp获取文件 span class token comment usr bin bash span span class token comment T 1日期 span day 61 96 date span clas
  • 将EditPlus添加到右键菜单中

    将EditPlus添加到右键菜单中 一 以管理员权限打开打开Edit Plus 二 工具 gt 配置用户工具 三 点击常规选项选中左侧将EditPlus添加到右键快捷菜单中 四 选中一个文件 xff0c 右键就可以看到了
  • windows安装jdk

    windows安装jdk 一 xff1a 下载地址 xff0c 可下载自己需要的版本 https www oracle com technetwork java javase downloads jdk8 downloads 2133151
  • VMware共享本机网络

    VMware共享本机网络 一 设置桥接模式 xff1a 左上角菜单栏 gt 虚拟机 gt 设置 gt 网络适配器 xff08 如图操作 xff09 二 编辑虚拟网络 左上角菜单栏 gt 编辑 gt 虚拟网络编 xff08 如图操作 xff0
  • vim设置行号

    vim设置行号 方法一 xff1a 临时 或者 方法二 xff1a 当前用户永久 1 修改vim配置文件vimrc vim vimrc 输入 xff1a set number 或 set nu 保存退出 方法三 所有用户 1 vim etc
  • tomcat 配置https

    一 生成证书 1 使用jdk自带的keytool ext生成证书 xff0c 进入jdk下bin目录 xff1b 2 在路径栏输入cmd 回车打开dos命令窗口 xff0c 打开之后当前路径为jdk下bin目录 ps xff1a 也可直接w
  • Google http测试工具

    一 下载 xff1a 下载地址 xff1a https pan baidu com s 16mCI0QUn z0kNPX4yqGEWg 提取码 xff1a sgiz 二 配置 1 解压文件 2 在Google里配置插件 xff0c 或者叫扩
  • linux mysql 离线安装

    一 下载 1 官网地址 https dev mysql com downloads mysql 点击Archives 选择需要的版本 点击Download 进行下载 xff0c 如需要登录自行注册登录 将下载的安装包上传至linux系统 2
  • cmd介绍及常用命令

    cmd介绍 cmd基本概念 cmd commander xff0c 命令提示符是在操作系统中 xff0c 提示进行命令输入的一种工作提示符 在不同的操作系统环境下 xff0c 命令提示符各不相同 在windows环境下 xff0c 命令行程
  • 计算机的发展史

    计算机的发展史 计算机的前身 1642年的时候 xff0c 一位19岁的法国小伙设计并制作了一台能自动进位的加减法计算装置 xff0c 一开始是只能算加法的 xff0c 所以叫 加法器 后来慢慢改良 xff0c 可以做加减乘除的四则运算 x
  • 利用Radiogroup Radiobutton 实现滑动效果菜单

    第一次在满世界大侠的地方撰写博客 xff0c 所以不免紧张 xff0c 怕自己写出让人消掉大牙的文章 本着学习的态度 xff0c 最后我还是决定把自己的学习感想记录下来 首先我要感谢一个哥们 xff0c 大部分的内容都是他的杰作 xff0c