こんにちは。私のアプリケーションでは、以下に示す 2 つの画像コードの間に線を引くことに成功しましたが、画像が一致したときに 2 つの列の間で 2 つの画像を一致させたいのですが、トースト メッセージを表示したいのです。両方の列に同じ画像がある場合は、その後線を引いて列 2 にトースト メッセージを表示します。
in MainActivity code :
RelativeLayout mRelativeLayout = (RelativeLayout)findViewById(R.id.linear);
mRelativeLayout.addView(new DrawView(this));
============================================== DrawViewでクラス:
public class DrawView extends View {
Paint paint = new Paint();
ArrayList<Line> lines = new ArrayList<Line>();
public DrawView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public DrawView(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);
}
@Override
protected void onDraw(Canvas canvas) {
for (Line l : lines) {
canvas.drawLine(l.startX, l.startY, l.stopX, l.stopY, paint);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
lines.add(new Line(event.getX(), event.getY()));
return true;
} else if ((event.getAction() == MotionEvent.ACTION_MOVE || event
.getAction() == MotionEvent.ACTION_UP) && lines.size() > 0) {
Line current = lines.get(lines.size() - 1);
current.stopX = event.getX();
current.stopY = event.getY();
invalidate();
return true;
} else {
return false;
}
}
}
=============================ラインクラス:
public class Line {
float startX, startY, stopX, stopY;
public Line(float startX, float startY, float stopX, float stopY) {
this.startX = startX;
this.startY = startY;
this.stopX = stopX;
this.stopY = stopY;
}
public Line(float startX, float startY) { // for convenience
this(startX, startY, startX, startY);
}
}