i come again android brain idea!
i put circle hole hole in imageview bitmap scaled centercrop. know need put circle hole( in dp left , bottom) , hole radius. don't know how build it!
i know can use porterduff hole suggest do?
- custom bitmap
- custom drawable/view
- custom code
thanks
following answers there customimage hole:
public class myimageview extends imageview { private attributeset attrs; private float y; private float x; private float r; private paint paint; private rect msrcrect; private rect mdestrect; private bitmap mbitmap; private int alreadycalled = 0; public myimageview(context context, attributeset attrs) { super(context, attrs); this.attrs = attrs; initview(); } public myimageview(context context, attributeset attrs, int defstyleattr) { super(context, attrs, defstyleattr); this.attrs = attrs; initview(); } public myimageview(context context, float x, float y, float radius) { super(context); this.x = x; this.y = y; this.r = radius; log.d("parameters", string.format("left:%s , right:%s, radius:%s", string.valueof(x), string.valueof(y), string.valueof(r))); initview(); } private void initview() { paint = new paint(paint.anti_alias_flag); paint.setstyle(paint.style.fill_and_stroke); paint.setstrokewidth(10); paint.setcolor(0xff000000); } @override protected void ondraw(canvas canvas) { alreadycalled++; log.d("alreadycalled", "called " + alreadycalled); drawable mdrawable = getdrawable(); if (mdrawable == null) { return; // couldn't resolve uri } int dwidth = mdrawable.getintrinsicwidth(); int dheight = mdrawable.getintrinsicheight(); float scale = 1.0f; scale = math.max(getwidth() * 1.0f / dwidth, getheight() * 1.0f / dheight); int nwidth = (int) (dwidth * scale); int nheight = (int) (dheight * scale); int offsetleft = (nwidth - getwidth()) / 2; int offsettop = (nheight - getheight()) / 2; mbitmap = ((bitmapdrawable) mdrawable).getbitmap(); //custom msrcrect mdestrect achieve centercrop msrcrect = new rect(0, 0, dwidth, dwidth); mdestrect = new rect(-offsetleft, -offsettop, getwidth() + offsetleft, getheight() + offsettop); log.d("src", msrcrect.tostring()); log.d("dest", mdestrect.tostring()); int sc = canvas.savelayer(0, 0, getwidth(), getheight(), null, canvas.matrix_save_flag | canvas.clip_save_flag | canvas.has_alpha_layer_save_flag | canvas.full_color_layer_save_flag | canvas.clip_to_layer_save_flag); paint.setcolor(0xffffffff); canvas.drawbitmap(mbitmap, msrcrect, mdestrect, paint); paint.setcolor(0xffff0000); paint.setxfermode(new porterduffxfermode(porterduff.mode.dst_out)); log.d("position", string.format("%s , %s", string.valueof(x), string.valueof(y))); canvas.drawcircle(x, y, r, paint); paint.setxfermode(null); canvas.restoretocount(sc); }
i called programmatically theses lines :
bitmapdrawable bd=(bitmapdrawable) getresources().getdrawable(r.drawable.triangle_bas_accueil2); myimageview customimview = new myimageview(getapplicationcontext(), mx, my, mradius); customimview.setimagedrawable(bd); linearlayout.layoutparams params = new linearlayout.layoutparams(viewgroup.layoutparams.match_parent, viewgroup.layoutparams.match_parent); params.gravity = gravity.bottom; customimview.setlayoutparams(params); down_relative.addview(customimview);
but ondraw()
method called twice (maybe needs) makes me 2 holes, 1 can change parameters other still @ same place ! container of myimageview
relativelayout
.
if has idea? @tiny-sunlight ?
this 1 centercrop , can't deal scaletype.and code may have problems because i'm not @ canvas.
public class myimageview extends imageview { private final attributeset attrs; private paint paint; private rect msrcrect; private rect mdestrect; private bitmap mbitmap; public myimageview(context context, attributeset attrs) { super(context, attrs); this.attrs = attrs; initview(); } public myimageview(context context, attributeset attrs, int defstyleattr) { super(context, attrs, defstyleattr); this.attrs = attrs; initview(); } private void initview() { paint = new paint(paint.anti_alias_flag); paint.setstyle(paint.style.fill_and_stroke); paint.setstrokewidth(10); paint.setcolor(0xff000000); } @override protected void ondraw(canvas canvas) { // super.ondraw(canvas); //create drawable.maybe can cache it. drawable mdrawable = (bitmapdrawable) getresources().getdrawable(r.drawable.triangle_bas_accueil2); if (mdrawable == null) { return; // couldn't resolve uri } int dwidth = mdrawable.getintrinsicwidth(); int dheight = mdrawable.getintrinsicheight(); float scale = 1.0f; scale = math.max(getwidth() * 1.0f / dwidth, getheight() * 1.0f / dheight); int nwidth = (int) (dwidth*scale); int nheight = (int) (dheight*scale); int offsetleft = (nwidth - getwidth())/2; int offsettop = (nheight - getheight())/2; //cache mbitmap mbitmap = mbitmap == null ? ((bitmapdrawable) mdrawable).getbitmap(): mbitmap; //custom msrcrect mdestrect achieve centercrop msrcrect = new rect(0, 0, dwidth, dwidth); mdestrect = new rect(-offsetleft, -offsettop,getwidth()+offsetleft, getheight()+offsettop); int x = 250;int r = 100; int sc = canvas.savelayer(0, 0, getwidth(), getheight(), null, canvas.matrix_save_flag | canvas.clip_save_flag | canvas.has_alpha_layer_save_flag | canvas.full_color_layer_save_flag | canvas.clip_to_layer_save_flag); paint.setcolor(0xffffffff); canvas.drawbitmap(mbitmap,msrcrect,mdestrect,paint); paint.setcolor(0xff000000); paint.setxfermode(new porterduffxfermode(porterduff.mode.dst_out)); canvas.drawcircle(x,x,r,paint); paint.setxfermode(null); canvas.restoretocount(sc); } }
Comments
Post a Comment