简体   繁体   中英

Android OntouchListener, check image intersection while drawing bitmap

OK guys fine..., seems like I was wrong from the last question...

I'm working on a wordsearch game, and I've changed the "Point of View" of my app implementation from GridView to Bitmap-drawing using canvas in android.

this is the case: there's a drawable object inside a canvas and also another drawable object that will be draw as the user invoke the OntouchListener event...

I want to make a logic operation so that if the second draw object have the same axis or ordinat as the first drawable object, it will do something...

here's the code:

public class DrawView extends View implements OnTouchListener{
private static final String TAG = "DrawView";

List<Point> points = new ArrayList<Point>();
Paint paint = new Paint();
Bitmap kangoo = BitmapFactory.decodeResource(getResources(),R.drawable.icon);

public DrawView(Context context, AttributeSet attrs) {
    super(context,attrs);
    setFocusable(true);
    setFocusableInTouchMode(true);

    this.setOnTouchListener(this);
    paint.setColor(Color.WHITE);
    paint.setAntiAlias(true);
}

@Override
public void onDraw(Canvas canvas) {
    Bitmap krazy = BitmapFactory.decodeResource(getResources(),R.drawable.schema);
    canvas.drawBitmap(krazy, 130, 130, null);
    for (Point point : points) {
        canvas.drawBitmap(kangoo, point.x, point.y, null);
        //canvas.drawCircle(point.x, point.y, 5, paint);
        // Log.d(TAG, "Painting: "+point);
    }
}

@Override
public boolean onTouch(View view, MotionEvent event) {
     // if(event.getAction() != MotionEvent.ACTION_DOWN)
    // return super.onTouchEvent(event);
    Point point = new Point();
    point.x = event.getX();
    point.y = event.getY();
    points.add(point);
    invalidate();
    Log.d(TAG, "point: " + point);
    return true;
}
    }

    class Point {
    float x, y;

    @Override
     public String toString() {
       return x + ", " + y;
     }
    }

see..., the static draw object is KRAZY and dynamic which will be draw while touch the screen is KANGOO

I want to know if those two object are in contact either by x or y....

Thank U

Actually this solution is used to assist my word-search game project...

The main thing that I just need to do is to measure the occupied words which represented by drawables blocks of string... and then to compare it with the square area that will be make by the square selection box which will draw itself inside the characters blocks as long as the user touch the screen...

and then invoke when the square area or the occupied area of square selection is OCCUPIED by the entire characters block.... i mean the square selection box is inside the characters blocks

in this case if a word make 4 blocks of characters start from point.x=0.0 , y=0.0 , and each block occupied 32pix H and W then the square selection box occupied position must be less than 4*32(W) and 1*32(H)....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM