android-circlebutton介绍原理



  
   android-circlebutton 是github上的一个开源项目,正如它的简介一样:Circle button widget for Android,就是一个圆形的button。它与一般圆形的button不同之处在于它是画出来的,属于自定义UI的范畴,因此我拿来介绍一下,而们平常一般使用的button可能是来自于美工的图片。它的好处是节省了资源空间,当然缺点也很明显开发人员也要参与界面设计这块了。其实这样也不能说不好,反过来想减少了对美工的依赖,而且也比较适合开发人员自己去实现一些扁平化的东西,因为它很简洁。
  首先看代码,
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package com.example.circlebutton;  
  2.   
  3. import android.animation.ObjectAnimator;  
  4. import android.content.Context;  
  5. import android.content.res.TypedArray;  
  6. import android.graphics.Canvas;  
  7. import android.graphics.Color;  
  8. import android.graphics.Paint;  
  9. import android.util.AttributeSet;  
  10. import android.util.TypedValue;  
  11. import android.widget.ImageView;  
  12.   
  13. public class CircleButton extends ImageView {  
  14.   
  15.      private static final int PRESSED_COLOR_LIGHTUP = 255 / 25;  
  16.      private static final int PRESSED_RING_ALPHA = 75;  
  17.      private static final int DEFAULT_PRESSED_RING_WIDTH_DIP = 4;  
  18.      private static final int ANIMATION_TIME_ID = android.R.integer.config_shortAnimTime;  
  19.   
  20.      private int centerY;  
  21.      private int centerX;  
  22.      private int outerRadius;  
  23.      private int pressedRingRadius;  
  24.   
  25.      private Paint circlePaint;  
  26.      private Paint focusPaint;  
  27.   
  28.      private float animationProgress;  
  29.   
  30.      private int pressedRingWidth;  
  31.      private int defaultColor = Color.BLACK;  
  32.      private int pressedColor;  
  33.      private ObjectAnimator pressedAnimator;  
  34.   
  35.      public CircleButton(Context context) {  
  36.           super(context);  
  37.           init(context, null);  
  38.      }  
  39.   
  40.      public CircleButton(Context context, AttributeSet attrs) {  
  41.           super(context, attrs);  
  42.           init(context, attrs);  
  43.      }  
  44.   
  45.      public CircleButton(Context context, AttributeSet attrs, int defStyle) {  
  46.           super(context, attrs, defStyle);  
  47.           init(context, attrs);  
  48.      }  
  49.   
  50.      @Override  
  51.      public void setPressed(boolean pressed) {  
  52.           super.setPressed(pressed);  
  53.   
  54.           if (circlePaint != null) {  
  55.                circlePaint.setColor(pressed ? pressedColor : defaultColor);  
  56.           }  
  57.   
  58.           if (pressed) {  
  59.                showPressedRing();  
  60.           } else {  
  61.                hidePressedRing();  
  62.           }  
  63.      }  
  64.   
  65.      @Override  
  66.      protected void onDraw(Canvas canvas) {  
  67.           canvas.drawCircle(centerX, centerY, pressedRingRadius + animationProgress, focusPaint);  
  68.           canvas.drawCircle(centerX, centerY, outerRadius - pressedRingWidth, circlePaint);  
  69.           super.onDraw(canvas);  
  70.      }  
  71.   
  72.      @Override  
  73.      protected void onSizeChanged(int w, int h, int oldw, int oldh) {  
  74.           super.onSizeChanged(w, h, oldw, oldh);  
  75.           centerX = w / 2;  
  76.           centerY = h / 2;  
  77.           outerRadius = Math.min(w, h) / 2;  
  78.           pressedRingRadius = outerRadius - pressedRingWidth - pressedRingWidth / 2;  
  79.      }  
  80.   
  81.      public float getAnimationProgress() {  
  82.           return animationProgress;  
  83.      }  
  84.   
  85.      public void setAnimationProgress(float animationProgress) {  
  86.           this.animationProgress = animationProgress;  
  87.           this.invalidate();  
  88.      }  
  89.   
  90.      public void setColor(int color) {  
  91.           this.defaultColor = color;  
  92.           this.pressedColor = getHighlightColor(color, PRESSED_COLOR_LIGHTUP);  
  93.   
  94.           circlePaint.setColor(defaultColor);  
  95.           focusPaint.setColor(defaultColor);  
  96.           focusPaint.setAlpha(PRESSED_RING_ALPHA);  
  97.   
  98.           this.invalidate();  
  99.      }  
  100.   
  101.      private void hidePressedRing() {  
  102.           pressedAnimator.setFloatValues(pressedRingWidth, 0f);  
  103.           pressedAnimator.start();  
  104.      }  
  105.   
  106.      private void showPressedRing() {  
  107.           pressedAnimator.setFloatValues(animationProgress, pressedRingWidth);  
  108.           pressedAnimator.start();  
  109.      }  
  110.   
  111.      private void init(Context context, AttributeSet attrs) {  
  112.           this.setFocusable(true);  
  113.           this.setScaleType(ScaleType.CENTER_INSIDE);  
  114.           setClickable(true);  
  115.   
  116.           circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);  
  117.           circlePaint.setStyle(Paint.Style.FILL);  
  118.   
  119.           focusPaint = new Paint(Paint.ANTI_ALIAS_FLAG);  
  120.           focusPaint.setStyle(Paint.Style.STROKE);  
  121.   
  122.           pressedRingWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, DEFAULT_PRESSED_RING_WIDTH_DIP, getResources()  
  123.                     .getDisplayMetrics());  
  124.   
  125.           int color = Color.BLACK;  
  126.           if (attrs != null) {  
  127.                final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CircleButton);  
  128.                color = a.getColor(R.styleable.CircleButton_cb_color, color);  
  129.                pressedRingWidth = (int) a.getDimension(R.styleable.CircleButton_cb_pressed_ring_width, pressedRingWidth);  
  130.                a.recycle();  
  131.           }  
  132.   
  133.           setColor(color);  
  134.   
  135.           focusPaint.setStrokeWidth(pressedRingWidth);  
  136.           final int pressedAnimationTime = getResources().getInteger(ANIMATION_TIME_ID);  
  137.           pressedAnimator = ObjectAnimator.ofFloat(this, "animationProgress", 0f, 0f);  
  138.           pressedAnimator.setDuration(pressedAnimationTime);  
  139.      }  
  140.   
  141.      private int getHighlightColor(int color, int amount) {  
  142.           return Color.argb(Math.min(255, Color.alpha(color)), Math.min(255, Color.red(color) + amount),  
  143.                     Math.min(255, Color.green(color) + amount), Math.min(255, Color.blue(color) + amount));  
  144.      }  
  145. }  
  
  继承了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. RectF rect=new RectF();  
  3. rect.left=8-animationProgress;  
  4. rect.top=8-animationProgress;  
  5. rect.right47+ animationProgress;  
  6. rect.bottom=47 + animationProgress;  
  7. canvas.drawRect(rect, focusPaint);// 长方形    
  8. //实心矩形  
  9. RectF rect1=new RectF();  
  10. rect1.left=5;  
  11. rect1.top=5;  
  12. rect1.right=50;  
  13. rect1.bottom=50;  
  14. canvas.drawRect(rect1, circlePaint);// 长方形    
  思路是通过RecF画矩形,在每次点击的时候,让坐标位置随着animationProgress而改变。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值