Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.testarc;
- import android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.EmbossMaskFilter;
- import android.graphics.MaskFilter;
- import android.graphics.Paint;
- import android.graphics.Path;
- import android.graphics.PathMeasure;
- import android.graphics.RectF;
- import android.graphics.Paint.Align;
- import android.graphics.Paint.Style;
- import android.util.AttributeSet;
- import android.view.View;
- import android.view.View.MeasureSpec;
- public class testview extends View {
- public testview(Context context, AttributeSet attrs, int defStyleAttr) {
- super(context, attrs, defStyleAttr);
- // TODO Auto-generated constructor stub
- }
- public testview(Context context)
- {
- super(context);
- }
- public testview(Context context, AttributeSet attrs)
- {
- super(context, attrs);
- }
- @Override
- protected void onDraw(Canvas canvas)
- {
- drawTuningLines(canvas, getWidth() / 2, getHeight(), getHeight() * .2f);
- }
- protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec){
- super.onMeasure(widthMeasureSpec, heightMeasureSpec);
- int parentWidth = MeasureSpec.getSize(widthMeasureSpec); // receives parents width in pixel
- int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
- //your desired sizes, converted from pixels to setMeasuredDimension's unit
- final int desiredWSpec = parentWidth;
- final int desiredHSpec = MeasureSpec.makeMeasureSpec((int) (parentHeight * .4f), MeasureSpec.EXACTLY);
- this.setMeasuredDimension(desiredWSpec, desiredHSpec);
- }
- private void drawTuningLines(Canvas canvas, float startX, float startY,
- float stopY) {
- float width = canvas.getWidth();
- float height = canvas.getHeight();
- float textHeight = 36;
- Paint tuningLinePaint = new Paint();
- tuningLinePaint.setStrokeWidth(3);
- tuningLinePaint.setColor(Color.BLACK);
- tuningLinePaint.setTextSize(textHeight * 1.3f);
- tuningLinePaint.setStyle(Style.STROKE);
- tuningLinePaint.setShadowLayer(5, -4, 4, Color.GRAY); // Shadow of the needle
- float rotateFactor = 3f;
- float radius = startY - stopY;
- RectF oval = new RectF();
- float a = (float)Math.abs( Math.sqrt( Math.abs( Math.pow(radius, 2) - Math.pow(startX, 2) ) ) );
- float endY = radius - a + stopY;
- RectF rect = new RectF(startX - radius, stopY + textHeight * .7f, startX + radius, startY + (startY - stopY ) - textHeight * .7f);
- oval.set(rect);
- //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);
- //oval.set(getSemicircle(0, height / 2, 200, height / 2, oval, Side.LEFT));
- int startAngle = (int) ((180 / Math.PI * Math.atan2(a, startX)) + 7);
- //canvas.drawArc(oval, startAngle + 180, 2* (90 - startAngle), false, tuningLinePaint);
- Path path = new Path();
- path.addArc(oval, startAngle + 180, 2* (90 - startAngle));
- canvas.drawPath(path, tuningLinePaint);
- canvas.save();
- canvas.translate(width / 2, textHeight);
- RectF bounds = new RectF();
- path.computeBounds(bounds, true);
- PathMeasure arcPath = new PathMeasure(path, false);
- float xscale = (bounds.right - bounds.left) / 7f;
- canvas.restore();
- canvas.translate(((bounds.right - bounds.left) / 2) + ((width - (bounds.right - bounds.left)) / 2), 0);
- for (int i = 0; i <= 15; i++)
- {
- float x = i * xscale / 5;
- float smallnessFactor = 2;
- float[] pos = { 0f, 0f };
- float[] tan = { 0f, 0f };
- float length = arcPath.getLength();
- arcPath.getPosTan((length / 2) + (i * length / 7 / 5), pos, tan);
- canvas.save();
- float angleRotate = (float) (Math.atan2( x, radius) * 180 / Math.PI);
- canvas.rotate(angleRotate, x, pos[1]);
- canvas.drawLine(x, pos[1] + textHeight / smallnessFactor, x, pos[1] - textHeight / smallnessFactor, tuningLinePaint);
- canvas.restore();
- canvas.save();
- canvas.rotate(-angleRotate, -x, pos[1]);
- canvas.drawLine(-x, pos[1] + textHeight / smallnessFactor, -x, pos[1] - textHeight / smallnessFactor, tuningLinePaint);
- canvas.restore();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement