
我已经制作了一个自定义视图,它是从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>解决方法:
您需要做的是访问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(); }}编辑:
看看你的新代码,我看到了一些问题.
我认为它不会清除你没有清除正确视图的事实.
首先,获取现有视图的实例.然后你可以清除它.而不是错误的非现有实例.
CustomVIEw cv = (CustomVIEw)findVIEwByID(R.ID.customVIEw); cv.clearCanvas(); 尝试invalIDate(); else postInvalIDate();一个应该工作.
postInvalIDate()适用于在非UI线程上运行的情况.
总结以上是内存溢出为你收集整理的android – 清除画布区域全部内容,希望文章能够帮你解决android – 清除画布区域所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)