0
我遇到問題。目的是在畫布上畫筆畫並得到它,但是當我製作doubleTap時,我繪製的筆畫採用了我選擇的顏色,並且我並不希望第一筆畫的顏色與第二筆畫的顏色相同另一種顏色。更改畫布顏色Android
這裏我的代碼:
package com.example.guiao_2_1;
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.v4.view.GestureDetectorCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;
public class NewView extends View implements GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener{
private Paint paint = new Paint();
private Path path = new Path();
private DrawingPath newPath;
private GestureDetectorCompat mDetector;
private static final String DEBUG_TAG = "Gestures";
private DrawingPath oldPath;
private ArrayList<DrawingPath> olds = new ArrayList<DrawingPath>();
private ArrayList<DrawingPath> news = new ArrayList<DrawingPath>();
public NewView(Context context, AttributeSet attrs) {
super(context, attrs);
paint.setAntiAlias(true);
paint.setStrokeWidth(6f);
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
mDetector = new GestureDetectorCompat(context, this);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawPath(path, paint);
for (DrawingPath p : olds){
paint.setColor(p.color);
paint.setStrokeWidth(p.width);
canvas.drawPath(p, paint);
}
for (DrawingPath j : news){
paint.setColor(j.color);
paint.setStrokeWidth(j.width);
canvas.drawPath(j, paint);
}
//canvas.drawPath(path, paint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
this.mDetector.onTouchEvent(event);
float eventX = event.getX();
float eventY = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
path.moveTo(eventX, eventY);
return true;
case MotionEvent.ACTION_MOVE:
path.lineTo(eventX, eventY);
break;
case MotionEvent.ACTION_UP:
// nothing to do
break;
default:
return false;
}
// Schedules a repaint.
invalidate();
return true;
}
@Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onDoubleTap(MotionEvent e) {
// TODO Auto-generated method stub
float width = paint.getStrokeWidth();
int color = paint.getColor();
oldPath = new DrawingPath(width, color);
olds.add(oldPath);
newPath = new DrawingPath(8, Color.GREEN);
news.add(newPath);
//int numColor = paint.getColor();
//String color = Integer.toHexString(numColor);
//Toast.makeText(getContext(), color, Toast.LENGTH_SHORT).show();
Log.d(DEBUG_TAG, "onDoubleTap: " + e.toString());
return true;
}
@Override
public boolean onDoubleTapEvent(MotionEvent event) {
// TODO Auto-generated method stub
Log.d(DEBUG_TAG, "onDoubleTap: " + event.toString());
return false;
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
private class DrawingPath extends Path {
public float width;
public int color;
public DrawingPath(float w, int c){
width = w;
color = c;
}
}
}
請告訴我,我在做什麼榮,請。
的目標是用不同的顏色繪製。例如,在開始雙擊之前,你畫一條線,顏色是黑色。當您雙擊時,您將顏色更改爲綠色,但不希望先前變爲綠色,但保留以前的顏色。而在我的代碼中,當我雙擊水龍頭變成綠色時,我繪製了。 –
我現在添加了代碼。我無法測試它,但它應該可以工作。如果沒有,只要提到會發生什麼。 – chuck258