在不影响任何$ P $改变颜色pviously画 [英] Change color without affecting anything previously drawn
问题描述
我有如下的 CustomView
我在我的应用程序中使用绘画:
I have the following CustomView
I am using for painting in my app:
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绘图:
I setup a FrameLayout
which keeps the canvas for drawing in my XML:
<FrameLayout
android:id="@+id/viewd"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:orientation="vertical" >
</FrameLayout>
我我的活动内调用
来获取视图,使用户能够借鉴:
I call inside my Activity
to get the view to enable the user to draw on:
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);
我有不同的颜色选项,用户可以从中选择在我的活动改变绘画笔触的颜色在
:对话框
I have different color option that user can choose from to change the paint stroke color in a Dialog
within my 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();
}
一切工作正常,只是每次我选择了新的颜色,任何被提请previously也变更为新选择的颜色。如何保持任何被吸引previously不变,无论选择下一个什么样的新的颜色?
Everything is working fine except everytime I choose a new color, anything that was drawn previously also changes to the newly chosen color. How do I keep whatever was drawn previously unchanged no matter what new color is chosen next?
推荐答案
您要添加到每个的TouchEvent一条路径。然后路径是使用涂料的颜色的电流值绘制。这就是为什么你看到一切都在一个单一的颜色绘制。 您将需要创建一个单独的路径和颜色每种颜色的变化,然后得出它们的顺序,改变涂料的颜色为每个drawPath()调用
You're adding to a single path with each TouchEvent. Then the Path is drawn using the current value of the Paint's color. That's why you are seeing everything drawing in a single color. You would need to create a separate Path and color for each color change and then draw them in sequence, changing the Paint's color for each drawPath() call
我不这么认为。这并不是说不好打出来的路径。
I don't think so. It's not that bad to break out the paths.
List<Pair<Path, Integer>> path_color_list = new ArrayList<Pair<Path,Integer>>()
那么每次改变颜色的时间。就拿当前路径和view.paint.getColor并将其保存到你的列表中。
then each time you change the color. Take the current path and view.paint.getColor and save it to your list.
path_color_list.add( new Pair.create(path, view.paint.getColor());
path = new Path();
然后在您的平局()遍历path_color_list,每次设置新漆色
then in your draw() iterate through the path_color_list, setting the new paint color each time
for (Pair<Path,Integer> path_clr : path_color_list ){
paint.setColor(path_clr.second);
canvas.drawPath( path_clr.first, paint);
}
随后与上次drawPath(),您有
followed with the last drawPath() that you have
这篇关于在不影响任何$ P $改变颜色pviously画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!