3

xmlレイアウトから参照されるカスタムビューを作成しました。ビューをクリアするためのボタンを追加しました。クリックするとキャンバス領域がクリアされます。xmlレイアウトファイルにonClickイベントを追加しました。ビュー/キャンバス全体をクリアするためのコードをどこに追加すればよいですか?コードの一部を追加しました。(これは何もクリアしていません)。アクティビティ、ビュー、レイアウトファイルを以下の順序で追加しました。

public class CustomViewActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

    }

    public void clearLine(View v) {

    new CustomView(CustomViewActivity.this, null).clearCanvas();        
  } 

}

public class CustomView extends View {

    private Paint paint = new Paint();
      private Path path = new Path();
      public Boolean clearCanvas = false;

      public CustomView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);         
        }

    public CustomView(Context context,AttributeSet attrs ) {
        super(context,attrs);
        paint.setAntiAlias(true);
        paint.setColor(Color.BLUE);
        paint.setTextSize(20);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setStrokeWidth(5f);
    }

    protected void onDraw(Canvas canvas) {
    if(clearCanvas)
        {  // Choose the colour you want to clear with.
            canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
            //canvas.drawColor(0, Mode.CLEAR);
            clearCanvas = false;            
        }

        super.onDraw(canvas);
        canvas.drawText("Hello World", 5, 30, paint);
        canvas.drawPath(path, paint);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

    //int action = event.getAction() & MotionEvent.ACTION_MASK;

       float eventX = event.getX();
        float eventY = event.getY();

        switch (event.getAction()) {
          case MotionEvent.ACTION_DOWN:
            path.moveTo(eventX, eventY);

            return true;
          case MotionEvent.ACTION_MOVE: 
            path.lineTo(eventX, eventY);
            break;
          case MotionEvent.ACTION_UP:
            // nothing to do 
           break;
          default:
            return false;
        }

        // Schedules a repaint.
        invalidate();
        return true;

    }
    public void clearCanvas(){

            clearCanvas = true;
            postInvalidate();
            //canvas.drawColor(0, Mode.CLEAR);

        }

}

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >


           <com.example.CustomViewEvent.CustomView
               android:id="@+id/customView"
               android:layout_width="fill_parent"
               android:layout_height="wrap_content" />

           <Button
               android:id="@+id/button1"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_alignParentBottom="true"
               android:layout_centerHorizontal="true"
               android:layout_marginBottom="28dp"
               android:onClick="clearLine"
               android:text="CLEAR" />

</RelativeLayout>
4

1 に答える 1

5

onDraw メソッドでキャンバスにアクセスする必要があります。

したがって、グローバル変数を使用する場合は、ボタン クリック メソッドで true に設定します。OnDraw では、ステータスを確認し、必要に応じてキャンバスをクリアできます。(その後、毎回実行しないように false に戻します)。

使用法については、以下のコードを参照してください。

public Boolean clearCanvas = false;

protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(clearCanvas)
        {  // Choose the colour you want to clear with.
            canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
            clearCanvas = false;
        }
        canvas.drawPath(path, paint);       
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

    float eventX = event.getX();
        float eventY = event.getY();

        switch (event.getAction()) {
          case MotionEvent.ACTION_DOWN:
            path.moveTo(eventX, eventY);

            return true;
          case MotionEvent.ACTION_MOVE: 
            path.lineTo(eventX, eventY);
            break;
          case MotionEvent.ACTION_UP:
            // nothing to do 
           break;
          default:
            return false;
        }

        // Schedules a repaint.
        invalidate();
        return true;

    }
// this is the method which will be invoked from main activity class for clearing whatever //is in the view/canvas 
         public void clearCanvas(){

            //canvas.drawColor(0, Mode.CLEAR);

            clearCanvas = true;
            invalidate();
        }

}

EDIT: あなたの新しいコードを見ると、いくつかの問題があります。

あなたが正しい見方をしていないという事実に関係していると思います。

まず、既存のビューのインスタンスを取得します。その後、クリアできます。それの間違った存在しないインスタンスではなく。

 CustomView cv = (CustomView)findViewById(R.id.customView); 
 cv.clearCanvas();   

invalidate();他のものを試してみてくださいpostInvalidate();

postInvalidate()非 UI スレッドで実行している場合に使用します。

于 2012-09-28T15:53:12.800 に答える