在我的应用程序中,需要在图像上绘制一个圆圈。现在我使用画布,Canvas
保存此图像。我的要求是我首先绘制一个半径为20的圆,然后在第二次清除前一个圆后绘制一个25半径的圆,依此类推。
以下是我的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
课程中?请帮忙。
答案 0 :(得分:1)
您可以重置路径,例如
mPath.reset();
之后你可以画出你的新路径。
这可能有助于你
答案 1 :(得分:1)
我认为你应该改变这个:
mCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
到
mCanvas.drawColor(Color.WHITE)
我认为这是因为你用透明的颜色绘制整个画布,它实际上并没有影响画布。因此,您仍然可以看到前一帧。