Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/196.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在android中清除视图?_Android_Android Canvas_Android View - Fatal编程技术网

如何在android中清除视图?

如何在android中清除视图?,android,android-canvas,android-view,Android,Android Canvas,Android View,我已经在android中创建了一个简单的绘图演示应用程序,现在有人能告诉我如何在一次点击中删除视图和清除屏幕。我已经尝试了以下方法:请帮助我做到这一点…thanx提前 main.java package com.example.singletouch; import com.example.singletouch.R.attr; import android.os.Bundle; import android.app.Activity; import android.graphics.Can

我已经在android中创建了一个简单的绘图演示应用程序,现在有人能告诉我如何在一次点击中删除视图和清除屏幕。我已经尝试了以下方法:请帮助我做到这一点…thanx提前

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) {


            }
        });


    }

}
    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){

           }

        }
SingleTouchView.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) {


            }
        });


    }

}
    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){

           }

        }
package 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;
公共涂料;
语境;
公共油漆;
公共道路环路;
公共枚举绘图笔{
围栏1(6),
围栏2(4),
围栏3(2),
围栏4(1);
最终公共油漆;
/**
*建造师
*
*@param笔划宽度
*@param笔划颜色
*/
专用绘图笔(最终整数宽度){
mPaint=新油漆();
mPaint.setAntiAlias(true);
mPaint.设定行程宽度(宽度);
//mPaint.setColor(颜色);
mPaint.setStyle(油漆、样式、笔划);
mPaint.setStrokeJoin(油漆.连接.圆形);
}
/**
*@返回相应的油漆
*/
Paint getPaint(){
返回mPaint;
}
}
公共SingleTouchView(最终上下文){
超级(上下文);
init(上下文);
}
公共SingleTouchView(最终上下文、最终属性集属性){
超级(上下文,attrs);
init(上下文);
}
公共SingleTouchView(最终上下文上下文、最终属性集属性、最终int-defStyle){
超级(上下文、属性、定义样式);
init(上下文);
}
/**存储绘制路径关系的步骤*/
//TODO:根据具体的限制,可以找到更优化的方法
私有ConcurrentLinkedQueue MPath=新ConcurrentLinkedQueue();
/**缓存的当前路径,注意:此字段位于MPath的尾部,仅用于缓存,不用于绘图*/
专用路径mCurrentPath;
/**
*应该从每个构造函数调用Inits内部视图数据
*
*@param context{@link context}
*/
私有void init(最终上下文){
/*TODO:如果无法在静态上下文中(在枚举中)确定某些绘制值,
然后,应在此处创建油漆,并通过EnumMap使用*/
//首笔
setPen(绘图笔。PEN_1);
}
@凌驾
公共空白onDraw(画布){
//只是画背景
super.onDraw(帆布);
//绘制所有路径
用于(地图条目:百万帕){
drawPath(entry.getKey(),entry.getValue().getPaint());
}
}
@凌驾
公共事件(MotionEvent me){
float eventX=me.getX();
float eventY=me.getY();
开关(me.getAction()){
case MotionEvent.ACTION\u DOWN:
mCurrentPath.moveTo(eventX,eventY);
返回true;
case MotionEvent.ACTION\u移动:
mCurrentPath.lineTo(eventX,eventY);
打破
case MotionEvent.ACTION\u UP:
打破
}
使无效();
返回true;
}
/**
*新笔的设定器
*
*@param pen{@link DrawingPens}用于下一次绘图
*/
公共无效设置笔(最终绘图笔){
//将最新项目放入队列
mCurrentPath=新路径();
add(newAbstractMap.SimpleImmutableEntry(mCurrentPath,pen));
}
公共空间清理(视图五){
}
}
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>

将要清除的屏幕部分放在xml格式的单个布局中,例如

<RelativeLayout
android:id="@+id/layout"
android:layout_width="wrap_content"
android:layout_height= "wrap_content">

    The Views that have to be gone on click

</RelativeLayout>
编辑:

如果要清除
图像视图
,请使用:

((ImageView) findViewById(R.id.imageView)).setImageDrawable(null);
绝对用法

mCanvas.drawColor(Color.BLACK);
。。。用于清除画布或任何其他背景颜色。例如

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);
    }

希望这有助于。。。干杯

您可以绘制透明色,而不是绘制纯色,这有助于您在将其他视图用作画布背景时不覆盖它们

remove.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                layout.removeView(mDrawView);
                mDrawView = new SingleTouchView(MainActivity.this);
                layout.addView(mDrawView);
            }
        });
mCanvas.drawColor(Color.TRANSPARENT,PorterDuff.Mode.CLEAR)


我从这个答案中得到了这个答案:

我不知道你的意思,但也许这会有所帮助:@yser2617212-m发布我的xml文件,以便你了解。。。