在我的應用程序中,需要在圖像上繪製一個圓。現在我使用畫布和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
類中?請幫忙。
Canvas.drawColor(Color.TRANSPARENT,PorterDuff.Mode.CLEAR) –
@AmitVaghela:已經完成了。 – Barrier