如何在android中清除视图?

时间:2013-07-25 05:25:23

标签: android android-canvas android-view

我已经在android中创建了一个简单的演示应用程序用于绘图,现在任何人都可以告诉我如何在一次单击中删除视图和清除屏幕。我试过如下:请帮我做...提前谢谢..!

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>

4 个答案:

答案 0 :(得分:4)

Jus使用......

mCanvas.drawColor(Color.BLACK);

...用于清除画布或您想要在背景中拥有的任何其他颜色。 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。:使用removeView(....)从容器中动态删除视图,例如

((ViewGroup)viewToRemove.getParent()).removeView(viewToRemove);

希望这有帮助......干杯!

答案 1 :(得分:4)

remove.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                layout.removeView(mDrawView);
                mDrawView = new SingleTouchView(MainActivity.this);
                layout.addView(mDrawView);
            }
        });

答案 2 :(得分:0)

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

mCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);

我是从以下答案中得到的:https://stackoverflow.com/a/10882301/8113211

答案 3 :(得分:-1)

将您想要清除的屏幕部分放在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>

然后在按钮onClick()的代码中输入

((RelativeLayout) findViewById(R.id.layout)).setVisiblity(View.GONE));

编辑:

如果您希望ImageView清除使用:

((ImageView) findViewById(R.id.imageView)).setImageDrawable(null);