Advertisement
Guest User

Untitled

a guest
Dec 30th, 2014
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.27 KB | None | 0 0
  1. package com.example.testarc;
  2.  
  3. import android.content.Context;
  4. import android.graphics.Canvas;
  5. import android.graphics.Color;
  6. import android.graphics.EmbossMaskFilter;
  7. import android.graphics.MaskFilter;
  8. import android.graphics.Paint;
  9. import android.graphics.Path;
  10. import android.graphics.PathMeasure;
  11. import android.graphics.RectF;
  12. import android.graphics.Paint.Align;
  13. import android.graphics.Paint.Style;
  14. import android.util.AttributeSet;
  15. import android.view.View;
  16. import android.view.View.MeasureSpec;
  17.  
  18. public class testview extends View {
  19.  
  20.     public testview(Context context, AttributeSet attrs, int defStyleAttr) {
  21.         super(context, attrs, defStyleAttr);
  22.         // TODO Auto-generated constructor stub
  23.     }
  24.    
  25.     public testview(Context context)
  26.     {
  27.         super(context);
  28.     }
  29.     public testview(Context context, AttributeSet attrs)
  30.     {
  31.         super(context, attrs);
  32.     }
  33.  
  34.    
  35.     @Override
  36.     protected void onDraw(Canvas canvas)
  37.     {
  38.         drawTuningLines(canvas, getWidth() / 2, getHeight(), getHeight() * .2f);
  39.     }
  40.    
  41.     protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec){
  42.        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  43.  
  44.        int parentWidth = MeasureSpec.getSize(widthMeasureSpec); // receives parents width in pixel
  45.        int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
  46.  
  47.         //your desired sizes, converted from pixels to setMeasuredDimension's unit
  48.         final int desiredWSpec = parentWidth;
  49.         final int desiredHSpec = MeasureSpec.makeMeasureSpec((int) (parentHeight * .4f), MeasureSpec.EXACTLY);
  50.         this.setMeasuredDimension(desiredWSpec, desiredHSpec);
  51.     }
  52.    
  53.     private void drawTuningLines(Canvas canvas, float startX, float startY,
  54.             float stopY) {
  55.         float width = canvas.getWidth();           
  56.         float height = canvas.getHeight();
  57.         float textHeight = 36;
  58.        
  59.         Paint tuningLinePaint = new Paint();           
  60.         tuningLinePaint.setStrokeWidth(3);
  61.         tuningLinePaint.setColor(Color.BLACK);
  62.         tuningLinePaint.setTextSize(textHeight * 1.3f);
  63.         tuningLinePaint.setStyle(Style.STROKE);
  64.         tuningLinePaint.setShadowLayer(5, -4, 4, Color.GRAY); // Shadow of the needle
  65.        
  66.         float rotateFactor = 3f;       
  67.         float radius = startY - stopY; 
  68.        
  69.         RectF oval = new RectF();
  70.         float a = (float)Math.abs( Math.sqrt( Math.abs( Math.pow(radius, 2)  -  Math.pow(startX, 2) ) ) );
  71.         float endY = radius - a + stopY;
  72.         RectF rect = new RectF(startX - radius, stopY + textHeight * .7f, startX + radius, startY + (startY - stopY ) - textHeight * .7f);
  73.         oval.set(rect);
  74.         //float startAngle = getSemicircle(startX + (float)(radius * Math.sin(angle)), startY + (float)(radius * Math.cos(angle)), startX + (float)(radius * Math.sin(-angle)), startY + (float)(radius * Math.cos(-angle)), oval, Side.LEFT);
  75.         //oval.set(getSemicircle(0, height / 2, 200, height / 2, oval, Side.LEFT));
  76.         int startAngle = (int) ((180 / Math.PI * Math.atan2(a, startX)) + 7);
  77.         //canvas.drawArc(oval,  startAngle + 180, 2* (90 - startAngle), false, tuningLinePaint);
  78.        
  79.         Path path = new Path();
  80.         path.addArc(oval,  startAngle + 180, 2* (90 - startAngle));
  81.        
  82.         canvas.drawPath(path, tuningLinePaint);
  83.        
  84.         canvas.save();
  85.         canvas.translate(width / 2, textHeight);
  86.        
  87.         RectF bounds = new RectF();
  88.         path.computeBounds(bounds, true);
  89.        
  90.         PathMeasure arcPath =  new PathMeasure(path, false);
  91.         float xscale = (bounds.right - bounds.left) / 7f;
  92.        
  93.         canvas.restore();
  94.         canvas.translate(((bounds.right - bounds.left) / 2) + ((width - (bounds.right - bounds.left)) / 2), 0);
  95.        
  96.         for (int i = 0; i <= 15; i++)
  97.         {
  98.             float x = i * xscale / 5;
  99.            
  100.             float smallnessFactor = 2;
  101.            
  102.             float[] pos = { 0f, 0f };
  103.             float[] tan = { 0f, 0f };
  104.             float length = arcPath.getLength();
  105.             arcPath.getPosTan((length / 2) + (i * length / 7 / 5), pos, tan);
  106.             canvas.save();
  107.             float angleRotate = (float) (Math.atan2( x, radius) * 180 / Math.PI);
  108.             canvas.rotate(angleRotate, x, pos[1]);
  109.             canvas.drawLine(x, pos[1] + textHeight / smallnessFactor, x, pos[1] - textHeight / smallnessFactor, tuningLinePaint);
  110.             canvas.restore();
  111.             canvas.save();
  112.             canvas.rotate(-angleRotate, -x, pos[1]);
  113.             canvas.drawLine(-x, pos[1] + textHeight / smallnessFactor, -x, pos[1] - textHeight / smallnessFactor, tuningLinePaint);
  114.             canvas.restore();
  115.         }
  116.     }
  117.  
  118.  
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement