2011-07-08 10 views
5

私はキャンバスに線を描くことができるシンプルな描画アプリケーションを作った。さまざまな色選択ボタンを追加しました。私が今問題になっているのは、カラーボタンをクリックして描画を続けると、以前に描かれた線もすべて新しく選択された色に変更されるということです。キャンバスに複数の色で描画する

私は、その目的のためにペイント(またはパス)リストを使用することに関するいくつかのフォーラム投稿を見つけました。しかし、私は完全に解決策を理解することができませんでした。誰かが実用的な例のいくつかのコードを投稿できますか?

は事前にありがとうございました。

+0

PaintオブジェクトのArrayListを作成し、ボタンのクリックごとに新しいPaintオブジェクトを初期化することによって、線の色を設定することができます。 –

答えて

1

が、私はそれを行っている、それは私のためにgreateの作品、これを試してみてください。

public void onClick(View view){ 

     switch (view.getId()){ 
      case R.id.colorRedBtn: 

       //Toast.makeText(getApplicationContext(), "Red", Toast.LENGTH_SHORT).show(); 
       currentPaint = new Paint(); 
       currentPaint.setColor(0xFFFF0000); 
       currentPaint.setDither(true); 
       currentPaint.setStyle(Paint.Style.STROKE); 
       currentPaint.setStrokeJoin(Paint.Join.ROUND); 
       currentPaint.setStrokeCap(Paint.Cap.ROUND); 
       currentPaint.setStrokeWidth(3); 
       break; 
      case R.id.colorBlueBtn: 

       //Toast.makeText(getApplicationContext(), "Green", Toast.LENGTH_SHORT).show(); 
        currentPaint = new Paint(); 
       currentPaint.setColor(0xFF00FF00); 
       currentPaint.setDither(true); 
       currentPaint.setStyle(Paint.Style.STROKE); 
       currentPaint.setStrokeJoin(Paint.Join.ROUND); 
       currentPaint.setStrokeCap(Paint.Cap.ROUND); 
       currentPaint.setStrokeWidth(3); 
       break; 
      case R.id.colorGreenBtn: 

       //Toast.makeText(getApplicationContext(), "Blue", Toast.LENGTH_SHORT).show(); 
       currentPaint = new Paint(); 
       currentPaint.setColor(0xFF0000FF); 
       currentPaint.setDither(true); 
       currentPaint.setStyle(Paint.Style.STROKE); 
       currentPaint.setStrokeJoin(Paint.Join.ROUND); 
       currentPaint.setStrokeCap(Paint.Cap.ROUND); 
       currentPaint.setStrokeWidth(3); 

       break; 

      case R.id.colorBlackBtn: 

       //Toast.makeText(getApplicationContext(), "Black", Toast.LENGTH_SHORT).show(); 
       currentPaint = new Paint(); 
       currentPaint.setColor(0xFF000000); 
       currentPaint.setDither(true); 
       currentPaint.setStyle(Paint.Style.STROKE); 
       currentPaint.setStrokeJoin(Paint.Join.ROUND); 
       currentPaint.setStrokeCap(Paint.Cap.ROUND); 
       currentPaint.setStrokeWidth(3); 
       break; 
      } 
} 

希望すると、これが役立ちます。 お楽しみください。

6
  1. Canvas
  2. Paint

    Paint bluePaint = new Paint(); 
    p1.setColor(Color.BLUE); 
    
    Paint greenPaint = new Paint(); 
    p2.setColor(Color.GREEN); 
    
    canvas.drawLine(1.0, 1.0, 2.0, 2.0, bluePaint); //blue line 
    canvas.drawLine(2.0, 1.0, 1.0, 2.0, greenPaint); //green line 
    
関連する問題