android-circlebutton介绍原理



android-circlebutton 是github上的一个开源项目,正如它的简介一样:Circle button widget for Android,就是一个圆形的button。它与一般圆形的button不同之处在于它是画出来的,属于自定义UI的范畴,因此我拿来介绍一下,而们平常一般使用的button可能是来自于美工的图片。它的好处是节省了资源空间,当然缺点也很明显开发人员也要参与界面设计这块了。其实这样也不能说不好,反过来想减少了对美工的依赖,而且也比较适合开发人员自己去实现一些扁平化的东西,因为它很简洁。
首先看代码,
[html] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. packagecom.example.circlebutton;
  2. importandroid.animation.ObjectAnimator;
  3. importandroid.content.Context;
  4. importandroid.content.res.TypedArray;
  5. importandroid.graphics.Canvas;
  6. importandroid.graphics.Color;
  7. importandroid.graphics.Paint;
  8. importandroid.util.AttributeSet;
  9. importandroid.util.TypedValue;
  10. importandroid.widget.ImageView;
  11. publicclassCircleButtonextendsImageView{
  12. privatestaticfinalintPRESSED_COLOR_LIGHTUP=255/25;
  13. privatestaticfinalintPRESSED_RING_ALPHA=75;
  14. privatestaticfinalintDEFAULT_PRESSED_RING_WIDTH_DIP=4;
  15. privatestaticfinalintANIMATION_TIME_ID=android.R.integer.config_shortAnimTime;
  16. privateintcenterY;
  17. privateintcenterX;
  18. privateintouterRadius;
  19. privateintpressedRingRadius;
  20. privatePaintcirclePaint;
  21. privatePaintfocusPaint;
  22. privatefloatanimationProgress;
  23. privateintpressedRingWidth;
  24. privateintdefaultColor=Color.BLACK;
  25. privateintpressedColor;
  26. privateObjectAnimatorpressedAnimator;
  27. publicCircleButton(Contextcontext){
  28. super(context);
  29. init(context,null);
  30. }
  31. publicCircleButton(Contextcontext,AttributeSetattrs){
  32. super(context,attrs);
  33. init(context,attrs);
  34. }
  35. publicCircleButton(Contextcontext,AttributeSetattrs,intdefStyle){
  36. super(context,attrs,defStyle);
  37. init(context,attrs);
  38. }
  39. @Override
  40. publicvoidsetPressed(booleanpressed){
  41. super.setPressed(pressed);
  42. if(circlePaint!=null){
  43. circlePaint.setColor(pressed?pressedColor:defaultColor);
  44. }
  45. if(pressed){
  46. showPressedRing();
  47. }else{
  48. hidePressedRing();
  49. }
  50. }
  51. @Override
  52. protectedvoidonDraw(Canvascanvas){
  53. canvas.drawCircle(centerX,centerY,pressedRingRadius+animationProgress,focusPaint);
  54. canvas.drawCircle(centerX,centerY,outerRadius-pressedRingWidth,circlePaint);
  55. super.onDraw(canvas);
  56. }
  57. @Override
  58. protectedvoidonSizeChanged(intw,inth,intoldw,intoldh){
  59. super.onSizeChanged(w,h,oldw,oldh);
  60. centerX=w/2;
  61. centerY=h/2;
  62. outerRadius=Math.min(w,h)/2;
  63. pressedRingRadius=outerRadius-pressedRingWidth-pressedRingWidth/2;
  64. }
  65. publicfloatgetAnimationProgress(){
  66. returnanimationProgress;
  67. }
  68. publicvoidsetAnimationProgress(floatanimationProgress){
  69. this.animationProgress=animationProgress;
  70. this.invalidate();
  71. }
  72. publicvoidsetColor(intcolor){
  73. this.defaultColor=color;
  74. this.pressedColor=getHighlightColor(color,PRESSED_COLOR_LIGHTUP);
  75. circlePaint.setColor(defaultColor);
  76. focusPaint.setColor(defaultColor);
  77. focusPaint.setAlpha(PRESSED_RING_ALPHA);
  78. this.invalidate();
  79. }
  80. privatevoidhidePressedRing(){
  81. pressedAnimator.setFloatValues(pressedRingWidth,0f);
  82. pressedAnimator.start();
  83. }
  84. privatevoidshowPressedRing(){
  85. pressedAnimator.setFloatValues(animationProgress,pressedRingWidth);
  86. pressedAnimator.start();
  87. }
  88. privatevoidinit(Contextcontext,AttributeSetattrs){
  89. this.setFocusable(true);
  90. this.setScaleType(ScaleType.CENTER_INSIDE);
  91. setClickable(true);
  92. circlePaint=newPaint(Paint.ANTI_ALIAS_FLAG);
  93. circlePaint.setStyle(Paint.Style.FILL);
  94. focusPaint=newPaint(Paint.ANTI_ALIAS_FLAG);
  95. focusPaint.setStyle(Paint.Style.STROKE);
  96. pressedRingWidth=(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,DEFAULT_PRESSED_RING_WIDTH_DIP,getResources()
  97. .getDisplayMetrics());
  98. intcolor=Color.BLACK;
  99. if(attrs!=null){
  100. finalTypedArraya=context.obtainStyledAttributes(attrs,R.styleable.CircleButton);
  101. color=a.getColor(R.styleable.CircleButton_cb_color,color);
  102. pressedRingWidth=(int)a.getDimension(R.styleable.CircleButton_cb_pressed_ring_width,pressedRingWidth);
  103. a.recycle();
  104. }
  105. setColor(color);
  106. focusPaint.setStrokeWidth(pressedRingWidth);
  107. finalintpressedAnimationTime=getResources().getInteger(ANIMATION_TIME_ID);
  108. pressedAnimator=ObjectAnimator.ofFloat(this,"animationProgress",0f,0f);
  109. pressedAnimator.setDuration(pressedAnimationTime);
  110. }
  111. privateintgetHighlightColor(intcolor,intamount){
  112. returnColor.argb(Math.min(255,Color.alpha(color)),Math.min(255,Color.red(color)+amount),
  113. Math.min(255,Color.green(color)+amount),Math.min(255,Color.blue(color)+amount));
  114. }
  115. }
继承了ImageView,这也注定了没有文字效果,所以如果你还想有文字效果,那么可以继承于button。看一下变量,除了Paint之外,再也就是3.0之后引入的属性动画ObjectAnimator这个类。既然是属性动画,当然是改变属性值了(不断更改对象的属性值),在这里就是改变button的颜色值。
构造方法里,还是常规的初始化paint、从attrs里获取属性值以及设置一些其他变量值,还要就是ObjectAnimator的初始化。然后CircleButton类会进入onSizeChanged方法,在这个方法里初始化了CircleButton的圆形坐标以及其他的变量。接着进入onDraw方法,在这里面绘制CircleButton,代码如下:
[html] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. canvas.drawCircle(centerX,centerY,pressedRingRadius+animationProgress,focusPaint);
  2. canvas.drawCircle(centerX,centerY,outerRadius-pressedRingWidth,circlePaint);
可以看到它绘制了两个圆形,其实是这样的,从两个Paint变量就可以看出:一个是cirlcePaint,它的Style是FILL类型的,说明它画的是实心圆也就是默认状态下的button。另一个focusPaint是获取焦点时候调用的,它的Style是STROKE类型的,所以画出一个圆环效果,这也就是点击button之后出现的。但是看一下这两个圆环的半径发现实心圆的半径比空心圆的半径要大4。那把画实心圆的代码注释掉,效果是这样的:
看完就明白了,原来是实心圆把空心圆给遮住了,所以默认情况下,看到的是实心圆。当点击button的时候,执行setPressed()方法,这个方法在里面会调用showPressedRing()方法,也就是开启动画效果。这样,关于CircleButton的机制就清楚了。
接下来根据这个lizi 做一个拓展,学以致用。比如我还想使用矩形的butoon,然后也有点击动画的效果,那如何完成呢?很简单,原理同上,上面的例子画的是圆形,那么类似地就画矩形。在onDraw()中加入如下代码:
[html] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //可以放大的矩形
  2. RectFrect=newRectF();
  3. rect.left=8-animationProgress;
  4. rect.top=8-animationProgress;
  5. rect.right=47+animationProgress;
  6. rect.bottom=47+animationProgress;
  7. canvas.drawRect(rect,focusPaint);//长方形
  8. //实心矩形
  9. RectFrect1=newRectF();
  10. rect1.left=5;
  11. rect1.top=5;
  12. rect1.right=50;
  13. rect1.bottom=50;
  14. canvas.drawRect(rect1,circlePaint);//长方形
思路是通过RecF画矩形,在每次点击的时候,让坐标位置随着animationProgress而改变。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值