Unable to paint on background setted images on my view android -
here code able draw on view when set background view, unable paint on background imgae please me out. in advance
public class fingerpaint extends graphicsactivity implements mycolorpickerdialog.oncolorchangedlistener { listview dialog_listview; string[] listcontent = { "stroke size 1", "stroke size 2", "stroke size 3", "stroke size 4", "stroke size 5","stroke size 10","stroke size 20","stroke size 30" }; string[] effectcontent = { "normal", "blur", "emboss", "srcatop" }; private static int font_size=5; private static int color_code=color.black; public int mpos; private bitmap mbitmap; myview mv; imageshowactivity imageshowactivity =new imageshowactivity(); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); mv= new myview(this); mv.setdrawingcacheenabled(true); //mv.setbackgroundresource(r.drawable.image1_layer1); setcontentview(mv); initialiazation(); memboss = new embossmaskfilter(new float[] { 1, 1, 1 }, 0.4f, 6, 3.5f); mblur = new blurmaskfilter(8, blurmaskfilter.blur.normal);// todo auto-generated method stub loadimagetoeddit(); } private void initialiazation() { mpaint = new paint(); mpaint.setantialias(true); mpaint.setdither(true); mpaint.setcolor(color_code); mpaint.setstyle(paint.style.stroke); mpaint.setstrokejoin(paint.join.round); mpaint.setstrokecap(paint.cap.round); mpaint.setstrokewidth(font_size); } private void loadimagetoeddit() { bundle bundle=getintent().getextras(); if(bundle!=null){ int pos=bundle.getint("image_path"); if(pos!=0){ // bitmap bitmap =bitmapfactory.decodefile(imageshowactivity.imagepathlist.get(path)); // mv=new myview(this,bitmap); //drawable d = new bitmapdrawable(getresources(),bitmap); //mv.setbackground(d); mpos=pos; } } } private paint mpaint; private maskfilter memboss; private maskfilter mblur; public void colorchanged(int color) { mpaint.setcolor(color); color_code=color; } private void fontchanged(float size){ mpaint.setstrokewidth(size); } private void showeffectdialog(){ final dialog dialog = new dialog(fingerpaint.this); dialog.setcontentview(r.layout.font_dialog); dialog.setcancelable(true); dialog.settitle("font"); windowmanager.layoutparams params = new windowmanager.layoutparams(); params.width=200; params.height=250; params.gravity = gravity.center_horizontal; dialog.getwindow().setattributes(params); arrayadapter<string> ad =new arrayadapter<string>(this, android.r.layout.simple_list_item_1,effectcontent); listview list = (listview) dialog.findviewbyid(r.id.dialoglist); list.setadapter(ad); dialog.show(); list.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> arg0, view arg1, int pos, long arg3) { switch (pos) { case 0: mpaint.setmaskfilter(null); initialiazation(); break; case 1: mpaint.setmaskfilter(null); //initialiazation(); mpaint.setstrokewidth(font_size); if (mpaint.getmaskfilter() != mblur) { mpaint.setmaskfilter(mblur); } else { mpaint.setmaskfilter(null); } break; case 2: mpaint.setmaskfilter(null); if (mpaint.getmaskfilter() != memboss) { mpaint.setmaskfilter(memboss); } else { mpaint.setmaskfilter(null); } break; case 3: mpaint.setmaskfilter(null); mpaint.setxfermode(new porterduffxfermode( porterduff.mode.src_atop)); mpaint.setalpha(0x80); break; default: break; } dialog.dismiss(); } }); } private void showdialog(){ final dialog dialog = new dialog(fingerpaint.this); dialog.setcontentview(r.layout.font_dialog); dialog.setcancelable(true); dialog.settitle("font"); windowmanager.layoutparams params = new windowmanager.layoutparams(); params.width=200; params.height=250; params.gravity = gravity.center_horizontal; dialog.getwindow().setattributes(params); arrayadapter<string> ad =new arrayadapter<string>(this, android.r.layout.simple_list_item_1,listcontent); listview list = (listview) dialog.findviewbyid(r.id.dialoglist); list.setadapter(ad); dialog.show(); list.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> arg0, view arg1, int pos, long arg3) { switch (pos) { case 0: font_size=1; fontchanged(1f); break; case 1: font_size=2; fontchanged(2f); break; case 2: font_size=3; fontchanged(3f); break; case 3: font_size=4; fontchanged(4f); break; case 4: font_size=5; fontchanged(5f); break; case 5: font_size=10; fontchanged(10f); break; case 6: font_size=20; fontchanged(20f); break; case 7: font_size=30; fontchanged(30f); break; default: break; } dialog.dismiss(); } }); } public class myview extends view { private static final float minp = 0.25f; private static final float maxp = 0.75f; private canvas mcanvas; private path mpath; private paint mbitmappaint; public myview(context c) { super(c); // displaymetrics dm = new displaymetrics(); // getwindowmanager().getdefaultdisplay().getmetrics(dm); // int w = dm.widthpixels; // int h=dm.heightpixels; // mbitmap = bitmap.createbitmap(w, h, bitmap.config.argb_8888); // mbitmap.erasecolor(c) // mcanvas = new canvas(mbitmap); // mcanvas.drawcolor(0xffaaaaaa); mpath = new path(); mbitmappaint = new paint(paint.dither_flag); // mpaint.setstrokewidth(font_size); } @override protected void onsizechanged(int w, int h, int oldw, int oldh) { super.onsizechanged(w, h, oldw, oldh); mbitmap = bitmap.createbitmap(w, h, bitmap.config.argb_8888); mcanvas = new canvas(mbitmap); } @override protected void ondraw(canvas canvas) { canvas.drawcolor(color.white); if(mpos!=0){ bitmap bitmap =bitmapfactory.decodefile(imageshowactivity.imagepathlist.get(mpos)); mbitmap=bitmap; canvas.drawbitmap(mbitmap, 0, 0, mbitmappaint); //canvas.drawpath(mpath, mpaint); log.d("ondraw", "if called"); //invalidate(); }else{ canvas.drawbitmap(mbitmap, 0, 0, mbitmappaint); log.d("ondraw", "else called"); } canvas.drawpath(mpath, mpaint); } private float mx, my; private static final float touch_tolerance = 4; private void touch_start(float x, float y) { mpath.reset(); mpath.moveto(x, y); mx = x; = y; } private void touch_move(float x, float y) { float dx = math.abs(x - mx); float dy = math.abs(y - my); if (dx >= touch_tolerance || dy >= touch_tolerance) { mpath.quadto(mx, my, (x + mx)/2, (y + my)/2); mx = x; = y; } } private void touch_up() { mpath.lineto(mx, my); // commit path our offscreen mcanvas.drawpath(mpath, mpaint); // kill don't double draw mpath.reset(); } @override public boolean ontouchevent(motionevent event) { float x = event.getx(); float y = event.gety(); switch (event.getaction()) { case motionevent.action_down: touch_start(x, y); invalidate(); break; case motionevent.action_move: touch_move(x, y); invalidate(); break; case motionevent.action_up: touch_up(); invalidate(); break; } return true; } } private static final int color_menu_id = menu.first; private static final int font_id = menu.first + 1; private static final int blur_menu_id = menu.first + 2; private static final int erase_menu_id = menu.first + 3; private static final int srcatop_menu_id = menu.first + 4; @override public boolean oncreateoptionsmenu(menu menu) { super.oncreateoptionsmenu(menu); menu.add(0, color_menu_id, 0, "color").setshortcut('3', 'c'); menu.add(0, font_id, 0, "font size").setshortcut('4', 's'); menu.add(0, blur_menu_id, 0, "effects").setshortcut('5', 'z'); menu.add(0, erase_menu_id, 0, "erase").setshortcut('5', 'z'); menu.add(0, srcatop_menu_id, 0, "save").setshortcut('5', 'z'); /**** mechanism extend filter effects? intent intent = new intent(null, getintent().getdata()); intent.addcategory(intent.category_alternative); menu.addintentoptions( menu.alternative, 0, new componentname(this, noteslist.class), null, intent, 0, null); *****/ return true; } @override public boolean onprepareoptionsmenu(menu menu) { super.onprepareoptionsmenu(menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { mpaint.setxfermode(null); mpaint.setalpha(0xff); switch (item.getitemid()) { case color_menu_id: // initialiazation(); new mycolorpickerdialog(this,this, color.red, false).show(); return true; case font_id: showdialog(); //emboss // return true; case blur_menu_id: showeffectdialog(); // return true; case erase_menu_id: //initialiazation(); mpaint.setalpha(0xff);//transperent color mpaint.setmaskfilter(null); mpaint.setxfermode(new porterduffxfermode( porterduff.mode.clear)); return true; case srcatop_menu_id: // mpaint.setxfermode(new porterduffxfermode( // porterduff.mode.src_atop)); // mpaint.setalpha(0x80); saveimagedialog2(); return true; } return super.onoptionsitemselected(item); } //src fingerpaint.java private void saveimagedialog2() { alertdialog.builder editalert = new alertdialog.builder(fingerpaint.this); editalert.settitle("please enter name want save"); final edittext input = new edittext(fingerpaint.this); linearlayout.layoutparams lp = new linearlayout.layoutparams( linearlayout.layoutparams.match_parent, linearlayout.layoutparams.match_parent); input.setlayoutparams(lp); editalert.setview(input); editalert.setpositivebutton("ok", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int whichbutton) { string name= input.gettext().tostring(); bitmap bitmap = mv.getdrawingcache(); file directory= new file(environment.getexternalstoragedirectory(),"hand brush/"); file mypath=new file(directory,name); file file = new file(mypath+".png"); try { if(!file.exists()) { file.createnewfile(); } fileoutputstream ostream = new fileoutputstream(file); bitmap.compress(compressformat.png, 10, ostream); //system.out.println("saving......................................................"+path); ostream.close(); mv.invalidate(); } catch (exception e) { e.printstacktrace(); }finally { mv.setdrawingcacheenabled(false); } } }); editalert.show(); }
}
remove in ondraw()
canvas.drawcolor(color.white);
mv= new myview(this); mv.setdrawingcacheenabled(true); mv.setbackgroundresource(r.drawable.afor); setcontentview(mv);
on draw
@override protected void ondraw(canvas canvas) { super.ondraw(canvas); canvas.drawbitmap(mbitmap, 0, 0, mbitmappaint); canvas.drawpath(mpath, mpaint); }
snap shot
Comments
Post a Comment