長方形の線で描画し、その後破線で描画したい場合、私の長方形の線は破線に変換されます。私はこのコードを使用します:
MyLine.java です。
public class MyLine { // line
public float x,y;
public float xStart,yStart,xEnd,yEnd;
public Paint paint = new Paint();
Path path = new Path();
public MyLine()
{
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.STROKE);
paint.setPathEffect(new DashPathEffect(new float[]{20,30}, 0));
paint.setAntiAlias(true);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeWidth(5f);
path = new Path();
}
public void mouseDown(Path path,float xDown,float yDown){
path.moveTo(xDown, yDown);
path.lineTo(xDown, yDown);
xStart = xDown;
yStart = yDown;
}
public void mouseUp(Path path,float xUp,float yUp){
path.lineTo(xUp, yUp);
xEnd = xUp;
yEnd = yUp;
}
public void draw(Canvas c){
paint.setColor(Color.GREEN);
paint.setStyle(Paint.Style.STROKE);
paint.setPathEffect(new DashPathEffect(new float[]{10,20}, 0));
paint.setAntiAlias(true);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeWidth(5f);
path = new Path();
c.drawLine(xStart,yStart,xEnd,yEnd,paint);
}
public void changeColor(int color){
paint = new Paint(paint);
paint.setColor(color);
//myPaint.setColor(color);
}
}
問題があると思われる CanvasView.java のコードを次に示します。
public void setDashLine(){
dashedLine = true;
paint = new Paint();
paint.setPathEffect(dashEffect);
paint.setStyle(Paint.Style.STROKE);
paint.setAntiAlias(true);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeWidth(STROKE_WIDTH);
myCanvas = new Canvas();
path = new Path();
}
public void setNormalLine(){
paint.setColor(Color.RED);
paint = new Paint();
paint.setPathEffect(null);
paint.setStyle(Paint.Style.STROKE);
paint.setPathEffect(null);
paint.setAntiAlias(true);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeWidth(STROKE_WIDTH);
myCanvas = new Canvas();
path = new Path();
}
protected void onDraw(Canvas canvas) {
paint.setPathEffect(null);
if(bitmap!=null){
canvas.drawBitmap(bitmap, 0, 0, paint);
for(MyCircle circle:circleList){// draw circles
myCanvas.drawCircle(getCircleMidPointX(circle.firstX, circle.lastX),getCircleMidPointY(circle.firstY, circle.lastY),circle.radius,myPaint);
}
}
for(MyLine line:lineList){ //draw lines
if(dashedLine)
line.paint.setPathEffect(dashEffect);
else
line.paint.setPathEffect(null);
canvas.drawLine(line.xStart, line.yStart, line.xEnd, line.yEnd, line.paint);
}
final OnTouchListener drawLineListener = new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
FirstActivity.ll.setVisibility(LinearLayout.GONE);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
undonePaths.clear();
path.reset();
myLine = new MyLine();
myLine.xStart = event.getX();
myLine.yStart = event.getY();
return true;
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_UP:
myLine.xEnd = event.getX();
myLine.yEnd = event.getY();
invalidate();
lineList.add(myLine);
break;
default:
Log.d("mock it up", "Unknown touch event " + event.toString());
return false;
}
return true;
}
};
final OnTouchListener drawDashedLineListener = new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
FirstActivity.ll.setVisibility(LinearLayout.GONE);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
myLine = new MyLine();
return true;
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_UP:
invalidate();
lineList.add(myLine);
break;
default:
Log.d("mock it up", "Unknown touch event " + event.toString());
return false;
}
return true;
}
};
どこが間違っているかわかる場合は、どうすれば役立つようになるか教えてください。