Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public SMCustomView(Context context) {
- super(context,null);
- }
- public SMCustomView(Context context, AttributeSet attrs) {
- super(context, attrs,0);
- }
- public SMCustomView(Context context, AttributeSet attrs, int defStyleAttr) {
- super(context, attrs, defStyleAttr);
- readAttrs(attrs);
- onPath=new Path();
- offPath=new Path();
- init();
- }
- public void readAttrs(AttributeSet attrs) {
- TypedArray typedArray=getContext().getTheme().obtainStyledAttributes(attrs,R.styleable.Speedometer,0,0);
- mMaxSpeed=typedArray.getFloat(R.styleable.Speedometer_maxSpeed,MAXSPEED);
- mCurrentSpeed=typedArray.getFloat(R.styleable.Speedometer_currentSpeed,0);
- ONCOLOR=typedArray.getColor(R.styleable.Speedometer_onColor,ONCOLOR);
- OFFCOLOR=typedArray.getColor(R.styleable.Speedometer_offColor,OFFCOLOR);
- SCALECOLOR=typedArray.getColor(R.styleable.Speedometer_scaleColor,SCALECOLOR);
- SCALESIZE=typedArray.getDimension(R.styleable.Speedometer_scaleTextSize,SCALESIZE);
- READSIZE=typedArray.getDimension(R.styleable.Speedometer_readingTextSize,READSIZE);
- typedArray.recycle();
- }
- public void init()
- {
- onMark=new Paint();
- onMark.setStyle(Paint.Style.STROKE);
- onMark.setColor(ONCOLOR);
- onMark.setStrokeWidth(35f);
- onMark.setShadowLayer(10f,2f,2f,ONCOLOR);
- onMark.setAntiAlias(true);
- offMark=new Paint(onMark);
- offMark.setColor(OFFCOLOR);
- offMark.setStyle(Paint.Style.FILL_AND_STROKE);
- offMark.setShadowLayer(2f,2f,2f,OFFCOLOR);
- scalePaint=new Paint(offMark);
- scalePaint.setTextSize(SCALESIZE);
- scalePaint.setColor(SCALECOLOR);
- scalePaint.setStrokeWidth(2f);
- scalePaint.setShadowLayer(10f,2f,2f,Color.CYAN);
- read=new Paint(scalePaint);
- read.setStyle(Paint.Style.FILL_AND_STROKE);
- read.setColor(Color.YELLOW);
- read.setTextSize(85f);
- read.setShadowLayer(5f,2f,2f,Color.YELLOW);
- read.setTypeface(Typeface.DEFAULT_BOLD);
- }
- public float getmCurrentSpeed()
- {
- return mCurrentSpeed;
- }
- public void setmCurrentSpeed(float current)
- {
- if(current>mMaxSpeed)
- mCurrentSpeed=mMaxSpeed;
- else if(current<0)
- mCurrentSpeed=0;
- else
- mCurrentSpeed=current;
- }
- @Override
- protected void onSizeChanged(int width,int height,int w,int h)
- {
- if(width>height)
- {
- radius=height/4;
- }
- else
- {
- radius=width/4;
- }
- oval.set(centerX-radius,centerY-radius,centerX+radius,centerY+radius);
- }
- @Override
- protected void onMeasure(int widthMeasureSpec,int heightMeasureSpec)
- {
- int widthMode=MeasureSpec.getMode(widthMeasureSpec);
- int widthSize=MeasureSpec.getSize(widthMeasureSpec);
- int heightMode=MeasureSpec.getMode(heightMeasureSpec);
- int heightSize=MeasureSpec.getSize(heightMeasureSpec);
- int choosenWidth=chooseDimension(widthMode,widthSize);
- int choosenHeight=chooseDimension(heightMode,heightSize);
- int choosenDimension=Math.min(choosenWidth,choosenHeight);
- centerX=choosenDimension/2;
- centerY=choosenDimension/2;
- setMeasuredDimension(choosenDimension,choosenDimension);
- }
- public int chooseDimension(int mode,int size)
- {
- if(mode==MeasureSpec.AT_MOST || mode==MeasureSpec.EXACTLY)
- {
- return size;
- }
- else
- {
- return getSize();
- }
- }
- public int getSize()
- {
- return 240;
- }
- @Override
- public void onDraw(Canvas canvas)
- {
- drawScaleBackground(canvas);
- drawScale(canvas);
- drawInscription(canvas);
- drawRead(canvas);
- }
- public void drawScaleBackground(Canvas canvas)
- {
- offPath.reset();
- for (int i = -180; i < 0; i=i+4) {
- offPath.addArc(oval,i,4f);
- }
- canvas.drawPath(offPath,offMark);
- }
- public void drawScale(Canvas canvas)
- {
- onPath.reset();
- for (int i = -180; i < (mCurrentSpeed/mMaxSpeed)*180-180; i=+4) {
- onPath.addArc(oval,i,4f);
- }
- canvas.drawPath(onPath,onMark);
- }
- public void drawInscription(Canvas canvas)
- {
- canvas.save(Canvas.MATRIX_SAVE_FLAG);
- canvas.rotate(-180,centerX,centerY);
- Path circle=new Path();
- double halfCircle=radius*Math.PI;
- for (int i = 0; i < mMaxSpeed; i=i+20) {
- circle.addCircle(centerX,centerY,radius, Path.Direction.CW);
- canvas.drawTextOnPath(String.format("%d",i),circle,(float)(i*halfCircle/mMaxSpeed),-30f,scalePaint);
- }
- canvas.restore();
- }
- public void drawRead(Canvas canvas)
- {
- Path path=new Path();
- String message=String.format("%d",(int)mCurrentSpeed);
- float[] width=new float[message.length()];
- read.getTextWidths(message,width);
- float adv=0;
- for (double w:width) {
- adv+=w;
- }
- path.moveTo(centerX-adv/2,centerY);
- path.lineTo(centerX+adv/2,centerY);
- canvas.drawTextOnPath(message,path,0,0,read);
- }
- public void onSpeedChanged(float speed)
- {
- this.setmCurrentSpeed(speed);
- this.invalidate();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement