我有以下CustomView
我在我的应用中使用绘画:
package com.test.testing;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Paint.Style;
import android.view.MotionEvent;
import android.widget.TextView;
public class CustomView extends TextView {
Paint paint;
Path path;
float x = 0;
float y = 0;
private int cWhite = Color.WHITE;
public CustomView(Context context) {
super(context);
paint = new Paint();
path= new Path();
paint.setAlpha(255);
paint.setColor(cWhite);
paint.setStyle(Style.STROKE);
paint.setStrokeWidth(20);
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawPath(path,paint);
canvas.drawCircle(x, y, 10, paint);
}
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
path.moveTo(event.getX(), event.getY());
path.lineTo(event.getX(), event.getY());
break;
case MotionEvent.ACTION_MOVE:
x = event.getX();
y = event.getY();
path.lineTo(x, y);
invalidate();
break;
case MotionEvent.ACTION_UP:
path.lineTo(event.getX(), event.getY());
break;
case MotionEvent.ACTION_CANCEL:
break;
default:
break;
}
return true;
}
}
我设置了一个FrameLayout
,用于保存画布以便在我的XML中绘图:
<FrameLayout
android:id="@+id/viewd"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:orientation="vertical" >
</FrameLayout>
我在Activity
内部调用以获取视图以使用户能够进行绘制:
layout = (FrameLayout)findViewById(R.id.viewd);
//layout.removeAllViews();
view = new CustomView(Activity.this);
view.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
layout.addView(view);
我有不同的颜色选项,用户可以选择更改Dialog
中Activity
内的描边颜色:
public void colorHandle() {
// custom dialog
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.colorlayout);
dialog.setTitle("Choose a Drawing Color");
Button btnWH = (Button) dialog.findViewById(R.id.btnWhite);
Button btnBL = (Button) dialog.findViewById(R.id.btnBlack);
Button btnBLU = (Button) dialog.findViewById(R.id.btnBlue);
Button btnCY = (Button) dialog.findViewById(R.id.btnCyan);
Button btnDG = (Button) dialog.findViewById(R.id.btnDkGray);
Button btnGR = (Button) dialog.findViewById(R.id.btnGray);
Button btnGRE = (Button) dialog.findViewById(R.id.btnGreen);
Button btnLG = (Button) dialog.findViewById(R.id.btnLtGray);
Button btnMG = (Button) dialog.findViewById(R.id.btnMagenta);
Button btnRD = (Button) dialog.findViewById(R.id.btnRed);
Button btnYE = (Button) dialog.findViewById(R.id.btnYellow);
if (btnWH != null) {
btnWH.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
view.paint.setColor(Color.WHITE);
dialog.dismiss();
}
});
}
if (btnBL != null) {
btnBL.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
view.paint.setColor(Color.BLACK);
dialog.dismiss();
}
});
}
if (btnBLU != null) {
btnBLU.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
view.paint.setColor(Color.BLUE);
dialog.dismiss();
}
});
}
if (btnCY != null) {
btnCY.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
view.paint.setColor(Color.CYAN);
dialog.dismiss();
}
});
}
if (btnDG != null) {
btnDG.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
view.paint.setColor(Color.DKGRAY);
dialog.dismiss();
}
});
}
if (btnGR != null) {
btnGR.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
view.paint.setColor(Color.GRAY);
dialog.dismiss();
}
});
}
if (btnGRE != null) {
btnGRE.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
view.paint.setColor(Color.GREEN);
dialog.dismiss();
}
});
}
if (btnLG != null) {
btnLG.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
view.paint.setColor(Color.LTGRAY);
dialog.dismiss();
}
});
}
if (btnMG != null) {
btnMG.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
view.paint.setColor(Color.MAGENTA);
dialog.dismiss();
}
});
}
if (btnRD != null) {
btnRD.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
view.paint.setColor(Color.RED);
dialog.dismiss();
}
});
}
if (btnYE != null) {
btnYE.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
view.paint.setColor(Color.YELLOW);
dialog.dismiss();
}
});
}
dialog.show();
}
除非每次选择新颜色,否则一切正常,之前绘制的任何内容也会更改为新选择的颜色。无论接下来选择什么新颜色,我如何保持之前绘制的内容不变?
答案 0 :(得分:5)
您将为每个TouchEvent添加一个路径。然后使用Paint颜色的当前值绘制Path。这就是为什么你看到所有东西都用一种颜色绘制的原因。 您需要为每个颜色更改创建单独的路径和颜色,然后按顺序绘制它们,更改每个drawPath()调用的Paint颜色
我不这么认为。打破这条路并不是那么糟糕。
List<Pair<Path, Integer>> path_color_list = new ArrayList<Pair<Path,Integer>>()
然后每次更改颜色。获取当前路径并查看.paint.getColor并将其保存到列表中。
path_color_list.add( new Pair.create(path, view.paint.getColor());
path = new Path();
然后在你的draw()中遍历path_color_list,每次都设置新的颜色
for (Pair<Path,Integer> path_clr : path_color_list ){
paint.setColor(path_clr.second);
canvas.drawPath( path_clr.first, paint);
}
接着是你拥有的最后一个drawPath()
答案 1 :(得分:3)
只需做一件事总是创建view
类view1.paint.setColor(Color.LTGRAY);
的新实例,
然后layout.addView(view1);
意味着创建新的视图,它也将保存prevoius一个,
它为我工作。很简单!!