2016-05-23 98 views
0

在我的應用程序中,需要在圖像上繪製一個圓。現在我使用畫布和Canvas擁有此圖像。我的要求是,我先畫一個半徑爲20的圓,然後在第二次清除前一個圓並繪製一個25半徑的圓,等等。如何在android中清除畫布?

以下是我的Fragment代碼。

import android.app.Activity; 
    import android.content.pm.ActivityInfo; 
    import android.graphics.Color; 
    import android.os.Bundle; 
    import android.support.v4.app.Fragment; 
    import android.support.v4.app.FragmentManager; 
    import android.view.LayoutInflater; 
    import android.view.View; 
    import android.view.ViewGroup; 
    import android.widget.Button; 

    /** 
    * Created by user on 5/23/2016. 
    */ 
    public class StepTwentyOneFragment extends Fragment { 

     private CanvasView customCanvas; 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
      View v = inflater.inflate(R.layout.step21_fragment, container, false); 
      customCanvas=(CanvasView)v.findViewById(R.id.signature_canvas); 

      final Button button1=(Button)v.findViewById(R.id.step18button1); 

      button1.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 

        if(v.getId()==R.id.step18button1){ 

         customCanvas.clear2(); 

         v.setBackgroundResource(R.drawable.button_border_5); 
         button1.setTextColor(Color.WHITE); 

        } 
       } 
      }); 





      return v; 
     } 

     public static StepTwentyOneFragment newInstance() { 

      StepTwentyOneFragment f = new StepTwentyOneFragment(); 
      Bundle b = new Bundle(); 

      f.setArguments(b); 

      return f; 
     } 


     @Override 
     public void setUserVisibleHint(boolean isVisibleToUser) { 
      super.setUserVisibleHint(isVisibleToUser); 
      if(isVisibleToUser) { 
       Activity a = getActivity(); 
       if(a != null) a.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
      } 
     } 

下面是我如何繪製圖像和圓。我的Fragment班學習這門課。

import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.graphics.Path; 
import android.graphics.PorterDuff; 
import android.graphics.drawable.Drawable; 
import android.util.AttributeSet; 
import android.util.Log; 
import android.view.MotionEvent; 
import android.view.View; 
import android.widget.Toast; 

/** 
* Created by user on 4/28/2016. 
*/ 
public class CanvasView extends View { 

    public int width; 
    public int height; 
    private Bitmap mBitmap; 
    private Canvas mCanvas; 
    private Path mPath; 
    Context context; 
    private Paint mPaint; 
    private float mX, mY; 
    private static final float TOLERANCE = 5; 

    public CanvasView(Context c, AttributeSet attrs) { 
     super(c, attrs); 
     context = c; 

     mPath = new Path(); 

     mPaint = new Paint(); 
     mPaint.setStrokeWidth(3); 
     mPaint.setColor(Color.CYAN); 
    } 

    // override onDraw 
    @Override 
    protected void onDraw(Canvas canvas) { 

     super.onDraw(canvas); 

     mCanvas=canvas; 

     Drawable d = getResources().getDrawable(R.drawable.circle_1); 

     int canvasHeight= canvas.getHeight(); 
     int canvasWidth= canvas.getWidth(); 

     Log.d("Height - ","/"+canvas.getHeight()); 
     Log.d("Width - ","/"+canvas.getWidth()); 

//   DisplayMetrics displaymetrics = new DisplayMetrics(); 
//  ((Activity)context).getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); 
//  int height = displaymetrics.heightPixels; 
//  int width = displaymetrics.widthPixels; 

     float h=canvasHeight/2; 
     float w=canvasWidth/2; 
     float r=(canvasWidth/2) - ((canvasWidth/2)/100)*20; 

     d.setBounds(0, 0, canvasWidth, canvasHeight); 
     d.draw(canvas); 

     canvas.drawCircle(w, h, r, mPaint); 
    } 

    public void clear2(){ 

     mCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR); 

     Drawable d = getResources().getDrawable(R.drawable.circle); 

     int canvasHeight= mCanvas.getHeight(); 
     int canvasWidth= mCanvas.getWidth(); 

     Log.d("Height - ","/"+mCanvas.getHeight()); 
     Log.d("Width - ","/"+mCanvas.getWidth()); 

//   DisplayMetrics displaymetrics = new DisplayMetrics(); 
//  ((Activity)context).getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); 
//  int height = displaymetrics.heightPixels; 
//  int width = displaymetrics.widthPixels; 

     float h=canvasHeight/2; 
     float w=canvasWidth/2; 
     float r=(canvasWidth/2) - ((canvasWidth/2)/100)*20; 

     d.setBounds(0, 0, canvasWidth, canvasHeight); 
     d.draw(mCanvas); 
     mCanvas.restore(); 

    } 

} 

但是,問題是我根本無法清除Canvas。即使我有一個名爲clear2的方法,它不會清除任何內容。另外我不確定一旦清除完成後如何畫出另一個圓圈。我相信這個「​​繪圖」代碼必須在我的Fragment類中?請幫忙。

+1

Canvas.drawColor(Color.TRANSPARENT,PorterDuff.Mode.CLEAR) –

+0

@AmitVaghela:已經完成了。 – Barrier

回答

1

您可以重新設置路徑一樣,

mPath.reset(); 

在這之後,你可以畫出你的新路徑。

這可能有助於你

+0

可以用更多的細節來解釋這個答案嗎?因爲這不起作用? – Barrier

0

我想你應該改變這樣的:

mCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR); 

mCanvas.drawColor(Color.WHITE) 

我想這是因爲,因爲你正在繪製在整個畫布上用透明顏色,它實際上並不影響畫布。因此,你仍然可以看穿前一幀。