2017-02-17 7 views
0

画面を押すと、アプリケーションが円を描きます。私は画面の数に応じてテキストを円に配置しようとしています。だからあなたの最初のタップはあなたにテキストC0の円を与えるならば、第二はあなたC1のためのサークルを与える、など異なるテキストのAndroid Draw Circle

現在、私のコードは

VXとVYがコーディネーターある
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); 

のように見えますあなたが画面に触れたところで、私は円のカウンターです。このコードはうまく始まりますが、最初の円を描いた後、すべての円のテキストが新しいi値に変更されます。これをどうやって回避するのですか?

ありがとうございました

答えて

0

カスタムビュー内に新しい変数iを作成するだけです。次に、変数onをクリックしてインクリメントし、onDrawメソッドで円を描くだけです。たとえば、次のようにします。

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; 
    } 

} 
関連する問題