android Drag and Drop don't Functionality to Move Objects on Finger Touch in Android(Especially zoom in, zoom out don't function) -
this xml :
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:gravity="center" > <android.support.design.widget.appbarlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/themeoverlay.appcompat.dark.actionbar" app:elevation="0dp"> <android.support.v7.widget.toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionbarsize" android:background="?attr/colorprimary" style="@style/custom_toolbar" app:layout_scrollflags="scroll|enteralways" app:popuptheme="@style/themeoverlay.appcompat.light" /> </android.support.design.widget.appbarlayout> <relativelayout android:id="@+id/layout_title" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignparenttop="true" android:background="#ffffff" /> <framelayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="12" > <relativelayout android:id="@+id/ec_rltabselected" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="5dp" > <absolutelayout android:id="@+id/relative1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/border_rounding" > <imageview android:id="@+id/ivcardview" android:layout_width="fill_parent" android:layout_height="match_parent" /> </absolutelayout> </relativelayout> <linearlayout android:id="@+id/llbottomlayout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom" android:layout_margin="5dp" android:orientation="horizontal" android:padding="5dp" android:weightsum="0" > </linearlayout> </framelayout> <android.support.v7.widget.recyclerview android:layout_weight="1" android:layout_marginbottom="10dp" android:id="@+id/recyclerview_up" android:layout_width="match_parent" android:layout_height="150dp" /> </linearlayout>
and class :
public class clothe_codi extends appcompatactivity { private toolbar toolbar; arraylist<string> arr_id_list; clothe_dbhelper helper; sqlitedatabase db; list<clothe_codi_mini_item> codi; recyclerview recyclerview_mini_codi; //경계선 private button m_btnselectimage, m_btnsdeleteimage, m_btnzoom; private context m_context; private linearlayout m_lltoplayout; private imageview m_ivimage, m_ivtmpimage; private display m_screen; private int m_displaywidth, m_imagecount, m_viewsaddedheightemotions = 0, m_height, m_absheight = 0, m_addedviewsheighttext = 0, m_deleteeditheightwidth; private dialog m_dialog; private view.ontouchlistener m_touchimaglistener, m_strectharrowlistener; private absolutelayout m_absolutelayout, m_abstextlayout, m_abszoomlayout; private int m_widthdelete = 0, m_totaltextviewcount = 0; private float m_olddist = 1f, m_scale, m_oldx = 0, m_oldy = 0, m_dx, m_dy, m_posx, m_posy, m_prevx = 0, m_prevy = 0, m_newx, m_newy; viewtreeobserver m_vtotree; private absolutelayout.layoutparams m_layoutparams, m_layoutparamsdelete, m_layoutparamsedit; private arraylist<viewsvo> m_arrsignobjects; private bitmap m_bitmap; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.clothe_codi); //id데리고 오기 arr_id_list = new arraylist<string>(); //db만들기 helper = new clothe_dbhelper(getapplicationcontext()); db = helper.getwritabledatabase(); toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); getsupportactionbar().setdisplayhomeasupenabled(true); toolbar.setnavigationicon(r.drawable.ref_back); //toolbar상단에 글씨설정 setsupportactionbar(toolbar); getsupportactionbar().settitle("코디하기"); recyclerview_mini_codi=(recyclerview)findviewbyid(r.id.recyclerview_up); linearlayoutmanager layoutmanager_up=new linearlayoutmanager(this, linearlayoutmanager.horizontal, false); recyclerview_mini_codi.sethasfixedsize(true); recyclerview_mini_codi.setlayoutmanager(layoutmanager_up); codi=new arraylist<>(); //listview에 뿌려주기 위하여 clotheselect(); recyclerview_mini_codi.setadapter(new clothe_codi_mini_item_recycleradapter(getapplicationcontext(),codi,r.layout.activity_main)); recyclerview_mini_codi.addonitemtouchlistener(new recyclerviewonitemclicklistener(this, recyclerview_mini_codi, new recyclerviewonitemclicklistener.onitemclicklistener() { @override public void onitemclick(view v, int position) { final integer selectedpos = position; string popo = arr_id_list.get(selectedpos); cursor search; search = db.rawquery("select * clothe_list _id = '" + popo + "';", null); while (search.movetonext()){ byte[] byteimage = search.getblob(3); bitmap bitmapimage = dbbitmaputility.getbitmap(byteimage); getimagelayout(bitmapimage); } search.close(); } @override public void onitemlongclick(view v, int position) { } } )); //i made drag , drop move objects on finger touch in android m_context = clothe_codi.this; m_ivimage = (imageview) findviewbyid(r.id.ivcardview); m_absolutelayout = (absolutelayout) findviewbyid(r.id.relative1); m_lltoplayout = (linearlayout) findviewbyid(r.id.llbottomlayout); m_arrsignobjects = new arraylist<viewsvo>(); // set layout parameters absolute layout adding images. relativelayout.layoutparams rl_pr = new relativelayout.layoutparams( relativelayout.layoutparams.fill_parent, relativelayout.layoutparams.fill_parent); rl_pr.addrule(relativelayout.above, r.id.llbottomlayout); rl_pr.addrule(relativelayout.below, r.id.layout_title); m_absolutelayout.setlayoutparams(rl_pr); m_screen = ((windowmanager) getsystemservice(window_service)) .getdefaultdisplay(); m_displaywidth = m_screen.getwidth(); m_addedviewsheighttext = m_lltoplayout.getheight(); // absoulte layout height according device screen density // set layout. m_vtotree = m_absolutelayout.getviewtreeobserver(); m_vtotree.addongloballayoutlistener(new viewtreeobserver.ongloballayoutlistener() { @override public void ongloballayout() { m_absheight = m_absolutelayout.getheight(); m_absolutelayout.getviewtreeobserver() .removeglobalonlayoutlistener(this); } }); m_dialog = new dialog(this, r.style.dialog); m_dialog.setcancelable(true); } public void clotheselect() { cursor search; search = db.rawquery("select * clothe_list", null); while (search.movetonext()){ string up_down = search.getstring(1); string clothename = search.getstring(2); byte[] byteimage = search.getblob(3); bitmap bitmapimage = dbbitmaputility.getbitmap(byteimage); codi.add(new clothe_codi_mini_item(up_down, clothename, bitmapimage)); //id를 저장 arr_id_list.add(search.getstring(0)); } search.close(); } @override public boolean oncreateoptionsmenu(menu menu) { menuinflater inflater = getmenuinflater(); inflater.inflate(r.menu.main, menu); return super.oncreateoptionsmenu(menu); } @override public boolean onoptionsitemselected(menuitem item) { // todo auto-generated method stub super.onoptionsitemselected(item); switch (item.getitemid()) { case android.r.id.home: // navutils.navigateupfromsametask(this) 를 사용할 경우 모든 activity를 destroy 시키고 부모 activity로 돌아간다. // navutils.navigateupfromsametask(this); // finish() 를 사용할 경우 현재 activity를 destroy하고 부모 activity로 돌아간다. finish(); return true; case r.id.setting: intent timesetting = new intent(clothe_codi.this, ref_time_setting.class); startactivity(timesetting); return true; } return super.onoptionsitemselected(item); } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); switch (requestcode) { case 0: if (requestcode == 0 && resultcode == result_ok && null != data) { m_bitmap = null; uri selectedimage = data.getdata(); string[] filepathcolumn = { mediastore.images.media.data }; cursor cursor = getcontentresolver().query(selectedimage,filepathcolumn, null, null, null); cursor.movetofirst(); int columnindex = cursor.getcolumnindex(filepathcolumn[0]); string picturepath = cursor.getstring(columnindex); cursor.close(); m_bitmap = bitmapfactory.decodefile(picturepath); getimagelayout(m_bitmap); } break; } } /** * method set view's height dynamically according screen size. */ private void setviewsheightdynamically() { if (m_absheight <= 500) { m_layoutparamsdelete = new absolutelayout.layoutparams(20, 20, 110,0); m_layoutparamsedit = new absolutelayout.layoutparams(20, 20, 110,110); m_deleteeditheightwidth = 20; } else if (m_absheight >= 900) { m_layoutparamsdelete = new absolutelayout.layoutparams(40, 40, 130,0); m_layoutparamsedit = new absolutelayout.layoutparams(40, 40, 130,130); m_deleteeditheightwidth = 40; } else { m_layoutparamsdelete = new absolutelayout.layoutparams(35, 35, 140,0); m_layoutparamsedit = new absolutelayout.layoutparams(35, 35, 120,120); m_deleteeditheightwidth = 35; } } /** * method add image setting , creating views dynamically * delete , zoom option. */ @suppresswarnings("deprecation") private void getimagelayout(bitmap p_bitmap) { viewsvo m_signvo; //check images count .set count limiting number of images add on screen. if (m_imagecount < 1) { m_viewsaddedheightemotions = m_viewsaddedheightemotions + 90; m_imagecount++; } m_btnsdeleteimage = new button(m_context); m_btnzoom = new button(m_context); m_ivtmpimage = new imageview(m_context); setviewsheightdynamically(); m_btnsdeleteimage.setlayoutparams(m_layoutparamsdelete); m_btnsdeleteimage.setbackgrounddrawable(getresources().getdrawable( r.drawable.ic_deletered)); m_btnsdeleteimage.setid(0); m_btnsdeleteimage.setonclicklistener(new imagedeletelistener()); m_btnzoom.setlayoutparams(m_layoutparamsedit); m_btnzoom.setbackgrounddrawable(getresources().getdrawable( r.drawable.right_arrow)); m_btnzoom.setid(0); m_abstextlayout = new absolutelayout(m_context); m_abszoomlayout = new absolutelayout(m_context); //500여기 숫자를 늘리면 이미지 자체 화질이 좋아진다. m_ivtmpimage.setimagebitmap(bitmap.createscaledbitmap(p_bitmap, 500, 500, true)); m_abstextlayout.setlayoutparams(new absolutelayout.layoutparams(absolutelayout.layoutparams.wrap_content,absolutelayout.layoutparams.wrap_content, 0, 0)); m_abszoomlayout.setlayoutparams(new absolutelayout.layoutparams(absolutelayout.layoutparams.wrap_content,absolutelayout.layoutparams.wrap_content, 0, 0)); if (m_absheight >= 900) //setlayoutparams를 이용하여 위의 이미지 크기를 지정한다. m_ivtmpimage.setlayoutparams(new framelayout.layoutparams(100, 100)); else m_ivtmpimage.setlayoutparams(new framelayout.layoutparams(80, 80)); m_ivtmpimage.setbackgroundcolor(color.transparent); m_abstextlayout.addview(m_btnsdeleteimage); if (m_absheight >= 900) m_abszoomlayout.setpadding(20, 20, 15, 15); else m_abszoomlayout.setpadding(15, 15, 15, 15); /*m_abszoomlayout.setbackgroundresource(r.drawable.dashedbordersmall);*/ m_abszoomlayout.addview(m_ivtmpimage); m_abstextlayout.addview(m_abszoomlayout); m_abstextlayout.addview(m_btnzoom); m_abstextlayout.setdrawingcacheenabled(true); m_abstextlayout.setclickable(true); m_abstextlayout.setid(0); m_ivtmpimage.setid(0); m_vtotree =m_abstextlayout.getviewtreeobserver(); m_vtotree.addongloballayoutlistener(new viewtreeobserver.ongloballayoutlistener() { @override public void ongloballayout() { m_abstextlayout.getviewtreeobserver() .removeglobalonlayoutlistener(this); } }); /** * add views arraylist added screen * further perform deletion of each views. */ m_signvo = new viewsvo(); m_arrsignobjects.add(0, m_signvo); m_absolutelayout.addview(m_abstextlayout); // image touch listener move image ontouch event on screen. m_touchimaglistener = new view.ontouchlistener() { @override public boolean ontouch(view v, motionevent event) { switch (event.getaction() & motionevent.action_mask) { case motionevent.action_down: m_oldx = event.getx(); m_oldy = event.gety(); break; case motionevent.action_up: case motionevent.action_pointer_up: break; case motionevent.action_move: m_dx = event.getx() - m_oldx; m_dy = event.gety() - m_oldy; m_posx = m_prevx + m_dx; m_posy = m_prevy + m_dy; if (m_posx > 0 && m_posy > 0&& (m_posx + v.getwidth()) < m_absolutelayout.getwidth()&& (m_posy + v.getheight()) < m_absolutelayout.getheight()) { v.setlayoutparams(new absolutelayout.layoutparams(v.getmeasuredwidth(), v.getmeasuredheight(),(int) m_posx, (int) m_posy)); m_prevx = m_posx; m_prevy = m_posy; } break; } return false; } }; // listener arrow ontouch of arrow zoomin , zoomout image. m_strectharrowlistener = new view.ontouchlistener() { @override public boolean ontouch(view v, motionevent event) { view view; view = v; v.setclickable(true); v.setdrawingcacheenabled(true); absolutelayout m_abslayout = null; switch (event.getaction() & motionevent.action_mask) { case motionevent.action_down: m_oldx = event.getx(); m_oldy = event.gety(); break; case motionevent.action_up: case motionevent.action_pointer_up: break; case motionevent.action_move: m_newx = event.getx(); m_newy = event.gety(); float newdist = m_newx - m_oldx; if (m_newx > m_oldx && m_newy > m_oldy) { if (newdist > 0.0f) { m_scale = 1; m_abslayout = (absolutelayout) v.getparent(); int m_hightofimage = (int) (m_scale + (((imageview) ((absolutelayout) m_abslayout.getchildat(1)).getchildat(0)).getheight())); int m_widthofimage = (int) (m_scale + (((imageview) ((absolutelayout) m_abslayout.getchildat(1)).getchildat(0)).getwidth())); m_widthdelete = (int) (m_scale + ((((absolutelayout) m_abslayout.getchildat(1))).getwidth())); if (m_abslayout.getbottom() <= (m_ivimage.getbottom()) && m_abslayout.getright() <= (m_displaywidth)) { m_layoutparams = new absolutelayout.layoutparams( m_widthofimage, m_hightofimage, 0, 0); ((imageview) ((absolutelayout) m_abslayout.getchildat(1)).getchildat(0)).setlayoutparams(m_layoutparams); m_layoutparams = new absolutelayout.layoutparams( absolutelayout.layoutparams.wrap_content,absolutelayout.layoutparams.wrap_content,m_abslayout.getleft(), m_abslayout.gettop()); m_abslayout.setlayoutparams(m_layoutparams); ((button) m_abslayout.getchildat(0)).setlayoutparams(new absolutelayout.layoutparams(m_deleteeditheightwidth,m_deleteeditheightwidth,m_widthdelete, 0)); ((button) m_abslayout.getchildat(2)).setlayoutparams(new absolutelayout.layoutparams(m_deleteeditheightwidth,m_deleteeditheightwidth,m_widthdelete, m_widthdelete)); m_hightofimage = (int) (m_scale + (((absolutelayout) m_abslayout.getchildat(1)).getheight())); m_widthofimage = (int) (m_scale + (((absolutelayout) m_abslayout.getchildat(1)).getwidth())); m_layoutparams = new absolutelayout.layoutparams( m_widthofimage, m_hightofimage,((absolutelayout) m_abslayout .getchildat(1)).getleft(),((absolutelayout) m_abslayout .getchildat(1)).gettop()); ((absolutelayout) m_abslayout.getchildat(1)).setlayoutparams(m_layoutparams); } } } if (m_newx < m_oldx && m_newy < m_oldy) { m_abslayout = (absolutelayout) view.getparent(); int m_hightofimage = (int) (((imageview) ((absolutelayout) m_abslayout.getchildat(1)).getchildat(0)).getheight() - m_scale); int m_widthofimage = (int) (((imageview) ((absolutelayout) m_abslayout.getchildat(1)).getchildat(0)).getwidth() - m_scale); m_widthdelete = (int) (((absolutelayout) m_abslayout.getchildat(1)).getwidth() - m_scale); m_layoutparams = new absolutelayout.layoutparams( m_widthofimage, m_hightofimage, 0, 0); ((imageview) ((absolutelayout) m_abslayout.getchildat(1)).getchildat(0)).setlayoutparams(m_layoutparams); m_layoutparams = new absolutelayout.layoutparams( absolutelayout.layoutparams.wrap_content, absolutelayout.layoutparams.wrap_content, m_abslayout.getleft(), m_abslayout.gettop()); m_abslayout.setlayoutparams(m_layoutparams); ((button) m_abslayout.getchildat(0)).setlayoutparams(new absolutelayout.layoutparams(m_deleteeditheightwidth, m_deleteeditheightwidth, m_widthdelete,0)); ((button) m_abslayout.getchildat(2)).setlayoutparams(new absolutelayout.layoutparams(m_deleteeditheightwidth, m_deleteeditheightwidth, m_widthdelete,m_widthdelete)); m_hightofimage = (int) ((((absolutelayout) m_abslayout .getchildat(1)).getheight()) - m_scale); m_widthofimage = (int) ((((absolutelayout) m_abslayout .getchildat(1)).getwidth()) - m_scale); m_layoutparams = new absolutelayout.layoutparams( m_widthofimage, m_hightofimage, ((absolutelayout) m_abslayout.getchildat(1)).getleft(),((absolutelayout) m_abslayout.getchildat(1)).gettop()); ((absolutelayout) m_abslayout.getchildat(1)) .setlayoutparams(m_layoutparams); } break; } return false; } }; m_abstextlayout.setontouchlistener(m_touchimaglistener); m_btnzoom.setontouchlistener(m_strectharrowlistener); } // delete button listener show alert , confirmation deleting // items. private class imagedeletelistener implements view.onclicklistener { @override public void onclick(final view v) { alertdialog.builder alertdialogbuilder = new alertdialog.builder( m_context); alertdialogbuilder.settitle("drag & drop"); alertdialogbuilder .setmessage("are sure want delete ?") .setcancelable(false) .setpositivebutton("yes", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int id) { m_imagecount--; (int counter = 0; counter < m_arrsignobjects .size(); counter++) { if (v.getid() == m_arrsignobjects.get(counter).getviewid()) { if (m_totaltextviewcount <= 0) { m_addedviewsheighttext = m_addedviewsheighttext - m_arrsignobjects.get(counter).getviewheight(); } else { m_totaltextviewcount--; } m_absolutelayout.removeview((view) v .getparent()); m_arrsignobjects.remove(counter); break; } } } }).setnegativebutton("no",new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int id) { // if button clicked, close // dialog box , nothing dialog.cancel(); } }); // create alert dialog alertdialog alertdialog = alertdialogbuilder.create(); // show alertdialog.show(); } } }
i made drag , drop move objects on finger touch in android
reference site : http://grishma102.blogspot.kr/2013/10/drag-and-drop-functionality-to-move.html
but, activity's zoom in, zoom out don't function when move objects on finger touch, objects zoom out , screen out.
please me..
try overriding ondrag() listener instead of ontouch()
view.setondraglistener(new view.ondraglistener() { @override public boolean ondrag(view view, dragevent dragevent) { return true; } });
Comments
Post a Comment