当前位置: 移动技术网 > 移动技术>移动开发>Android > Android UI设计系列之自定义SwitchButton开关实现类似IOS中UISwitch的动画效果(2)

Android UI设计系列之自定义SwitchButton开关实现类似IOS中UISwitch的动画效果(2)

2019年07月24日  | 移动技术网移动技术  | 我要评论

做ios开发的都知道,ios提供了一个具有动态开关效果的uiswitch组件,这个组件很好用效果相对来说也很绚丽,当我们去点击开关的时候有动画效果,但遗憾的是android上并没有给我们提供类似的组件(听说在android4.0的版本上提供了具有动态效果的开关组件,不过我还没有去看文档),如果我们想实现类似的效果那该怎么办了呢?看来又得去自定义了。
公司的产品最近一直在做升级,主要做的就是把界面做的更绚丽更美观给用户更好的体验(唉,顾客是上帝......),其中的设置功能中就有开关按钮,原来的开关做的是两幅图片,通过点击图片来给开关设置不同的状态图片,但是这种效果很死板和程序的整体风格不太协调,于是就想着实现类似于ios中的开关效果。
拿着笔在图纸上画了画,我实现的原理也是采用了两幅图片,一个整体的背景图:和一个小滑块图:,用小滑块图实现在背景图上滑动,当小滑块滑到左边时恰好遮挡了开字,就是显示的关了,同样原理当小滑块滑动到右侧时恰好遮挡了关字也就是现实开了,滑动的实现主要用的就是translateanimation类,如有对translateanimation不太熟悉的,问问度娘,她那有关translateanimation的解说多了去了,毕竟自己动手,丰衣食足嘛,(*^__^*) 嘻嘻……
好了,老规矩来看一下项目结构吧:


工程中switch_button.xml文件就是对应的switchbutton的布局文件,内容不需要解释,你一看就懂

<?xml version="1.0" encoding="utf-8"?> 
<linearlayout 
 xmlns:android="http://schemas.android.com/apk/res/android" 
 android:id="@+id/switch_parent" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:background="@drawable/switch_bg"> 
 
 <imageview 
  android:id="@+id/switch_button" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:src="@drawable/switch_btn" /> 
 
</linearlayout> 

switchbutton的布局文件中根节点是个linearlayout,把它的背景设置成了一个含有开关文字的图片,里边包含一个imageview,这个imageview就是用来在linearlayout中进行滑动的。
其中自定义开关组件就是都在wedgit包下的switchbutton,那么赶紧来看一下switchbutton的代码吧

public class switchbutton extends linearlayout { 
 
 /** 
  * 开关图片 
  */ 
 private linearlayout switchparent; 
 /** 
  * 滑块图片 
  */ 
 private imageview switchbutton; 
 /** 
  * 按钮状态,默认关闭 
  */ 
 private boolean ison = false; 
 /** 
  * 滑块需要滑动的距离 
  */ 
 private int scrolldistance; 
 /** 
  * 开关按钮监听器 
  */ 
 private switchchangedlistner listner; 
 
 public switchbutton(context context) { 
  super(context); 
  initwedgits(context); 
 } 
 
 public switchbutton(context context, attributeset attrs) { 
  super(context, attrs); 
  initwedgits(context); 
 } 
 
 /** 
  * 初始化组件 
  * 
  * @param context 
  *   上下文环境 
  */ 
 private void initwedgits(context context) { 
  try { 
   view view = layoutinflater.from(context).inflate( 
     r.layout.switch_button, this); 
   switchparent = (linearlayout) view.findviewbyid(r.id.switch_parent); 
   switchbutton = (imageview) view.findviewbyid(r.id.switch_button); 
   addlisteners(); 
  } catch (exception e) { 
   e.printstacktrace(); 
  } 
 } 
 
 /** 
  * 添加事件监听器 
  */ 
 private void addlisteners() { 
  try { 
   switchparent.setonclicklistener(new onclicklistener() { 
    @override 
    public void onclick(view v) { 
     ison = !ison; 
     scrollswitch(); 
     if (null != listner) { 
      // 开关开发或者关闭的回调方法 
      listner.switchchanged(getid(), ison); 
     } 
    } 
   }); 
  } catch (exception e) { 
   e.printstacktrace(); 
  } 
 } 
 
 /** 
  * 滑动开关 
  */ 
 private void scrollswitch() { 
  // 获取滑块需要滑动的距离,滑动距离等于父组建的宽度减去滑块的宽度 
  scrolldistance = switchparent.getwidth() - switchbutton.getwidth(); 
  // 初始化滑动事件 
  animation animation = null; 
  if (ison) { 
   animation = new translateanimation(0, scrolldistance, 0, 0); 
  } else { 
   animation = new translateanimation(scrolldistance, 0, 0, 0); 
  } 
  // 设置滑动时间 
  animation.setduration(200); 
  // 滑动之后保持状态 
  animation.setfillafter(true); 
  // 开始滑动 
  switchbutton.startanimation(animation); 
 } 
 
 /** 
  * 获取开关状态 
  * 
  * @return 【true:打开】【false:关闭】 
  */ 
 public boolean ison() { 
  return ison; 
 } 
 
 /** 
  * 设置开关状态 
  * 
  * @param ison 
  *   开关状态【true:打开】【false:关闭】 
  */ 
 public void seton(boolean ison) { 
  if (this.ison == ison) { 
   return; 
  } 
  this.ison = ison; 
  post(new runnable() { 
   @override 
   public void run() { 
    scrollswitch(); 
   } 
  }); 
 } 
 
 /** 
  * 设置开关状态监听器 
  * 
  * @param listner 
  *   开关状态监听器 
  */ 
 public void setonswitchlistner(switchchangedlistner listner) { 
  this.listner = listner; 
 } 
 
 /** 
  * 开关状态监听器 
  * 
  * @author llew 
  * 
  */ 
 public interface switchchangedlistner { 
  /** 
   * 开关状态改变 
   * 
   * @param viewid 
   *   当前开关id 
   * @param ison 
   *   开关是否打开【true:打开】【false:关闭】 
   */ 
  public void switchchanged(integer viewid, boolean ison); 
 } 
} 

switchbutton的实现也很简单,首先是初始化组件initwedgits(),然后添加事件监听器addlisteners(),在监听器中做逻辑判断,代码都有注释,就不再详细说明了
那么到了最后,我们来看看mainactivity中对switchbutton的用法吧

public class mainactivity extends activity { 
  
 private switchbutton switchbutton; 
  
 /** called when the activity is first created. */ 
 @override 
 public void oncreate(bundle savedinstancestate) { 
  super.oncreate(savedinstancestate); 
  setcontentview(r.layout.main); 
  initwedgits(); 
 } 
 
 /** 
  * 初始化各组件 
  */ 
 private void initwedgits() { 
  try { 
   switchbutton = (switchbutton) findviewbyid(r.id.switchbutton); 
   // switchbutton.seton(false); 
   switchbutton.seton(true); 
   addlisteners(); 
  } catch (exception e) { 
   e.printstacktrace(); 
  } 
 } 
  
 /** 
  * 添加事件监听器 
  */ 
 private void addlisteners() { 
  try { 
   switchbutton.setonswitchlistner(new switchchangedlistner() { 
    @override 
    public void switchchanged(integer viewid, boolean ison) { 
     if(ison) { 
      toast.maketext(getapplicationcontext(), "开关打开了", toast.length_long).show(); 
     } else { 
      toast.maketext(getapplicationcontext(), "开关关闭了", toast.length_long).show(); 
     } 
    } 
   }); 
  } catch (exception e) { 
   e.printstacktrace(); 
  } 
 } 
} 

好了,代码都贴完了,接下来看看运行效果吧,(*^__^*) 嘻嘻……


很遗憾的是,木有动画效果,呜呜~~~~(>_<)~~~~ ,先这样吧,呵呵
好了,基本上自定义开关组件到这里就讲完了,如有不足,请大家谅解。

源码下载:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网