如何在它们之间画线时匹配两个图像

时间:2013-10-16 12:55:08

标签: android android-canvas

大家好..              在我的应用程序中,我已经成功绘制了两个图像之间的代码 但我希望匹配图像匹配时两个列之间的两个图像我想显示一个Toast消息。就像第1列有一个玫瑰图像,它与第2列的匹配,如果两列都有相同的图像显示吐司消息。 enter image description here

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);
}

}

1 个答案:

答案 0 :(得分:3)

Action_UP 中的方法 onTouchEvent 内查看第二行的坐标,看它们是否与所选图像匹配。如果它们匹配或介于图像范围之间,则在那里显示祝酒消息。