toggbutton

简介: 2013年8月14日Android记录 很多应用都会有用户设置,用户的一些偏好可以由用户来决定那是应用人性化的体现,在实际开发中很多情况都作成可配置的了,本篇博客要介绍的是一个比较炫的状态按钮切换,我想很多开发者都想做出这样的效果,在这里我也就把自己参与的项目当中的这部分实现,做出Demo来于朋友们分享。 没有图,我感觉就特别不舒服:
2013年8月14日Android记录

很多应用都会有用户设置,用户的一些偏好可以由用户来决定那是应用人性化的体现,在实际开发中很多情况都作成可配置的了,本篇博客要介绍的是一个比较炫的状态按钮切换,我想很多开发者都想做出这样的效果,在这里我也就把自己参与的项目当中的这部分实现,做出Demo来于朋友们分享。

没有图,我感觉就特别不舒服:


这样看没办法看出效果,如果能做出动态图就好了,下次吧。

除了ToggleButton的自定义之外,用户配置的信息也是要保存起来的,每一次启动程序的时候要能保证使用的是之前的配置,而不是默认配置,在这里使用SharedPreferences是最好的选择了。

想要源码的猛戳这里:http://download.csdn.net/detail/wwj_748/5945829


布局文件:
/2013.08.14_ToggleButton_demo/res/layout/settings.xml
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:id="@+id/LinearLayout1"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:background="@drawable/bg"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <RelativeLayout  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:background="@drawable/banner_bg" >  
  12.   
  13.         <TextView  
  14.             android:id="@+id/tv_Title"  
  15.             android:layout_width="wrap_content"  
  16.             android:layout_height="42dp"  
  17.             android:layout_centerHorizontal="true"  
  18.             android:gravity="center"  
  19.             android:text="设置"  
  20.             android:textColor="#ffffff"  
  21.             android:textSize="22sp" />  
  22.     </RelativeLayout>  
  23.   
  24.     <ScrollView  
  25.         android:layout_width="match_parent"  
  26.         android:layout_height="match_parent" >  
  27.   
  28.         <LinearLayout  
  29.             android:layout_width="match_parent"  
  30.             android:layout_height="wrap_content"  
  31.             android:orientation="vertical" >  
  32.   
  33.             <LinearLayout  
  34.                 android:id="@+id/layout_AutoPlay"  
  35.                 android:layout_width="match_parent"  
  36.                 android:layout_height="wrap_content"  
  37.                 android:background="@drawable/item_short_bg_selector"  
  38.                 android:gravity="center_vertical" >  
  39.   
  40.                 <TextView  
  41.                     android:id="@+id/tv_AutoPlay"  
  42.                     android:layout_width="0dp"  
  43.                     android:layout_height="wrap_content"  
  44.                     android:layout_marginLeft="10dp"  
  45.                     android:layout_weight="1"  
  46.                     android:focusable="false"  
  47.                     android:singleLine="true"  
  48.                     android:text="自动播放"  
  49.                     android:textColor="#7a6f66"  
  50.                     android:textSize="18sp" />  
  51.   
  52.                 <RelativeLayout  
  53.                     android:layout_width="wrap_content"  
  54.                     android:layout_height="wrap_content"  
  55.                     android:layout_marginRight="10dp" >  
  56.   
  57.                     <ToggleButton  
  58.                         android:id="@+id/toggle_AutoPlay"  
  59.                         android:layout_width="wrap_content"  
  60.                         android:layout_height="wrap_content"  
  61.                         android:background="@drawable/toggle_selector"  
  62.                         android:gravity="left|center_vertical"  
  63.                         android:paddingLeft="14dp"  
  64.                         android:paddingRight="14dp"  
  65.                         android:textColor="#ffffff"  
  66.                         android:textOff="OFF"  
  67.                         android:textOn="ON" />  
  68.   
  69.                     <ImageButton  
  70.                         android:id="@+id/toggleButton_AutoPlay"  
  71.                         android:layout_width="wrap_content"  
  72.                         android:layout_height="wrap_content"  
  73.                         android:layout_alignRight="@+id/toggle_AutoPlay"  
  74.                         android:background="#00000000"  
  75.                         android:src="@drawable/progress_thumb_selector" />  
  76.                 </RelativeLayout>  
  77.             </LinearLayout>  
  78.   
  79.             <ImageView  
  80.                 android:layout_width="match_parent"  
  81.                 android:layout_height="wrap_content"  
  82.                 android:scaleType="fitXY"  
  83.                 android:src="@drawable/list_divider" />  
  84.   
  85.             <LinearLayout  
  86.                 android:id="@+id/layout_StartOnBoot"  
  87.                 android:layout_width="match_parent"  
  88.                 android:layout_height="wrap_content"  
  89.                 android:background="@drawable/item_short_bg_selector"  
  90.                 android:gravity="center_vertical" >  
  91.   
  92.                 <TextView  
  93.                     android:id="@+id/tv_StartOnBoot"  
  94.                     android:layout_width="0dp"  
  95.                     android:layout_height="wrap_content"  
  96.                     android:layout_marginLeft="10dp"  
  97.                     android:layout_weight="1"  
  98.                     android:focusable="false"  
  99.                     android:singleLine="true"  
  100.                     android:text="开机自启动"  
  101.                     android:textColor="#7a6f66"  
  102.                     android:textSize="18sp" />  
  103.   
  104.                 <RelativeLayout  
  105.                     android:layout_width="wrap_content"  
  106.                     android:layout_height="wrap_content"  
  107.                     android:layout_marginRight="10dp" >  
  108.   
  109.                     <ToggleButton  
  110.                         android:id="@+id/toggle_StartOnBoot"  
  111.                         android:layout_width="wrap_content"  
  112.                         android:layout_height="wrap_content"  
  113.                         android:background="@drawable/toggle_selector"  
  114.                         android:gravity="left|center_vertical"  
  115.                         android:paddingLeft="14dp"  
  116.                         android:paddingRight="14dp"  
  117.                         android:textColor="#ffffff"  
  118.                         android:textOff="OFF"  
  119.                         android:textOn="ON" />  
  120.   
  121.                     <ImageButton  
  122.                         android:id="@+id/toggleButton_StartOnBoot"  
  123.                         android:layout_width="wrap_content"  
  124.                         android:layout_height="wrap_content"  
  125.                         android:layout_alignRight="@+id/toggle_StartOnBoot"  
  126.                         android:background="#00000000"  
  127.                         android:src="@drawable/progress_thumb_selector" />  
  128.                 </RelativeLayout>  
  129.             </LinearLayout>  
  130.         </LinearLayout>  
  131.     </ScrollView>  
  132.   
  133. </LinearLayout>  


哪些selector文件的代码就不贴了,自己看源码吧


Activity文件
/2013.08.14_ToggleButton_demo/src/com/wwj/toggle/Setting.java
  1. package com.wwj.toggle;  
  2.   
  3.   
  4. import android.app.Activity;  
  5. import android.os.Bundle;  
  6. import android.view.Gravity;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.ImageButton;  
  10. import android.widget.LinearLayout;  
  11. import android.widget.RelativeLayout;  
  12. import android.widget.ToggleButton;  
  13.   
  14. /** 
  15.  * 自定义ToggleButton的例子 
  16.  *  
  17.  * @author wwj 2013年8月14 
  18.  */  
  19. public class Setting extends Activity {  
  20.   
  21.     private LinearLayout layout_AutoPlay;  
  22.     private LinearLayout layout_StartOnBoot;  
  23.     private ToggleButton toggle_AutoPlay;  
  24.     private ToggleButton toggle_StartOnBoot;  
  25.     private ImageButton toggleButton_AutoPlay;  
  26.     private ImageButton toggleButton_StartOnBoot;  
  27.   
  28.     @Override  
  29.     protected void onCreate(Bundle savedInstanceState) {  
  30.         super.onCreate(savedInstanceState);  
  31.         setContentView(R.layout.settings);  
  32.   
  33.         // 找到控件  
  34.         layout_AutoPlay = (LinearLayout) findViewById(R.id.layout_AutoPlay);  
  35.         layout_StartOnBoot = (LinearLayout) findViewById(R.id.layout_StartOnBoot);  
  36.         toggle_AutoPlay = (ToggleButton) findViewById(R.id.toggle_AutoPlay);  
  37.         toggle_StartOnBoot = (ToggleButton) findViewById(R.id.toggle_StartOnBoot);  
  38.         toggleButton_AutoPlay = (ImageButton) findViewById(R.id.toggleButton_AutoPlay);  
  39.         toggleButton_StartOnBoot = (ImageButton) findViewById(R.id.toggleButton_StartOnBoot);  
  40.   
  41.         initViews();  
  42.         setListeners();  
  43.     }  
  44.   
  45.     private void initViews() {  
  46.         // 是否自动播放,获取SharePerference保存的用户配置  
  47.         boolean isAutoPlay = SettingUtils.get(this, SettingUtils.AUTO_PLAY,  
  48.                 false);  
  49.         toggle_AutoPlay.setChecked(isAutoPlay);  
  50.         RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) toggleButton_AutoPlay  
  51.                 .getLayoutParams();  
  52.         if (isAutoPlay) { // 如果是自动播放  
  53.             // 调整位置  
  54.             params.addRule(RelativeLayout.ALIGN_RIGHT, -1);  
  55.             params.addRule(RelativeLayout.ALIGN_LEFT,  
  56.                     R.id.toggleButton_AutoPlay);  
  57.             toggleButton_AutoPlay.setLayoutParams(params);  
  58.             toggleButton_AutoPlay  
  59.                     .setImageResource(R.drawable.progress_thumb_selector);  
  60.             toggle_AutoPlay.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);  
  61.         } else {  
  62.             // 调整位置  
  63.             params.addRule(RelativeLayout.ALIGN_RIGHT, R.id.toggle_AutoPlay);  
  64.             params.addRule(RelativeLayout.ALIGN_LEFT, -1);  
  65.             toggleButton_AutoPlay.setLayoutParams(params);  
  66.             toggleButton_AutoPlay  
  67.                     .setImageResource(R.drawable.progress_thumb_off_selector);  
  68.             toggle_AutoPlay.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);  
  69.         }  
  70.   
  71.         boolean isAutostart = SettingUtils.get(this,  
  72.                 SettingUtils.IS_AUTO_START, true);  
  73.   
  74.         toggle_StartOnBoot.setChecked(isAutostart);  
  75.         RelativeLayout.LayoutParams params3 = (RelativeLayout.LayoutParams) toggleButton_StartOnBoot  
  76.                 .getLayoutParams();  
  77.         if (isAutostart) {  
  78.             // 调整位置  
  79.             params3.addRule(RelativeLayout.ALIGN_RIGHT, -1);  
  80.             params3.addRule(RelativeLayout.ALIGN_LEFT, R.id.toggle_StartOnBoot);  
  81.             toggleButton_StartOnBoot.setLayoutParams(params3);  
  82.             toggleButton_StartOnBoot  
  83.                     .setImageResource(R.drawable.progress_thumb_selector);  
  84.   
  85.             toggle_StartOnBoot.setGravity(Gravity.RIGHT  
  86.                     | Gravity.CENTER_VERTICAL);  
  87.         } else {  
  88.             // 调整位置  
  89.             params3.addRule(RelativeLayout.ALIGN_RIGHT, R.id.toggle_StartOnBoot);  
  90.             params3.addRule(RelativeLayout.ALIGN_LEFT, -1);  
  91.             toggleButton_StartOnBoot.setLayoutParams(params3);  
  92.             toggleButton_StartOnBoot  
  93.                     .setImageResource(R.drawable.progress_thumb_off_selector);  
  94.   
  95.             toggle_StartOnBoot.setGravity(Gravity.LEFT  
  96.                     | Gravity.CENTER_VERTICAL);  
  97.         }  
  98.     }  
  99.   
  100.     private void setListeners() {  
  101.         toggle_AutoPlay.setOnCheckedChangeListener(new ToggleListener(this,  
  102.                 "自动播放", toggle_AutoPlay, toggleButton_AutoPlay));  
  103.         toggle_StartOnBoot.setOnCheckedChangeListener(new ToggleListener(this,  
  104.                 "开机自启动", toggle_StartOnBoot, toggleButton_StartOnBoot));  
  105.   
  106.         // UI事件,按钮点击事件  
  107.         OnClickListener clickToToggleListener = new OnClickListener() {  
  108.   
  109.             @Override  
  110.             public void onClick(View v) {  
  111.                 toggle_AutoPlay.toggle();  
  112.             }  
  113.         };  
  114.   
  115.         toggleButton_AutoPlay.setOnClickListener(clickToToggleListener);  
  116.         layout_AutoPlay.setOnClickListener(clickToToggleListener);  
  117.   
  118.         // UI事件,按钮点击事件  
  119.         OnClickListener clickToToggleAutostartListener = new OnClickListener() {  
  120.             public void onClick(View v) {  
  121.                 toggle_StartOnBoot.toggle();  
  122.             }  
  123.         };  
  124.         toggleButton_StartOnBoot  
  125.                 .setOnClickListener(clickToToggleAutostartListener);  
  126.         layout_StartOnBoot  
  127.                 .setOnClickListener(clickToToggleAutostartListener);  
  128.     }  
  129.   
  130. }  

工具类:
/2013.08.14_ToggleButton_demo/src/com/wwj/toggle/SettingUtils.java
  1. package com.wwj.toggle;  
  2.   
  3. import android.content.Context;  
  4. import android.content.SharedPreferences;  
  5. import android.content.SharedPreferences.Editor;  
  6. import android.preference.PreferenceManager;  
  7.   
  8. public class SettingUtils {  
  9.     public static final String AUTO_PLAY = "auto_play"// 自动播放  
  10.     public static final String IS_AUTO_START = "is_auto_start"// 开机自启动  
  11.       
  12.       
  13.     /** 
  14.      * 获取配置 
  15.      * @param context 
  16.      * @param name 
  17.      * @param defaultValue 
  18.      * @return 
  19.      */  
  20.     public static boolean get(Context context, String name, boolean defaultValue) {  
  21.         final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);  
  22.         boolean value = prefs.getBoolean(name, defaultValue);  
  23.         return value;  
  24.     }  
  25.       
  26.     /** 
  27.      * 保存用户配置 
  28.      * @param context 
  29.      * @param name 
  30.      * @param value 
  31.      * @return 
  32.      */  
  33.     public static boolean set(Context context, String name, boolean value) {  
  34.         final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);  
  35.         Editor editor = prefs.edit();  
  36.         editor.putBoolean(name, value);  
  37.         return editor.commit(); //提交  
  38.     }  
  39. }  

/2013.08.14_ToggleButton_demo/src/com/wwj/toggle/DisplayUtils.java
  1. package com.wwj.toggle;  
  2.   
  3. import android.content.Context;  
  4.   
  5. public class DisplayUtils {  
  6.     public static int dip2px(Context context, float dpValue) {  
  7.         final float scale = context.getResources().getDisplayMetrics().density;  
  8.         return (int) (dpValue * scale + 0.5f);  
  9.     }  
  10.   
  11.     public static int px2dip(Context context, float pxValue) {  
  12.         final float scale = context.getResources().getDisplayMetrics().density;  
  13.         return (int) (pxValue / scale + 0.5f);  
  14.     }  
  15.   
  16.     public static int getScreenWidth(Context context) {  
  17.         return context.getResources().getDisplayMetrics().widthPixels;  
  18.     }  
  19.   
  20.     public static int getScreenHeight(Context context) {  
  21.         return context.getResources().getDisplayMetrics().heightPixels;  
  22.     }  
  23. }  


/2013.08.14_ToggleButton_demo/src/com/wwj/toggle/ToggleListener.java
  1. package com.wwj.toggle;  
  2.   
  3.   
  4. import android.content.Context;  
  5. import android.view.Gravity;  
  6. import android.view.animation.TranslateAnimation;  
  7. import android.widget.CompoundButton;  
  8. import android.widget.CompoundButton.OnCheckedChangeListener;  
  9. import android.widget.ImageButton;  
  10. import android.widget.RelativeLayout;  
  11. import android.widget.ToggleButton;  
  12.   
  13. /** 
  14.  * 状态按钮的监听事件 
  15.  *  
  16.  * @author wwj 
  17.  *  
  18.  */  
  19. public class ToggleListener implements OnCheckedChangeListener {  
  20.     private Context context;  
  21.     private String settingName;  
  22.     private ToggleButton toggle;  
  23.     private ImageButton toggle_Button;  
  24.   
  25.     public ToggleListener(Context context, String settingName,  
  26.             ToggleButton toggle, ImageButton toggle_Button) {  
  27.         this.context = context;  
  28.         this.settingName = settingName;  
  29.         this.toggle = toggle;  
  30.         this.toggle_Button = toggle_Button;  
  31.     }  
  32.   
  33.     @Override  
  34.     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {  
  35.         // 保存设置  
  36.         if ("自动播放".equals(settingName)) {  
  37.             SettingUtils.set(context, SettingUtils.AUTO_PLAY, isChecked);  
  38.         } else if ("开机自启动".equals(settingName)) {  
  39.             SettingUtils.set(context, SettingUtils.IS_AUTO_START, isChecked);  
  40.         }  
  41.         // 播放动画  
  42.         RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) toggle_Button  
  43.                 .getLayoutParams();  
  44.         if (isChecked) {  
  45.             // 调整位置  
  46.             params.addRule(RelativeLayout.ALIGN_RIGHT, -1);  
  47.             if ("自动播放".equals(settingName)) {  
  48.                 params.addRule(RelativeLayout.ALIGN_LEFT, R.id.toggle_AutoPlay);  
  49.             } else if ("开机自启动".equals(settingName)) {  
  50.                 params.addRule(RelativeLayout.ALIGN_LEFT,  
  51.                         R.id.toggle_StartOnBoot);  
  52.             }  
  53.             toggle_Button.setLayoutParams(params);  
  54.             toggle_Button.setImageResource(R.drawable.progress_thumb_selector);  
  55.             toggle.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);  
  56.             // 播放动画  
  57.             TranslateAnimation animation = new TranslateAnimation(  
  58.                     DisplayUtils.dip2px(context, 40), 000);  
  59.             animation.setDuration(200);  
  60.             toggle_Button.startAnimation(animation);  
  61.         } else {  
  62.             // 调整位置  
  63.             if ("自动播放".equals(settingName)) {  
  64.                 params.addRule(RelativeLayout.ALIGN_RIGHT, R.id.toggle_AutoPlay);  
  65.             } else if ("开机自启动".equals(settingName)) {  
  66.                 params.addRule(RelativeLayout.ALIGN_RIGHT,  
  67.                         R.id.toggle_StartOnBoot);  
  68.             }  
  69.             params.addRule(RelativeLayout.ALIGN_LEFT, -1);  
  70.             toggle_Button.setLayoutParams(params);  
  71.             toggle_Button  
  72.                     .setImageResource(R.drawable.progress_thumb_off_selector);  
  73.   
  74.             toggle.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);  
  75.             // 播放动画  
  76.             TranslateAnimation animation = new TranslateAnimation(  
  77.                     DisplayUtils.dip2px(context, -40), 000);  
  78.             animation.setDuration(200);  
  79.             toggle_Button.startAnimation(animation);  
  80.         }  
  81.     }  
  82.   

目录
相关文章
|
6天前
|
Web App开发 人工智能 架构师
备考神器!基于通义听悟和Xmind快速生成思维导图
使用阿里通义听悟与Xmind,高效创建思维导图。通义听悟支持PDF、Word等格式文档分析,可一键转为Xmind格式。视频分析需下载,支持最大6GB,可用通义听悟插件实现在线视频内容提取。B站部分视频自带AI总结,或使用特定网站直接在线解析视频生成MD文档。结合Xmind,优化学习和工作效率。
23524 13
|
18天前
|
缓存 运维 关系型数据库
数据库容灾 | MySQL MGR与阿里云PolarDB-X Paxos的深度对比
经过深入的技术剖析与性能对比,PolarDB-X DN凭借其自研的X-Paxos协议和一系列优化设计,在性能、正确性、可用性及资源开销等方面展现出对MySQL MGR的多项优势,但MGR在MySQL生态体系内也占据重要地位,但需要考虑备库宕机抖动、跨机房容灾性能波动、稳定性等各种情况,因此如果想用好MGR,必须配备专业的技术和运维团队的支持。 在面对大规模、高并发、高可用性需求时,PolarDB-X存储引擎以其独特的技术优势和优异的性能表现,相比于MGR在开箱即用的场景下,PolarDB-X基于DN的集中式(标准版)在功能和性能都做到了很好的平衡,成为了极具竞争力的数据库解决方案。
|
9天前
|
人工智能 自然语言处理 搜索推荐
解读阿里云搜索开发工作台如何快速搭建AI语义搜索及RAG链路
本文介绍阿里云搜索开发工作台如何通过内置数据处理、查询分析、排序、效果测评、大模型等服务,结合阿里云搜索引擎及开源引擎,灵活打造AI语义搜索及RAG链路。
19137 12
|
10天前
|
存储 弹性计算 监控
几百T的视频、图片数据如何更有效地存储和管理?
采用传统硬盘搭建存储方案,看起来成本低廉,但是再加上各种附加因素后却大幅攀升,而云存储厂商通常提供基于订阅的定价模型、一些免费服务和一定的折扣。现在,我们就来了解一下如何更省钱地使用云存储。
18892 26
几百T的视频、图片数据如何更有效地存储和管理?
|
11天前
|
Kubernetes Dubbo Cloud Native
将Dubbo应用部署到服务网格中
本文主要就Dubbo应用如何接入服务网格、获得各项云原生能力进行了探讨,并提出了最佳实践以及过渡两种实践场景。我们首先推荐您使用Dubbo社区提供的最佳实践场景来接入服务网格,在必要时可以通过过渡方案来向最佳实践方案逐步实现过渡。
19207 7
|
8天前
|
分布式计算 监控 Serverless
E-MapReduce Serverless Spark 版测评
E-MapReduce Serverless Spark 版测评
11520 9
|
9天前
|
弹性计算 Prometheus 监控
如何从自建开源 Prometheus 迁移到阿里云托管 Prometheus 服务
阿里云可观测监控 Prometheus 版提供高性能、高可用、全托管的监控服务,对接开源生态,支持 Kubernetes、ECS 等场景,解决了自建 Prometheus+Thanos 高成本、运维复杂的问题。本文讨论在各个典型场景下的迁移方案。
10286 12
|
22天前
|
监控 Kubernetes Go
全链路追踪 & 性能监控,GO 应用可观测全面升级
当前,大多数面向 Golang 应用的监控能力主要是通过 SDK 方式接入,需要开放人员手动进行埋点,会存在一定问题。对此,可观测 Go Agent 应运而生。本文介绍的阿里云可观测 Go Agent 方案,能通过无侵入的方式实现应用监控能力。
108114 20
|
8天前
|
机器学习/深度学习 算法 PyTorch
使用Pytorch中从头实现去噪扩散概率模型(DDPM)
在本文中,我们将构建基础的无条件扩散模型,即去噪扩散概率模型(DDPM)。从探究算法的直观工作原理开始,然后在PyTorch中从头构建它。本文主要关注算法背后的思想和具体实现细节。
8555 3
|
17天前
|
机器学习/深度学习 算法 开发工具
大语言模型的直接偏好优化(DPO)对齐在PAI-QuickStart实践
阿里云的人工智能平台PAI,作为一站式的机器学习和深度学习平台,对DPO算法提供了全面的技术支持。无论是开发者还是企业客户,都可以通过PAI-QuickStart轻松实现大语言模型的DPO对齐微调。本文以阿里云最近推出的开源大型语言模型Qwen2(通义千问2)系列为例,介绍如何在PAI-QuickStart实现Qwen2的DPO算法对齐微调。