简体   繁体   English

Android用不同的文本绘制圆

[英]Android Draw Circle with Different Text

My application draws a circle when the screen is pressed. 当按下屏幕时,我的应用程序绘制了一个圆圈。 I'm trying to put text on the circles according to how many there are the screen. 我正在尝试根据屏幕上的文字将文本放在圆圈上。 So if your first tap will give you a circle with the text C0, the second will give you a circle for C1, etc. 因此,如果您的第一笔点击将为您提供一个带有文本C0的圆圈,第二笔将为您提供一个针对C1的圆圈,依此类推。

Currently my code looks like 目前我的代码看起来像

lPaint = new Paint();
lPaint.setColor(Color.WHITE);
lPaint.setTextAlign(Paint.Align.CENTER);
lPaint.setTextSize(40);

nCanvas.drawCircle(v.x, v.y, 55, cPaint);
nCanvas.drawText("C"+i, v.x, v.y, lPaint);

Where vx and vy are the coordinators where you've touched the screen, and i is the circle counter. 其中vx和vy是您触摸过屏幕的协调器,而i是圆圈计数器。 This code starts off just fine, but after the first circle draw, it changes ALL the text for ALL the circles to the new i value. 这段代码刚好开始,但是在绘制第一个圆之后,它将所有圆的所有文本更改为新的i值。 How do I get around this? 我该如何解决?

Thanks 谢谢

Just create a new variable i, inside custom view. 只需在自定义视图中创建一个新变量i。 Then increment variable i inside on click and in onDraw method just draw circle, or whatever you want.For example: 然后在click和onDraw方法中增加变量i即可绘制圆或任意形状,例如:

package yourpackage.

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Build;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.View;

/**
 * Color view used for picking color for drawing
 */
public class ColorView extends View {

    private Paint drawPaint;
    private int color = ContextCompat.getColor(getContext(), android.R.color.black);
    private int i;


    public ColorView(Context context) {
        this(context, null);
    }

    public ColorView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ColorView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public ColorView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void init() {
        drawPaint = new Paint();
        drawPaint.setAntiAlias(true);
        drawPaint.setColor(color);
        drawPaint.setStyle(Paint.Style.FILL);
        drawPaint.setStrokeJoin(Paint.Join.ROUND);
        drawPaint.setStrokeCap(Paint.Cap.ROUND);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawRect(0, 0, 100, 200, drawPaint);
    }

    public void setColor(int color) {
        drawPaint.setColor(color);
        this.color = color;
    }

    public void onClick() {
        i++;
    }

    public int getColor() {
        return color;
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM