如何清除机器人有何看法? [英] how to clear view in android?
本文介绍了如何清除机器人有何看法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经创建了Android的一个简单的演示应用程序绘制,现在任何一个可以说我,我怎么可以删除视图和清晰的屏幕只需点击一下鼠标。我已经试过如下:请帮我做这....感谢名单提前..!
main.java
包com.example.singletouch;
进口com.example.singletouch.R.attr;
进口android.os.Bundle;
进口android.app.Activity;
进口android.graphics.Canvas;
进口android.graphics.Color;
进口android.graphics.Paint;
进口android.graphics.Path;
进口android.view.LayoutInflater;
进口android.view.Menu;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.ImageView;
进口android.widget.LinearLayout;
公共类MainActivity延伸活动{
ImageView的笔;
SingleTouchView mDrawView;
ImageView的删除;
的LinearLayout笔;
的LinearLayout PEN1,PEN2,PEN3,pen4;
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
mDrawView =(SingleTouchView)findViewById(R.id.myview);
笔=(ImageView的)findViewById(R.id.pen);
笔=(的LinearLayout)findViewById(R.id.linear);
pens.setVisibility(View.GONE);
PEN1 =(的LinearLayout)findViewById(R.id.pen1);
PEN2 =(的LinearLayout)findViewById(R.id.pen2);
PEN3 =(的LinearLayout)findViewById(R.id.pen3);
pen4 =(的LinearLayout)findViewById(R.id.pen4);
删除=(ImageView的)findViewById(R.id.remove);
/ *
* pen1.setOnClickListener(本); pen2.setOnClickListener(本);
* pen3.setOnClickListener(本); pen4.setOnClickListener(本);
* /
pen.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(视图v){
// TODO自动生成方法存根
pens.setVisibility(View.VISIBLE);
}
}); pens.setVisibility(View.GONE);
pen1.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(视图v){
mDrawView.setPen(SingleTouchView.DrawingPens.PEN_1);
pens.setVisibility(View.GONE);
}
});
pen2.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(视图v){
mDrawView.setPen(SingleTouchView.DrawingPens.PEN_2);
pens.setVisibility(View.GONE);
}
});
pen3.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(视图v){
pens.setVisibility(View.GONE);
mDrawView.setPen(SingleTouchView.DrawingPens.PEN_3);
}
});
pen4.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(视图v){
pens.setVisibility(View.GONE);
mDrawView.setPen(SingleTouchView.DrawingPens.PEN_4);
}
});
remove.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(视图v){
}
});
}
}
SingleTouchView.java
包com.example.singletouch;
进口java.util.AbstractMap中;
进口的java.util.Map;
进口java.util.concurrent.ConcurrentLinkedQueue中;
进口android.R.color;
进口android.content.Context;
进口android.graphics.Bitmap;
进口android.graphics.Canvas;
进口android.graphics.Color;
进口android.graphics.Paint;
进口android.graphics.Path;
进口android.graphics.PorterDuff.Mode;
进口android.util.AttributeSet;
进口android.view.MotionEvent;
进口android.view.View;
进口android.widget.ImageView;
进口android.widget.Switch;
公共类SingleTouchView扩展视图{
公众诠释的宽度;
公众诠释的高度;
公共位图mBitmap;
公共帆布mCanvas;
公共道路的mpath;
市民漆mBitmapPaint;
上下文语境;
市民漆circlePaint;
公共路径circlePath;
公共枚举DrawingPens {
PEN_1(6),
PEN_2(4),
PEN_3(2),
PEN_4(1);
最后的公开油漆mPaint;
/ **
*构造函数
*
* @参数宽度笔划的宽度
* @参数彩笔画的颜色
* /
私人DrawingPens(最终诠释宽度){
mPaint =新的油漆();
mPaint.setAntiAlias(真正的);
mPaint.setStrokeWidth(宽);
//mPaint.setColor(color);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
}
/ **
* @返回相应的涂料
* /
漆getPaint(){
返回mPaint;
}
}
公共SingleTouchView(最终上下文的背景下){
超(上下文);
的init(上下文);
}
公共SingleTouchView(最终上下文的背景下,最终的AttributeSet ATTRS){
超(背景下,ATTRS);
的init(上下文);
}
公共SingleTouchView(最终上下文的背景下,最终的AttributeSet ATTRS,最终诠释defStyle){
超(背景下,ATTRS,defStyle);
的init(上下文);
}
/ **要存储油漆 - 路径关系* /
// TODO:这取决于具体的限制,更优的方式,可以发现
私人的ConcurrentLinkedQueue<为Map.Entry<路径,DrawingPens>> mPaths =新的ConcurrentLinkedQueue<为Map.Entry<路径,DrawingPens>>();
/ **缓存当前路径,< B>注意:< / B>这个字段是尾在mPaths并且只用于缓存它,不拉丝* /
私人路径mCurrentPath;
/ **
* Inits内部意见的数据,应该从每一个构造被称为
*
* @参数方面{@link语境}
* /
私人无效的init(最终上下文的背景下){
/ * TODO:如果涂料某些值不能在静态上下文中确定(枚举),
然后油漆应该在这里创建,并通过EnumMap的使用* /
//初始笔
setPen(DrawingPens.PEN_1);
}
@覆盖
公共无效的OnDraw(帆布油画){
//刚刚绘制背景
super.onDraw(画布);
//画出所有路径
对于(Map.Entry的<路径,DrawingPens>条目:mPaths){
canvas.drawPath(entry.getKey(),entry.getValue()getPaint());
}
}
@覆盖
公共布尔的onTouchEvent(MotionEvent我){
浮eventX = me.getX();
浮eventY = me.getY();
开关(me.getAction()){
案例MotionEvent.ACTION_DOWN:
mCurrentPath.moveTo(eventX,eventY);
返回true;
案例MotionEvent.ACTION_MOVE:
mCurrentPath.lineTo(eventX,eventY);
打破;
案例MotionEvent.ACTION_UP:
打破;
}
无效();
返回true;
}
/ **
*二传手新笔
*
*用于下一个图形@param笔{@link DrawingPens}
* /
公共无效setPen(最终DrawingPens笔){
//把最新的产品队列
mCurrentPath =新路径();
mPaths.add(新AbstractMap.SimpleImmutableEntry<路径,DrawingPens>(mCurrentPath,笔));
}
公共无效清除(视图v){
}
}
的main.xml
< RelativeLayout的的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
工具:上下文=MainActivity。>
< com.example.singletouch.SingleTouchView
机器人:ID =@ + ID / MyView的
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:layout_above =@ + ID /笔/>
<的LinearLayout
机器人:ID =@ + ID / linearLayout1
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentBottom =真
机器人:背景=@可绘制/菜单栏
机器人:填充=2DP
机器人:weightSum =4>
< ImageView的
机器人:ID =@ + ID /笔
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =FILL_PARENT
机器人:layout_alignParentBottom =真
机器人:layout_weight =1
机器人:重力=中心
机器人:SRC =@可绘制/笔/>
< ImageView的
机器人:ID =@ + ID /橡皮擦
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =FILL_PARENT
机器人:layout_alignParentBottom =真
机器人:layout_weight =1
机器人:SRC =@可绘制/橡皮擦/>
< ImageView的
机器人:ID =@ + ID /彩色
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =FILL_PARENT
机器人:layout_alignParentBottom =真
机器人:layout_weight =1
机器人:SRC =@可绘制/彩色/>
< ImageView的
机器人:ID =@ + ID /删除
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =FILL_PARENT
机器人:layout_alignParentBottom =真
机器人:layout_weight =1
机器人:SRC =@可绘制/删除/>
< / LinearLayout中>
<的LinearLayout
机器人:ID =@ + ID /线性
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_above =@ + ID / linearLayout1
机器人:layout_alignParentLeft =真
机器人:后台=#EEEEEE
机器人:方向=横向
机器人:能见度=水涨船高
机器人:weightSum =4>
<的LinearLayout
机器人:ID =@ + ID / PEN1
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =FILL_PARENT
机器人:layout_weight =1
机器人:重力=中心>
< ImageView的
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:SRC =@可绘制/ PEN1/>
< / LinearLayout中>
<的LinearLayout
机器人:ID =@ + ID / PEN2
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =FILL_PARENT
机器人:layout_weight =1
机器人:重力=中心>
< ImageView的
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =FILL_PARENT
机器人:SRC =@可绘制/ PEN2/>
< / LinearLayout中>
<的LinearLayout
机器人:ID =@ + ID / PEN3
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =FILL_PARENT
机器人:layout_weight =1
机器人:重力=中心>
< ImageView的
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_weight =1
机器人:SRC =@可绘制/ PEN3/>
< / LinearLayout中>
<的LinearLayout
机器人:ID =@ + ID / pen4
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =FILL_PARENT
机器人:layout_weight =1
机器人:重力=中心>
< ImageView的
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_weight =1
机器人:SRC =@可绘制/ pen4/>
< / LinearLayout中>
< / LinearLayout中>
< / RelativeLayout的>
解决方案
物权法的使用...
mCanvas.drawColor(Color.BLACK);
...清除画布,或者你想在后台任何其他颜色。例如,
remove.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(视图v){
//明确的帆布内容
mCanvas.drawColor(Color.BLACK);
//创建新的路径列表;旧的人会被垃圾收集
的ConcurrentLinkedQueue<为Map.Entry<路径,DrawingPens>> mPaths =
新的ConcurrentLinkedQueue<为Map.Entry<路径,DrawingPens>>();
pens.setVisibility(View.VISIBLE);
}
PS :从它的容器使用动态删除视图 removeView(....)
,例如:
((ViewGroup中)viewToRemove.getParent())removeView(viewToRemove)。
希望这有助于...干杯!
I have created a simple demo application in android for drawing ,now can any one say me how can i remove view and clear screen in just one click .I have tried as below:please help me to do it....thanx in advance..!
main.java
package com.example.singletouch;
import com.example.singletouch.R.attr;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
ImageView pen;
SingleTouchView mDrawView;
ImageView remove;
LinearLayout pens;
LinearLayout pen1, pen2, pen3, pen4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDrawView = (SingleTouchView) findViewById(R.id.myview);
pen = (ImageView) findViewById(R.id.pen);
pens = (LinearLayout) findViewById(R.id.linear);
pens.setVisibility(View.GONE);
pen1 = (LinearLayout) findViewById(R.id.pen1);
pen2 = (LinearLayout) findViewById(R.id.pen2);
pen3 = (LinearLayout) findViewById(R.id.pen3);
pen4 = (LinearLayout) findViewById(R.id.pen4);
remove=(ImageView)findViewById(R.id.remove);
/*
* pen1.setOnClickListener(this); pen2.setOnClickListener(this);
* pen3.setOnClickListener(this); pen4.setOnClickListener(this);
*/
pen.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
pens.setVisibility(View.VISIBLE);
}
});pens.setVisibility(View.GONE);
pen1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mDrawView.setPen(SingleTouchView.DrawingPens.PEN_1);
pens.setVisibility(View.GONE);
}
});
pen2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mDrawView.setPen(SingleTouchView.DrawingPens.PEN_2);
pens.setVisibility(View.GONE);
}
});
pen3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
pens.setVisibility(View.GONE);
mDrawView.setPen(SingleTouchView.DrawingPens.PEN_3);
}
});
pen4.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
pens.setVisibility(View.GONE);
mDrawView.setPen(SingleTouchView.DrawingPens.PEN_4);
}
});
remove.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
}
SingleTouchView.java
package com.example.singletouch;
import java.util.AbstractMap;
import java.util.Map;
import java.util.concurrent.ConcurrentLinkedQueue;
import android.R.color;
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.Mode;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.Switch;
public class SingleTouchView extends View{
public int width;
public int height;
public Bitmap mBitmap;
public Canvas mCanvas;
public Path mPath;
public Paint mBitmapPaint;
Context context;
public Paint circlePaint;
public Path circlePath;
public enum DrawingPens {
PEN_1(6),
PEN_2(4),
PEN_3(2),
PEN_4(1);
final public Paint mPaint;
/**
* Constructor
*
* @param width width of stroke
* @param color color of stroke
*/
private DrawingPens(final int width) {
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setStrokeWidth(width);
//mPaint.setColor(color);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
}
/**
* @return corresponding paint
*/
Paint getPaint() {
return mPaint;
}
}
public SingleTouchView(final Context context) {
super(context);
init(context);
}
public SingleTouchView(final Context context, final AttributeSet attrs) {
super(context, attrs);
init(context);
}
public SingleTouchView(final Context context, final AttributeSet attrs, final int defStyle) {
super(context, attrs, defStyle);
init(context);
}
/** To store Paint - Path relation */
// TODO: depending on exact limits, more optimal ways can be found
private ConcurrentLinkedQueue<Map.Entry<Path, DrawingPens>> mPaths = new ConcurrentLinkedQueue<Map.Entry<Path, DrawingPens>>();
/** Cached current path, <b>NOTE:</b> this field is tail at mPaths and is used it only for caching, not drawing */
private Path mCurrentPath;
/**
* Inits internal views data, should be called from every constructor
*
* @param context {@link Context}
*/
private void init(final Context context) {
/* TODO: if some values of paints cannot be determined in static context (in enum),
then Paints should be created here and used via EnumMap */
// Initial pen
setPen(DrawingPens.PEN_1);
}
@Override
public void onDraw(Canvas canvas){
// just to draw background
super.onDraw(canvas);
// Draw all paths
for (Map.Entry<Path, DrawingPens> entry : mPaths) {
canvas.drawPath(entry.getKey(), entry.getValue().getPaint());
}
}
@Override
public boolean onTouchEvent(MotionEvent me){
float eventX = me.getX();
float eventY = me.getY();
switch (me.getAction()) {
case MotionEvent.ACTION_DOWN:
mCurrentPath.moveTo(eventX, eventY);
return true;
case MotionEvent.ACTION_MOVE:
mCurrentPath.lineTo(eventX, eventY);
break;
case MotionEvent.ACTION_UP:
break;
}
invalidate();
return true;
}
/**
* Setter for new pen
*
* @param pen {@link DrawingPens} to be used for next drawing
*/
public void setPen(final DrawingPens pen) {
// put latest item to the queue
mCurrentPath = new Path();
mPaths.add(new AbstractMap.SimpleImmutableEntry<Path, DrawingPens>(mCurrentPath, pen));
}
public void clear(View v){
}
}
main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity" >
<com.example.singletouch.SingleTouchView
android:id="@+id/myview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@+id/pen" />
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@drawable/menubar"
android:padding="2dp"
android:weightSum="4" >
<ImageView
android:id="@+id/pen"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_weight="1"
android:gravity="center"
android:src="@drawable/pen" />
<ImageView
android:id="@+id/eraser"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_weight="1"
android:src="@drawable/eraser" />
<ImageView
android:id="@+id/color"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_weight="1"
android:src="@drawable/color" />
<ImageView
android:id="@+id/remove"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_weight="1"
android:src="@drawable/remove" />
</LinearLayout>
<LinearLayout
android:id="@+id/linear"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/linearLayout1"
android:layout_alignParentLeft="true"
android:background="#eeeeee"
android:orientation="horizontal"
android:visibility="gone"
android:weightSum="4" >
<LinearLayout
android:id="@+id/pen1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center" >
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/pen1" />
</LinearLayout>
<LinearLayout
android:id="@+id/pen2"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="@drawable/pen2" />
</LinearLayout>
<LinearLayout
android:id="@+id/pen3"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/pen3" />
</LinearLayout>
<LinearLayout
android:id="@+id/pen4"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/pen4" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
解决方案
Jus use ...
mCanvas.drawColor(Color.BLACK);
... for clearing the canvas or any other color you want to have in background. E.g.
remove.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// clear canvas contents
mCanvas.drawColor(Color.BLACK);
// create new path list; old one will be garbage collected
ConcurrentLinkedQueue<Map.Entry<Path, DrawingPens>> mPaths =
new ConcurrentLinkedQueue<Map.Entry<Path, DrawingPens>>();
pens.setVisibility(View.VISIBLE);
}
p.s.: for removing a view dynamically from its container use removeView(....)
, e.g.
((ViewGroup)viewToRemove.getParent()).removeView(viewToRemove);
Hope this helps ... Cheers!
这篇关于如何清除机器人有何看法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文