android - Detecting touch inside a irregular shape in ImageView -


i have imageview has transparent drawable circle @ center (http://wallpaperswide.com/circle_outline-wallpapers.html). circle red, , surrounding not coloured transparent, .png image. implement canvas.ondraw(), , when while tracing user goes outside circle, drawing should restart.

the doubt is: 1. how detect boundaries of image without hardcoding. 2. how detect user has clicked outside this, not regular rectangle.

i doing students trace alphabets, want answer generic on basis of image in shape of letter. can done way? if not, better way can suggest?

i'd go easy route: draw image , check colour of point user touched. if alpha channel transparent, user moved out.

the following code untested (and rough). have no idea if getdrawingcache trick works.

public class foobar extends imageview {  bitmap b = null;  public foobar(context context) {     super(context); }  @override protected void ondraw(canvas canvas) {     super.ondraw(canvas);     b = getdrawingcache(true); }  @override public boolean ontouchevent(motionevent event) {     if (event.getaction() == motionevent.action_move)         check((int) event.getx(), (int) event.gety());      return super.ontouchevent(event); }  private void check(int x, int y) {     if (b != null && color.alpha(b.getpixel(x, y)) >= 0)         onmovedoutofshape(); }  private void onmovedoutofshape() {     toast.maketext(getcontext(), "you're out", toast.length_short).show(); } } 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -