Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2017
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.71 KB | None | 0 0
  1. public SMCustomView(Context context) {
  2. super(context,null);
  3. }
  4.  
  5. public SMCustomView(Context context, AttributeSet attrs) {
  6. super(context, attrs,0);
  7. }
  8.  
  9. public SMCustomView(Context context, AttributeSet attrs, int defStyleAttr) {
  10. super(context, attrs, defStyleAttr);
  11. readAttrs(attrs);
  12. onPath=new Path();
  13. offPath=new Path();
  14. init();
  15. }
  16.  
  17. public void readAttrs(AttributeSet attrs) {
  18. TypedArray typedArray=getContext().getTheme().obtainStyledAttributes(attrs,R.styleable.Speedometer,0,0);
  19. mMaxSpeed=typedArray.getFloat(R.styleable.Speedometer_maxSpeed,MAXSPEED);
  20. mCurrentSpeed=typedArray.getFloat(R.styleable.Speedometer_currentSpeed,0);
  21. ONCOLOR=typedArray.getColor(R.styleable.Speedometer_onColor,ONCOLOR);
  22. OFFCOLOR=typedArray.getColor(R.styleable.Speedometer_offColor,OFFCOLOR);
  23. SCALECOLOR=typedArray.getColor(R.styleable.Speedometer_scaleColor,SCALECOLOR);
  24. SCALESIZE=typedArray.getDimension(R.styleable.Speedometer_scaleTextSize,SCALESIZE);
  25. READSIZE=typedArray.getDimension(R.styleable.Speedometer_readingTextSize,READSIZE);
  26. typedArray.recycle();
  27. }
  28.  
  29. public void init()
  30. {
  31. onMark=new Paint();
  32. onMark.setStyle(Paint.Style.STROKE);
  33. onMark.setColor(ONCOLOR);
  34. onMark.setStrokeWidth(35f);
  35. onMark.setShadowLayer(10f,2f,2f,ONCOLOR);
  36. onMark.setAntiAlias(true);
  37.  
  38. offMark=new Paint(onMark);
  39. offMark.setColor(OFFCOLOR);
  40. offMark.setStyle(Paint.Style.FILL_AND_STROKE);
  41. offMark.setShadowLayer(2f,2f,2f,OFFCOLOR);
  42.  
  43. scalePaint=new Paint(offMark);
  44. scalePaint.setTextSize(SCALESIZE);
  45. scalePaint.setColor(SCALECOLOR);
  46. scalePaint.setStrokeWidth(2f);
  47. scalePaint.setShadowLayer(10f,2f,2f,Color.CYAN);
  48.  
  49. read=new Paint(scalePaint);
  50. read.setStyle(Paint.Style.FILL_AND_STROKE);
  51. read.setColor(Color.YELLOW);
  52. read.setTextSize(85f);
  53. read.setShadowLayer(5f,2f,2f,Color.YELLOW);
  54. read.setTypeface(Typeface.DEFAULT_BOLD);
  55. }
  56. public float getmCurrentSpeed()
  57. {
  58. return mCurrentSpeed;
  59. }
  60.  
  61. public void setmCurrentSpeed(float current)
  62. {
  63. if(current>mMaxSpeed)
  64. mCurrentSpeed=mMaxSpeed;
  65. else if(current<0)
  66. mCurrentSpeed=0;
  67. else
  68. mCurrentSpeed=current;
  69. }
  70. @Override
  71. protected void onSizeChanged(int width,int height,int w,int h)
  72. {
  73. if(width>height)
  74. {
  75. radius=height/4;
  76. }
  77. else
  78. {
  79. radius=width/4;
  80. }
  81. oval.set(centerX-radius,centerY-radius,centerX+radius,centerY+radius);
  82. }
  83.  
  84. @Override
  85. protected void onMeasure(int widthMeasureSpec,int heightMeasureSpec)
  86. {
  87. int widthMode=MeasureSpec.getMode(widthMeasureSpec);
  88. int widthSize=MeasureSpec.getSize(widthMeasureSpec);
  89.  
  90. int heightMode=MeasureSpec.getMode(heightMeasureSpec);
  91. int heightSize=MeasureSpec.getSize(heightMeasureSpec);
  92.  
  93. int choosenWidth=chooseDimension(widthMode,widthSize);
  94. int choosenHeight=chooseDimension(heightMode,heightSize);
  95.  
  96. int choosenDimension=Math.min(choosenWidth,choosenHeight);
  97. centerX=choosenDimension/2;
  98. centerY=choosenDimension/2;
  99. setMeasuredDimension(choosenDimension,choosenDimension);
  100. }
  101.  
  102. public int chooseDimension(int mode,int size)
  103. {
  104. if(mode==MeasureSpec.AT_MOST || mode==MeasureSpec.EXACTLY)
  105. {
  106. return size;
  107. }
  108. else
  109. {
  110. return getSize();
  111. }
  112. }
  113.  
  114. public int getSize()
  115. {
  116. return 240;
  117. }
  118.  
  119. @Override
  120. public void onDraw(Canvas canvas)
  121. {
  122. drawScaleBackground(canvas);
  123. drawScale(canvas);
  124. drawInscription(canvas);
  125. drawRead(canvas);
  126. }
  127.  
  128. public void drawScaleBackground(Canvas canvas)
  129. {
  130. offPath.reset();
  131. for (int i = -180; i < 0; i=i+4) {
  132. offPath.addArc(oval,i,4f);
  133. }
  134. canvas.drawPath(offPath,offMark);
  135. }
  136.  
  137. public void drawScale(Canvas canvas)
  138. {
  139. onPath.reset();
  140. for (int i = -180; i < (mCurrentSpeed/mMaxSpeed)*180-180; i=+4) {
  141. onPath.addArc(oval,i,4f);
  142. }
  143. canvas.drawPath(onPath,onMark);
  144. }
  145.  
  146. public void drawInscription(Canvas canvas)
  147. {
  148. canvas.save(Canvas.MATRIX_SAVE_FLAG);
  149. canvas.rotate(-180,centerX,centerY);
  150. Path circle=new Path();
  151. double halfCircle=radius*Math.PI;
  152. for (int i = 0; i < mMaxSpeed; i=i+20) {
  153. circle.addCircle(centerX,centerY,radius, Path.Direction.CW);
  154. canvas.drawTextOnPath(String.format("%d",i),circle,(float)(i*halfCircle/mMaxSpeed),-30f,scalePaint);
  155. }
  156. canvas.restore();
  157. }
  158.  
  159. public void drawRead(Canvas canvas)
  160. {
  161. Path path=new Path();
  162. String message=String.format("%d",(int)mCurrentSpeed);
  163. float[] width=new float[message.length()];
  164. read.getTextWidths(message,width);
  165. float adv=0;
  166. for (double w:width) {
  167. adv+=w;
  168. }
  169. path.moveTo(centerX-adv/2,centerY);
  170. path.lineTo(centerX+adv/2,centerY);
  171. canvas.drawTextOnPath(message,path,0,0,read);
  172. }
  173.  
  174. public void onSpeedChanged(float speed)
  175. {
  176. this.setmCurrentSpeed(speed);
  177. this.invalidate();
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement