Advertisement
Guest User

Untitled

a guest
Jul 17th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.05 KB | None | 0 0
  1. public class HistogramView extends View {
  2.  
  3. private Paint mAxisPaint;
  4.  
  5. private Paint mPaint;
  6.  
  7. // 需要绘制的值
  8. private float[] mValues = new float[7];
  9.  
  10. // 坐标原点
  11. private float[] mAxisZero = new float[2];
  12.  
  13. // 文字的坐标
  14. private float[] mTextXCoordinator = new float[7];
  15.  
  16. // 每一个柱状的宽度
  17. private float mItemWidth = 0;
  18.  
  19. private int mScreenWidth;
  20.  
  21. private int mItemSpace;
  22.  
  23. private Interpolator mInterpolator = new BounceInterpolator();
  24.  
  25. private float mAnimatorValue;
  26.  
  27. public HistogramView(Context context) {
  28. this(context, null);
  29. }
  30.  
  31. public HistogramView(Context context, @Nullable AttributeSet attrs) {
  32. super(context, attrs);
  33.  
  34.  
  35. mAxisPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
  36. mAxisPaint.setStyle(Paint.Style.STROKE);
  37. mAxisPaint.setColor(Color.WHITE);
  38. mAxisPaint.setTextAlign(Paint.Align.CENTER);
  39. mAxisPaint.setTextSize(30);
  40.  
  41. mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
  42. mPaint.setColor(Color.GREEN);
  43. mPaint.setStyle(Paint.Style.STROKE);
  44.  
  45. // 动画
  46. ValueAnimator animator = new ValueAnimator();
  47. animator.setFloatValues(0, 1);
  48. animator.setDuration(3000);
  49. animator.setInterpolator(mInterpolator);
  50. animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
  51. @Override
  52. public void onAnimationUpdate(ValueAnimator animation) {
  53. mAnimatorValue = (float) animation.getAnimatedValue();
  54. invalidate();
  55. }
  56. });
  57.  
  58. animator.start();
  59. }
  60.  
  61. @Override
  62. protected void onSizeChanged(int w, int h, int oldw, int oldh) {
  63. super.onSizeChanged(w, h, oldw, oldh);
  64.  
  65. mScreenWidth = w - oldw;
  66.  
  67. // 总的是11个
  68. mItemWidth = mScreenWidth / 11 + 0.5f;
  69.  
  70. mPaint.setStrokeWidth(mItemWidth);
  71.  
  72. mItemSpace = (int) (mItemWidth / 4 + 0.5f);
  73.  
  74. mValues[0] = 50;
  75. mValues[1] = 100;
  76. mValues[2] = 100;
  77. mValues[3] = 300;
  78. mValues[4] = 400;
  79. mValues[5] = 450;
  80. mValues[6] = 400;
  81.  
  82. mAxisZero[0] = mItemWidth;
  83. mAxisZero[1] = 500 + 50;
  84.  
  85. }
  86.  
  87. @Override
  88. protected void onDraw(Canvas canvas) {
  89. super.onDraw(canvas);
  90.  
  91. // 综合练习
  92. // 练习内容:使用各种 Canvas.drawXXX() 方法画直方图
  93.  
  94. drawAxis(canvas);
  95. drawValues(canvas);
  96.  
  97.  
  98. }
  99.  
  100. private void drawValues(Canvas canvas) {
  101.  
  102. canvas.save();
  103.  
  104. for (int i = 0; i < mValues.length; i++) {
  105. // 加入动画的动态变换值
  106. float yValue = mAxisZero[1] - mValues[i] * mAnimatorValue;
  107. canvas.drawLine(mTextXCoordinator[i], mAxisZero[1], mTextXCoordinator[i], yValue, mPaint);
  108. }
  109.  
  110. canvas.restore();
  111.  
  112. }
  113.  
  114. private void drawAxis(Canvas canvas) {
  115. canvas.save();
  116.  
  117. // x轴
  118. canvas.drawLine(mAxisZero[0], mAxisZero[1], mScreenWidth - mItemWidth + 30, mAxisZero[1], mAxisPaint);
  119. // y轴
  120. canvas.drawLine(mAxisZero[0], mAxisZero[1], mAxisZero[0], 20, mAxisPaint);
  121.  
  122. // 文字
  123. for (int i = 0; i < mValues.length; i++) {
  124. // 文字的X坐标
  125. mTextXCoordinator[i] = mAxisZero[0] + mItemSpace * (i + 1) + (i + 1) * mItemWidth - mItemWidth * 0.5f;
  126. }
  127.  
  128. drawAxisText(canvas);
  129.  
  130. canvas.restore();
  131. }
  132.  
  133. private void drawAxisText(Canvas canvas) {
  134.  
  135. float textYCoordinator = mAxisZero[1] + 30;
  136.  
  137. canvas.drawText("Froyo", mTextXCoordinator[0], textYCoordinator, mAxisPaint);
  138. canvas.drawText("GB", mTextXCoordinator[1], textYCoordinator, mAxisPaint);
  139. canvas.drawText("IC S", mTextXCoordinator[2], textYCoordinator, mAxisPaint);
  140. canvas.drawText("JB", mTextXCoordinator[3], textYCoordinator, mAxisPaint);
  141. canvas.drawText("KitKat", mTextXCoordinator[4], textYCoordinator, mAxisPaint);
  142. canvas.drawText("L", mTextXCoordinator[5], textYCoordinator, mAxisPaint);
  143. canvas.drawText("M", mTextXCoordinator[6], textYCoordinator, mAxisPaint);
  144.  
  145. }
  146.  
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement