3

I have a bitmap and I draw it like this :

bit = BitmapFactory.decodeResource(getResources(),
                   R.drawable.anim_ctrl_panel);
Paint paint = new Paint();
canvas.drawBitmap(bit, 80, 440, paint);

The problem is that the image is set as fullscreen .On onTouchEvent method I implement the event for the bitmap and anywhere I select screen the event is implement. I want the image to have a certain position and only for that position to be implement the event. How can I do this?

EDIT: the problem was at onTouchEvent method. This is the solution :

public boolean onTouchEvent(MotionEvent event) {if(80 <= event.getX() && event.getX()<= (80+bit.getWidth()/2) && (440 <= event.getY() && (event.getY() <= 440+bit.getHeight()))){   
            //...
        }else if((80+bit.getWidth()/2) <= event.getX() && event.getX()<= (80+bit.getWidth()) && (440 <= event.getY() && (event.getY() <= 440+bit.getHeight()))){
            //...
            }

this help me : How can I check the Image is Touched using OnTouch()

Community
  • 1
  • 1
Gabrielle
  • 4,933
  • 13
  • 62
  • 122

2 Answers2

1

Try this:

canvas.save();
canvas.translate(posX, posY);
canvas.drawBitmap(bit, 0f, 0f, paint);
canvas.restore();
vicentazo
  • 1,739
  • 15
  • 19
  • Really it's similar, but it should works. I also try to limit the area where you want to draw. http://developer.android.com/reference/android/graphics/Canvas.html#drawBitmap(android.graphics.Bitmap, android.graphics.Rect, android.graphics.RectF, android.graphics.Paint) – vicentazo Oct 24 '11 at 08:21
1

For Image set in particular area, I think Rectangle is useful for this,

Something like this, (In my case Its work for me)

Canvas canvas = new Canvas(bitmap);
final Paint paint = new Paint();         
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());         
paint.setAntiAlias(true);         
canvas.drawARGB(0, 0, 0, 0);         
canvas.drawBitmap(bitmap, rect, rect, paint);

Try it.

For more info look at Android - Drawing (Core)

Thanks.

user370305
  • 108,599
  • 23
  • 164
  • 151
  • I get the same thing...I thing my problem is the onTouchEvent method...there I put only this : if (event.getX() > bit.getWidth() / 2) and I should put a condition for y too, right? – Gabrielle Oct 24 '11 at 08:17
  • Make a specific ImageView object and using rectangle and canvas fix the height and width (whatever you needed) and then implement onTouch for that perticluar view. (As per I unerstand your question). Thanks. – user370305 Oct 24 '11 at 08:21
  • or make calculation, getX any getY for touch and make if condition to check whether it is in your image's rectangle's co-ordinates then do wahtever you want. – user370305 Oct 24 '11 at 08:46
  • If you found this is the solution then please upvote it and accept as a correct answer. Its help you and also for other user. Thanks. :-) – user370305 Oct 24 '11 at 08:58