Android: Drawing a canvas to an ImageView -
i'm new android programming , i'm trying figure out this;
in layout have textview, imageview, , button, on vertically oriented linearlayout.
i want able dynamically draw circles in imageview, without disturbing rest of layout(textview/button). i'm trying create canvas, , use drawcircle function within canvas set location of circle. , draw canvas imageview in way. cannot work, there trick this? or method fundamentally wrong? how go drawing circles imageview without recreating entire layout?
thanks!
i had same challenge , came conclusion overwriting ondraw @ least in general case not work. my blog explains reasons. worked me following:
- create new image bitmap , attach brand new canvas it.
- draw image bitmap canvas.
- draw else want canvas.
- attach canvas imageview.
here code snippet this:
import android.graphics.bitmap; import android.graphics.canvas; import android.graphics.paint; import android.graphics.rectf; import android.graphics.drawable.bitmapdrawable; imageview myimageview = ... bitmap mybitmap = ... paint myrectpaint = ... int x1 = ... int y1 = ... int x2 = ... int y2 = ... //create new image bitmap , attach brand new canvas bitmap tempbitmap = bitmap.createbitmap(mybitmap.getwidth(), mybitmap.getheight(), bitmap.config.rgb_565); canvas tempcanvas = new canvas(tempbitmap); //draw image bitmap cavas tempcanvas.drawbitmap(mybitmap, 0, 0, null); //draw else want canvas, in example rectangle rounded edges tempcanvas.drawroundrect(new rectf(x1,y1,x2,y2), 2, 2, mypaint); //attach canvas imageview myimageview.setimagedrawable(new bitmapdrawable(getresources(), tempbitmap));
Comments
Post a Comment