Android:在屏幕上绘制圆圈

时间:2016-04-01 13:31:18

标签: android android-view draw android-bitmap

我无法在屏幕上画圆圈。我不知道我是否正确接近这个,或者我是否必须使用位图。下面是我创建的Circle类,专门为我的规范创建一个圆圈。

package com.example.alex.parkinsonsdiseaseapp;

import android.view.View;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;

public class Circle extends View {
    private final float x;
    private final float y;
    private final int r;
    private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

    public Circle(Context context, float x, float y, int r) {
        super(context);
        mPaint.setColor(0x000000);
        this.x = x;
        this.y = y;
        this.r = r;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawCircle(x, y, r, mPaint);
    }
}

以下是使用上述类的Activity。

package com.example.alex.parkinsonsdiseaseapp;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;
import android.widget.LinearLayout.LayoutParams;

public class FingerTappingActivity extends Activity {

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

        LinearLayout circle = (LinearLayout) findViewById(R.id.lt);
        View circleView = new Circle(this, 100, 100, 100);
        circleView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        circle.addView(circleView);
        Toast.makeText(FingerTappingActivity.this, "Test", Toast.LENGTH_SHORT).show();

    }
}

2 个答案:

答案 0 :(得分:0)

您应该在CircleView中设置LayoutParams

View circleView = new Circle(this, 50, 50, 100)
circleView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
circle.addView(circleView);

答案 1 :(得分:0)

如果只是一个你想要的圆圈,请使用xml drawable。检查How to define a circle shape in an android xml drawable file?中的答案。

在拥有xml drawable之后,在你的布局中创建一个imageView并将其包含在你的android:src

有了这个,您不必担心LayoutParam等。您可以在imageView中定义它并将它们相应地放置在您在布局中的位置。

已更新

嗨Alex,根据您的需要,我有一个完整的代码,可以在视图上生成随机圆圈。

public class RandomCircles extends View {

    public int height;
    Context context;
    private Paint mPaint;

    private static final int MAX_SIZE = 200;
    private List<Data> dataList = new ArrayList<>();

    public RandomCircles(Context c, AttributeSet attrs) {
        super(c, attrs);
        context = c;
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.BLACK);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeWidth(4f);

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        for (int i = 0; i <= 50; i++) {
            int x = (int)(Math.random() * w);
            int y = (int)(Math.random() * h);
            int size = (int)(Math.random() * MAX_SIZE);
            addCircle(x, y, size);
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        for (Data data : dataList) {
            int size = data.size;
            mPaint.setAlpha(255 - size);
            canvas.drawCircle(data.x, data.y, data.size, mPaint);
        }
    }

    private void addCircle(float x, float y, int size) {
        dataList.add(new Data(x, y, size));
    }


    class Data {
        public float x;
        public float y;
        public int size;

        public Data(float x, float y, int size) {
            this.x = x;
            this.y = y;
            this.size = size;
        }
    }
}

从中创建一个类并在布局中启动它。

<com.YOU_PACKAGE_NAME.RandomCircles
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

你会得到类似下面的内容。虽然不完全相同,因为圆圈是随机的。根据需要编辑代码以满足您的需求。欢呼声。

enter image description here