gpt4 book ai didi

android - 如何在android中清除 View ?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:27:00 25 4
gpt4 key购买 nike

我已经在 android 中创建了一个用于绘图的简单演示应用程序,现在有人能告诉我如何只需单击一下即可删除 View 和清除屏幕吗?我已尝试如下:请帮助我做到这一点....提前谢谢..!

ma​​in.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){

}

}

ma​​in.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>

最佳答案

只需使用...

mCanvas.drawColor(Color.BLACK);

... 用于清除 Canvas 或您想要在背景中使用的任何其他颜色。例如

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

附注:要从容器中动态移除 View ,请使用removeView(....),例如

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

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

关于android - 如何在android中清除 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17849677/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com