用记号笔圈

时间:2014-10-26 09:58:25

标签: android eclipse geometry marker

我希望我能在Android中实现我在此屏幕截图中显示的内容:

enter image description here

圈内的数字对我来说没问题(只需做一些帐号)。但我不知道如何放置圆圈或放置标记。

我的意思是,它就像一个速度表。然后我想根据数值,标记位于与数值匹配的圆的位置(该值在0-100范围内移动,因此圆圈中标记的最小值将为0或者显然是100。

我对此有点困惑,欢迎任何帮助。谢谢。

1 个答案:

答案 0 :(得分:1)

我为你创建了一个circleView ..只是试一试......

<强> CircleView.java

package com.example.err2;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class Circle extends View {

    int radius, number, fontSize = 80;
    Paint myPaint;

    public void setRadius(int radius) {
        this.radius = radius;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    private void init() {
        Log.d("init", "start");
        myPaint = new Paint();
        myPaint.setColor(Color.RED);
    }

    public Circle(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        init();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);
        canvas.save();
        // Drawing main crcle
        canvas.drawCircle(getWidth() / 2, 
            getHeight() / 2, 
            radius, myPaint);
        myPaint.setColor(Color.WHITE);
        canvas.drawCircle(getWidth() / 2, 
            getHeight() / 2, 
            radius - 10, myPaint);
        // end of main circle

        // drawing text-number
        myPaint.setColor(Color.BLACK);
        myPaint.setTextSize(fontSize);
        myPaint.setTextAlign(Align.CENTER);
        myPaint.setFakeBoldText(true);
        canvas.drawText(number + "", 
            getWidth() / 2, 
            getHeight() / 2 + fontSize / 3, myPaint);
        // end of drwaing text

        // drawing point on circle boundry
        // findPointLocation();
        double deg = number * 3.6f;
        double radians = Math.toRadians(deg);
        int px = (int) Math.abs(radius * Math.sin(radians));
        int py = (int) Math.abs(radius * Math.cos(radians));
        if (number <= 25) {
            px = -px;
        } else if (number <= 50) {
            px = -px;
            py = -py;
        } else if (number <= 75) {
            py = -py;
        }
        // end od find point
        myPaint.setColor(Color.GREEN);
        canvas.drawCircle(getWidth() / 2 + px, getHeight() / 2 + py, 15,
                myPaint);
        // end of drawing point
        canvas.restore();
    }
}

下面给出了主要活动代码

<强> MainActivity.java

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Circle circle=(Circle)findViewById(R.id.circle1);
        circle.setRadius(100);
        circle.setNumber(90);
    }
}

下面给出了xml代码 的 XML

<com.example.err2.Circle
    android:id="@+id/circle1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="73dp"
    android:background="#778888" />