Java 一次只显示一种颜色
我正在开发绘画应用程序问题是,当我选择颜色和绘画,然后选择不同的颜色时,整个绘画颜色会变为新的颜色。有人能告诉我为什么会发生这种情况以及如何解决这个问题吗?如何添加橡皮擦到这个? imageview DrawView此处:Java 一次只显示一种颜色,java,android,colors,custom-view,Java,Android,Colors,Custom View,我正在开发绘画应用程序问题是,当我选择颜色和绘画,然后选择不同的颜色时,整个绘画颜色会变为新的颜色。有人能告诉我为什么会发生这种情况以及如何解决这个问题吗?如何添加橡皮擦到这个? imageview DrawView此处: public class DrawView extends ImageView { private ArrayList<Point> mTouches; int paintColor; public int setcolor(int a){
public class DrawView extends ImageView {
private ArrayList<Point> mTouches;
int paintColor;
public int setcolor(int a){
paintColor=a;
return paintColor;}
// Java constructor
public DrawView(Context context) {
super(context);
init();}
// XML constructor
public DrawView(Context context, AttributeSet attrs) {
super(context, attrs);
init();}
private void init() {
mTouches = new ArrayList<Point>();}
@Override
public boolean onTouchEvent(MotionEvent event) {
// Capture a reference to each touch for drawing
float touchX = event.getX();
float touchY = event.getY();
mTouches.add(new Point(Math.round(touchX), Math.round(touchY)));
return super.onTouchEvent(event);}
@Override
protected void onDraw(Canvas c) {
// Let the image be drawn first
super.onDraw(c);
// Draw your custom points here
Paint paint = new Paint();
paint.setColor(paintColor);
paint.setAntiAlias(true);
paint.setStrokeWidth(5);
paint.setStyle(Paint.Style.FILL);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeCap(Paint.Cap.ROUND);
for (Point p : mTouches) {
c.drawCircle( p.x, p.y,15,paint);}} }
每次调用
onDraw
,都会重新绘制整个画布。由于在自定义类中仅存储一个paintColor,因此仅使用最后选定的一个
您需要将颜色与关联点一起存储。然后,在循环抛出mTouches
数组时,更改当前点颜色的颜色
顺便说一句,Paint
对象可以更新,因此您不需要每次都创建它,因为在onDraw
中创建新对象是一种不好的做法
编辑
公共类DrawView扩展了android.support.v7.widget.AppCompatImageView{
私人ArrayList mTouches;
//当前使用的颜色
私人国际色彩;
私人油漆;
公共空间设置颜色(内部颜色){
mCurrColour=颜色;
}
公共绘图视图(上下文){
超级(上下文);
init();
}
//XML构造函数
公共绘图视图(上下文、属性集属性){
超级(上下文,attrs);
init();
}
私有void init(){
mTouches=newarraylist();
mPaint=新油漆();
mPaint.setColor(mCurrColour);
mPaint.setAntiAlias(true);
mPaint.设定行程宽度(5);
mPaint.setStyle(油漆、样式、填充);
mPaint.setStrokeJoin(油漆.连接.圆形);
mPaint.setStrokeCap(油漆盖圆形);
}
@凌驾
公共布尔onTouchEvent(运动事件){
//为绘图捕获每个触摸的参考
float touchX=event.getX();
float touchY=event.getY();
添加(新的着色点(Math.round(touchX)、Math.round(touchY)、McUrrColor));
返回super.onTouchEvent(事件);
}
@凌驾
受保护的void onDraw(画布c){
//让我们先画图像
super.onDraw(c);
//在此处绘制自定义点
用于(着色点p:mTouches){
mPaint.setColor(p.color);
c、 画圈(p.x,p.y,15,mPaint);
}
}
/**
*类来存储点的坐标和颜色。
*/
私有类着色点{
int x;
int-y;
内部色彩;
公共着色点(整数x、整数y、整数颜色){
这个.x=x;
这个。y=y;
这个颜色=颜色;
}
}
}
你的意思是我没有用绘图方法绘制和设置颜色@eSelfary您可以在创建视图时初始化绘制(在init
方法中)。然后,您可以在onDraw
中执行mPaint.setColor()
来设置要使用的颜色。基本上,实例化新对象是很耗时的,onDraw
需要尽可能短的执行时间。仍然执行相同的操作,旧点颜色将更改为新颜色!!当然可以。这部分只是为了改进代码。但正如我上面所说,你必须储存所有的颜色,而不仅仅是最后一种。如果我理解正确,您的用户会选择一种颜色、绘制、选择另一种颜色、绘制等等。因此,每次用户绘制一些东西时,你都需要将颜色存储在某个地方,以便以后能够重新绘制。我已经用一个示例编辑了上面的答案。可以改进,但应该有效。
im.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
DrawView mcustomImagview = (DrawView) v;
mcustomImagview.setcolor(color);
mcustomImagview.invalidate();
if (v.onTouchEvent(event)) {
// Do something with event.getX(), event.getY() }
return true;}})
public class DrawView extends android.support.v7.widget.AppCompatImageView {
private ArrayList<ColouredPoint> mTouches;
// Current used colour
private int mCurrColour;
private Paint mPaint;
public void setColor(int colour) {
mCurrColour = colour;
}
public DrawView(Context context) {
super(context);
init();
}
// XML constructor
public DrawView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
mTouches = new ArrayList<>();
mPaint = new Paint();
mPaint.setColor(mCurrColour);
mPaint.setAntiAlias(true);
mPaint.setStrokeWidth(5);
mPaint.setStyle(Paint.Style.FILL);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// Capture a reference to each touch for drawing
float touchX = event.getX();
float touchY = event.getY();
mTouches.add(new ColouredPoint(Math.round(touchX), Math.round(touchY), mCurrColour));
return super.onTouchEvent(event);
}
@Override
protected void onDraw(Canvas c) {
// Let the image be drawn first
super.onDraw(c);
// Draw your custom points here
for (ColouredPoint p : mTouches) {
mPaint.setColor(p.colour);
c.drawCircle(p.x, p.y, 15, mPaint);
}
}
/**
* Class to store the coordinate and the colour of the point.
*/
private class ColouredPoint {
int x;
int y;
int colour;
public ColouredPoint(int x, int y, int colour) {
this.x = x;
this.y = y;
this.colour = colour;
}
}
}