Material Design学习之 Button(详细分析,传说中的水滴动画)

Material Design学习之 Button(详细分析,传说中的水滴动画)

标签: Materialandroid设计view
1680人阅读 评论(2) 收藏 举报
分类:

目录(?)[+]

转载请注明出处:王亟亟的大牛之路


      上一篇大致介绍了Material Design的一些基本概念传送门:http://blog.csdn.net/ddwhan0123/article/details/50541561

这一片来具体学习下里面的内容,这篇分为两部分一部分是原理分析,一部分是代码分析。

先简要的介绍一些理论知识,顺便温顾下基础知识


按钮

按钮由文字和/或图标组成,文字及图标必须能让人轻易地和点击后展示的内容联系起来。
主要的按钮有三种:

  • 悬浮响应按钮(Floating action button), 点击后会产生墨水扩散效果的圆形按钮。
  • 浮动按钮(Raised button), 常见的方形纸片按钮,点击后会产生墨水扩散效果。
  • 扁平按钮(Flat button), 点击后产生墨水扩散效果,和浮动按钮的区别是没有浮起的效果。

颜色饱满的图标应当是功能性的,尽量避免把他们作为纯粹装饰用的元素。

在我们AS建包之后就会有一种Blank Activity的模式,里面会有一个悬浮响应按钮(Floating action button)

跟我们一直搜的什么360悬浮按钮一个实现吧,但是要点明一点中心,他是有厚度的。


大致像这样:



我们的控件都是有厚度的,他们不在一个层级上,造成了层次感。


顺便上一下我们的案例对象的效果:


从图中我们可以看出,按钮的事件是有响应的(也就是大家一直搜的“地震”传播的效果)

案例中有一个问题,就是颜色的不统一。

注意事项:

按钮的设计应当和应用的颜色主题保持一致。(这一点还是很重要的,尽量不要用户觉得一种杂乱感)


设计的过程中一定不要让我们的全副按钮重叠在底部的BAR/Button上,即使他们不是统一厚度


再提一下按钮的种类(突出注意下他们的使用场景):

悬浮响应按钮

悬浮响应按钮是促进动作里的特殊类型。 是一个圆形的漂浮在界面之上的、拥有一系列特殊动作的按钮,这些动作通常和变换、启动、以及它本身的转换锚点相关。

浮动按钮

浮动按钮使按钮在比较拥挤的界面上更清晰可见。能给大多数扁平的布局带来层次感。

底部固定按钮

如果需要一个对用户持续可见的功能按钮,应该首先考虑使用悬浮响应按钮。如果需要一个非主要、但是能快速定位到的按钮,则可以使用底部固定按钮。

扁平按钮

扁平按钮一般用于对话框或者工具栏, 可避免页面上过多无意义的层叠。

对话框中的按钮

对话框中使用扁平按钮作为主要按钮类型以避免过多的层次叠加。

主按钮

按钮类型应该基于主按钮、屏幕上容器的数量以及整体布局来进行选择。

首先,审视一遍你的按钮功能: 它是不是非常重要而且应用广泛到需要用上悬浮响应按钮?

然后,基于放置按钮的容器以及屏幕上层次堆叠的数量来选择使用浮动按钮还是扁平按钮。而且应该避免过多的层叠。

最后,检查你的布局。 一个容器应该只使用一种类型的按钮。 只在比较特殊的情况下(比如需要强调一个浮起的效果)才应该混合使用多种类型的按钮。

更多内容可以看原文:http://www.google.com/design/spec/components/buttons.html


接下来我们来分析下我们的实现效果--ButtonFlat和ButtonRectangle


在这里再次感谢开源作者:https://github.com/navasmdc/MaterialDesignLibrary


先说下ButtonRectangle

  1. public class ButtonRectangle extends Button  

继承于Button但是不是Android源生的Button,作者自己写了个Button我们看下去

  1. public abstract class Button extends CustomView  

一个抽象类,又继承于 另一个类 CustomView,那我们再看下去

  1. public class CustomView extends RelativeLayout  

OK,这下应该到底了,Custom应该是Button的基类然后 ButtonRectangle去实现他父类的一系列抽象方法。(读优秀的源码还是很重要的,加深理解和拓宽思路)

  1. final static String MATERIALDESIGNXML = "http://schemas.android.com/apk/res-auto";  
  2. final static String ANDROIDXML = "http://schemas.android.com/apk/res/android";  
  3.   
  4. final int disabledBackgroundColor = Color.parseColor("#E2E2E2");  
  5. int beforeBackground;  
  6.   
  7. // Indicate if user touched this view the last time  
  8. public boolean isLastTouch = false;  
  9.   
  10. public CustomView(Context context, AttributeSet attrs) {  
  11.     super(context, attrs);  
  12. }  

12-23行,构造函数(初始化),声明一个禁用的颜色,一个beforeBackground变量,还有XML配置的标签内容

  1. <span style="white-space:pre">    </span>@Override  
  2.     public void setEnabled(boolean enabled) {  
  3.         super.setEnabled(enabled);  
  4.         if(enabled)  
  5.             setBackgroundColor(beforeBackground);  
  6.         else  
  7.             setBackgroundColor(disabledBackgroundColor);  
  8.         invalidate();  
  9.     }  

25-33 重写了的setEnabled方法,自定义在可点击和不可点击情况下的着色

  1. <span style="white-space:pre">    </span>boolean animation = false;  
  2.       
  3.     @Override  
  4.     protected void onAnimationStart() {  
  5.         super.onAnimationStart();  
  6.         animation = true;  
  7.     }  
  8.       
  9.     @Override  
  10.     protected void onAnimationEnd() {  
  11.         super.onAnimationEnd();  
  12.         animation = false;  
  13.     }  

35-47 声明一个动画的波尔变量,根据调用开启/关闭动画来对波尔值进行修改

  1. <span style="white-space:pre">    </span>@Override  
  2.     protected void onDraw(Canvas canvas) {  
  3.         super.onDraw(canvas);  
  4.         if(animation)  
  5.         invalidate();  
  6.     }  


50-55刷新View


我们可以很清楚的看出,CustomView是个工具类,并未发现我们想要的实现,我们继续看下去!


接下来,我们来到了Button

  1. <span style="white-space:pre">    </span>int minWidth;  
  2.     int minHeight;  
  3.     int background;  
  4.     float rippleSpeed = 12f;  
  5.     int rippleSize = 3;  
  6.     Integer rippleColor;  
  7.     OnClickListener onClickListener;  
  8.     boolean clickAfterRipple = true;  
  9.     int backgroundColor = Color.parseColor("#1E88E5");  
  10.     TextView textButton;  


24-33一系列的参数声明, textButton就是我们等会要出现的字,还有就是监听事件,以及一些位置的参数

  1. public Button(Context context, AttributeSet attrs) {  
  2.     super(context, attrs);  
  3.     setDefaultProperties();  
  4.     clickAfterRipple = attrs.getAttributeBooleanValue(MATERIALDESIGNXML,  
  5.             "animate"true);  
  6.     setAttributes(attrs);  
  7.     beforeBackground = backgroundColor;  
  8.     if (rippleColor == null)  
  9.         rippleColor = makePressColor();  
  10. }  
  11.   
  12. protected void setDefaultProperties() {  
  13.     // Min size  
  14.     setMinimumHeight(Utils.dpToPx(minHeight, getResources()));  
  15.     setMinimumWidth(Utils.dpToPx(minWidth, getResources()));  
  16.     // Background shape  
  17.     setBackgroundResource(background);  
  18.     setBackgroundColor(backgroundColor);  
  19. }  

35-53 初始我们的Button,并且设置了背景的形状以及一个大小参数


  1. // Set atributtes of XML to View  
  2. abstract protected void setAttributes(AttributeSet attrs);  
  3.   
  4. // ### RIPPLE EFFECT ###  
  5.   
  6. float x = -1, y = -1;  
  7. float radius = -1;  

55-61 获取参数用的抽象方法,供子类实现,再下面是波纹特效的实现了(期盼已久,暂时还不知道这x y 是什么,继续看下去)

  1. @Override  
  2. public boolean onTouchEvent(MotionEvent event) {  
  3.     invalidate();  
  4.     if (isEnabled()) {  
  5.         isLastTouch = true;  
  6.         if (event.getAction() == MotionEvent.ACTION_DOWN) {  
  7.             radius = getHeight() / rippleSize;  
  8.             x = event.getX();  
  9.             y = event.getY();  
  10.         } else if (event.getAction() == MotionEvent.ACTION_MOVE) {  
  11.             radius = getHeight() / rippleSize;  
  12.             x = event.getX();  
  13.             y = event.getY();  
  14.             if (!((event.getX() <= getWidth() && event.getX() >= 0) && (event  
  15.                     .getY() <= getHeight() && event.getY() >= 0))) {  
  16.                 isLastTouch = false;  
  17.                 x = -1;  
  18.                 y = -1;  
  19.             }  
  20.         } else if (event.getAction() == MotionEvent.ACTION_UP) {  
  21.             if ((event.getX() <= getWidth() && event.getX() >= 0)  
  22.                     && (event.getY() <= getHeight() && event.getY() >= 0)) {  
  23.                 radius++;  
  24.                 if (!clickAfterRipple && onClickListener != null) {  
  25.                     onClickListener.onClick(this);  
  26.                 }  
  27.             } else {  
  28.                 isLastTouch = false;  
  29.                 x = -1;  
  30.                 y = -1;  
  31.             }  
  32.         } else if (event.getAction() == MotionEvent.ACTION_CANCEL) {  
  33.             isLastTouch = false;  
  34.             x = -1;  
  35.             y = -1;  
  36.         }  
  37.     }  
  38.     return true;  
  39. }  

63-101 这里就是我们的具体实现了,我们来好好读一下。

65行,这个onTouchEvent方法的整个过程中,UI会被不断的刷新。

66,判断屏幕是否可见

67行,当屏幕被View本身被触摸后父类的isLastTouch为true(我们看看他到底干吗用)

68-72行,当手直接出屏幕,初始化x,y为X,Y的手指坐标,制定“圆圈”半径。

72-82行,如果 手指的移动范围超出了空间的区域isLastTouch为false,X,Y坐标置为-1

82-94行,如果手指的触控点还在空间范围内的情况下手指离开屏幕我们的圆自增摆脱无效值并且触发Click事件,反之如上,都初始化一圈。

94-98行,用于操作当用户保持按下操作,并从空间区域移到其他外层控件时触发(幻想下滑动listview的行为就理解了,为什么划得时候,离开的时候都没有触发OnItemClick)

一整个onTouchEvent都是对绘制内容前参数的收集以及初始化,我们继续读源码


  1. <span style="white-space:pre">    </span>@Override  
  2.     protected void onFocusChanged(boolean gainFocus, int direction,  
  3.             Rect previouslyFocusedRect) {  
  4.         if (!gainFocus) {  
  5.             x = -1;  
  6.             y = -1;  
  7.         }  
  8.     }  

103-110,是否为焦点的判断,如果不是一切还原。

  1. <span style="white-space:pre">    </span>@Override  
  2.     public boolean onInterceptTouchEvent(MotionEvent ev) {  
  3.         // super.onInterceptTouchEvent(ev);  
  4.         return true;  
  5.     }  

112-116,干预Touch的处理,之后的时间不会被传递,可以参考:http://blog.csdn.net/lvxiangan/article/details/9309927


  1. public Bitmap makeCircle() {  
  2.     Bitmap output = Bitmap.createBitmap(  
  3.             getWidth() - Utils.dpToPx(6, getResources()), getHeight()  
  4.                     - Utils.dpToPx(7, getResources()), Config.ARGB_8888);  
  5.     Canvas canvas = new Canvas(output);  
  6.     canvas.drawARGB(0000);  
  7.     Paint paint = new Paint();  
  8.     paint.setAntiAlias(true);  
  9.     paint.setColor(rippleColor);  
  10.     canvas.drawCircle(x, y, radius, paint);  
  11.     if (radius > getHeight() / rippleSize)  
  12.         radius += rippleSpeed;  
  13.     if (radius >= getWidth()) {  
  14.         x = -1;  
  15.         y = -1;  
  16.         radius = getHeight() / rippleSize;  
  17.         if (onClickListener != null && clickAfterRipple)  
  18.             onClickListener.onClick(this);  
  19.     }  
  20.     return output;  
  21. }  

118-138,具体绘制图像的操作。

首先画了个Bitmap作为底板,填充颜色,固定圆圈的大小,直至大到超出控件大小被初始化继续维持事件触发

  1. <span style="white-space:pre">    </span>protected int makePressColor() {  
  2.         int r = (this.backgroundColor >> 16) & 0xFF;  
  3.         int g = (this.backgroundColor >> 8) & 0xFF;  
  4.         int b = (this.backgroundColor >> 0) & 0xFF;  
  5.         r = (r - 30 < 0) ? 0 : r - 30;  
  6.         g = (g - 30 < 0) ? 0 : g - 30;  
  7.         b = (b - 30 < 0) ? 0 : b - 30;  
  8.         return Color.rgb(r, g, b);  
  9.     }  

145-153,涟漪效果的颜色汲取

  1. @Override  
  2. public void setOnClickListener(OnClickListener l) {  
  3.     onClickListener = l;  
  4. }  

156-158,接收点击事件的回调


  1. public void setBackgroundColor(int color) {  
  2.     this.backgroundColor = color;  
  3.     if (isEnabled())  
  4.         beforeBackground = backgroundColor;  
  5.     try {  
  6.         LayerDrawable layer = (LayerDrawable) getBackground();  
  7.         GradientDrawable shape = (GradientDrawable) layer  
  8.                 .findDrawableByLayerId(R.id.shape_bacground);  
  9.         shape.setColor(backgroundColor);  
  10.         rippleColor = makePressColor();  
  11.     } catch (Exception ex) {  
  12.         // Without bacground  
  13.     }  
  14. }  

161-174 设置背景色,期间是掺杂了一个自绘bg的颜色设置,这边不进去细说了。

再下面就是一系列的set方法了,我们来分析下刚才那一系列的实现

首先,我们的圈必须是在手指触发事件之后再绘制,并且离开空间范围内的触发是无效的不会触发动画效果,只有手指的触摸圆圈,这个圆圈的大小取决于getHeight/规定值的算法,规定值我们可以设置,这个父类构建了我们按钮所需的动画和计算的基础。


最后我们来说下ButtonRectangle

  1. <span style="white-space:pre">    </span>TextView textButton;  
  2.       
  3.     int paddingTop,paddingBottom, paddingLeft, paddingRight;  
  4.       
  5.       
  6.     public ButtonRectangle(Context context, AttributeSet attrs) {  
  7.         super(context, attrs);  
  8.         setDefaultProperties();  
  9.     }  
  10.     @Override  
  11.     protected void setDefaultProperties(){  
  12. //      paddingBottom = Utils.dpToPx(16, getResources());  
  13. //      paddingLeft = Utils.dpToPx(16, getResources());  
  14. //      paddingRight = Utils.dpToPx(16, getResources());  
  15. //      paddingTop = Utils.dpToPx(16, getResources());  
  16.         super.minWidth = 80;  
  17.         super.minHeight = 36;  
  18.         super.background = R.drawable.background_button_rectangle;  
  19.         super.setDefaultProperties();  
  20.     }  


18-37行,初始化我们的控件。

  1.     // Set atributtes of XML to View  
  2.     protected void setAttributes(AttributeSet attrs){  
  3.           
  4.         //Set background Color  
  5.         // Color by resource  
  6.         int bacgroundColor = attrs.getAttributeResourceValue(ANDROIDXML,"background",-1);  
  7.         if(bacgroundColor != -1){  
  8.             setBackgroundColor(getResources().getColor(bacgroundColor));  
  9.         }else{  
  10.             // Color by hexadecimal  
  11.             // Color by hexadecimal  
  12.             background = attrs.getAttributeIntValue(ANDROIDXML, "background", -1);  
  13.             if (background != -1)  
  14.                 setBackgroundColor(background);  
  15.         }  
  16.           
  17.         // Set Padding  
  18.         String value = attrs.getAttributeValue(ANDROIDXML,"padding");  
  19. //      if(value != null){  
  20. //          float padding = Float.parseFloat(value.replace("dip", ""));  
  21. //          paddingBottom = Utils.dpToPx(padding, getResources());  
  22. //          paddingLeft = Utils.dpToPx(padding, getResources());  
  23. //          paddingRight = Utils.dpToPx(padding, getResources());  
  24. //          paddingTop = Utils.dpToPx(padding, getResources());  
  25. //      }else{  
  26. //          value = attrs.getAttributeValue(ANDROIDXML,"paddingLeft");  
  27. //          paddingLeft = (value == null) ? paddingLeft : (int) Float.parseFloat(value.replace("dip", ""));  
  28. //          value = attrs.getAttributeValue(ANDROIDXML,"paddingTop");  
  29. //          paddingTop = (value == null) ? paddingTop : (int) Float.parseFloat(value.replace("dip", ""));  
  30. //          value = attrs.getAttributeValue(ANDROIDXML,"paddingRight");  
  31. //          paddingRight = (value == null) ? paddingRight : (int) Float.parseFloat(value.replace("dip", ""));  
  32. //          value = attrs.getAttributeValue(ANDROIDXML,"paddingBottom");  
  33. //          paddingBottom = (value == null) ? paddingBottom : (int) Float.parseFloat(value.replace("dip", ""));  
  34. //      }  
  35.           
  36.           
  37.         // Set text button  
  38.         String text = null;  
  39.         int textResource = attrs.getAttributeResourceValue(ANDROIDXML,"text",-1);  
  40.         if(textResource != -1){  
  41.             text = getResources().getString(textResource);  
  42.         }else{  
  43.             text = attrs.getAttributeValue(ANDROIDXML,"text");  
  44.         }  
  45.         if(text != null){  
  46.             textButton = new TextView(getContext());  
  47.             textButton.setText(text);  
  48.             textButton.setTextColor(Color.WHITE);  
  49.             textButton.setTypeface(null, Typeface.BOLD);  
  50.             RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);  
  51.             params.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);  
  52.             params.setMargins(Utils.dpToPx(5, getResources()), Utils.dpToPx(5, getResources()), Utils.dpToPx(5, getResources()), Utils.dpToPx(5, getResources()));  
  53.             textButton.setLayoutParams(params);           
  54.             addView(textButton);  
  55. //                  FrameLayout.LayoutParams params = (LayoutParams) textView.getLayoutParams();  
  56. //                  params.width = getWidth();  
  57. //                  params.gravity = Gravity.CENTER_HORIZONTAL;  
  58.                     params.setMargins(paddingLeft, paddingTop, paddingRight, paddingRight);  
  59. //                  textView.setLayoutParams(params);textColor  
  60.             int textColor = attrs.getAttributeResourceValue(ANDROIDXML,"textColor",-1);  
  61.             if(textColor != -1){  
  62.                 textButton.setTextColor(textColor);  
  63.             }else{  
  64.                 // Color by hexadecimal  
  65.                 // Color by hexadecimal  
  66.                 textColor = attrs.getAttributeIntValue(ANDROIDXML, "textColor", -1);  
  67.                 if (textColor != -1)  
  68.                     textButton.setTextColor(textColor);  
  69.             }  
  70.             int[] array = {android.R.attr.textSize};  
  71.             TypedArray values = getContext().obtainStyledAttributes(attrs, array);  
  72.             float textSize = values.getDimension(0, -1);  
  73.             values.recycle();  
  74.             if(textSize != -1)  
  75.                 textButton.setTextSize(textSize);  
  76.               
  77.         }  
  78.           
  79.         rippleSpeed = attrs.getAttributeFloatValue(MATERIALDESIGNXML,  
  80.                 "rippleSpeed", Utils.dpToPx(6, getResources()));  
  81.     }  

41-120行,读取一系列XML传来的内容,包括系统的标签,以及自定义标签,如果没有,就设置预设的参数。

  1.     Integer height;  
  2.     Integer width;  
  3.     @Override  
  4.     protected void onDraw(Canvas canvas) {  
  5. //      if(!txtCenter)  
  6. //      centrarTexto();  
  7.         super.onDraw(canvas);  
  8.         if (x != -1) {  
  9.             Rect src = new Rect(00, getWidth()-Utils.dpToPx(6, getResources()), getHeight()-Utils.dpToPx(7, getResources()));  
  10.             Rect dst = new Rect(Utils.dpToPx(6, getResources()), Utils.dpToPx(6, getResources()), getWidth()-Utils.dpToPx(6, getResources()), getHeight()-Utils.dpToPx(7, getResources()));  
  11.             canvas.drawBitmap(makeCircle(), src, dst, null);  
  12.             invalidate();  
  13.         }  
  14.     }  

135-148行

判断是否有接触,如果没有就不绘制(作者拿X作为比较,其实X Y都一样,不为-1就是接触了,没接触(或接触区域不正确)的时候均为-1)

接着画了2个方块和我们之前计算的圆圈做组合效果。




总结:

实现,其实并不是太难,关键是需要更好的思考怎么实现更好,这里大致的再解释下流程。


首先,我们有一个大的方块他是RelativeLayout。

然后我们中间用OnTouchEvent来实现用户的触控过程和触控监听。

不断的绘制用户移动的触控点的圆。

当用户以合理的方式,在符合逻辑的位置up手指的时候出发画布的涟漪效果,这边使用色差和2个方块+圆变大过程的效果来呈现的。


过程中可能有我语言表达的问题,欢迎大家提出。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值