android - How can an image does not go over another to be dragged or moved? -
i have code can move multiple images mouse drag image passes on other. want move without going over. know how do? image can not spend 1 another.
package br.com.example.teste8; import android.app.activity; import android.os.bundle; import android.view.motionevent; import android.view.view; import android.view.viewgroup; import android.widget.imageview; import android.widget.relativelayout; public class mainactivity extends activity implements view.ontouchlistener { private imageview mimageview; private viewgroup mrrootlayout; private int _xdelta; private int _ydelta; relativelayout.layoutparams layoutparams; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mrrootlayout = (viewgroup) findviewbyid(r.id.marco); mimageview = (imageview) mrrootlayout.findviewbyid(r.id.imageview); relativelayout.layoutparams layoutparams = new relativelayout.layoutparams(50,50); layoutparams.leftmargin = 0; layoutparams.topmargin = 10; mimageview.bringtofront(); mimageview.setlayoutparams(layoutparams); mimageview.setontouchlistener(this); mimageview = (imageview) mrrootlayout.findviewbyid(r.id.imageview2); relativelayout.layoutparams layoutparams1 = new relativelayout.layoutparams(50,50); layoutparams1.leftmargin = 50; layoutparams1.topmargin = 10; mimageview.setlayoutparams(layoutparams1); mimageview.setontouchlistener(this); mimageview = (imageview) mrrootlayout.findviewbyid(r.id.imageview3); relativelayout.layoutparams layoutparams2 = new relativelayout.layoutparams(50,50); layoutparams2.leftmargin = 100; layoutparams2.topmargin = 10; mimageview.setlayoutparams(layoutparams2); mimageview.setontouchlistener(this); } public boolean ontouch(view view, motionevent event) { final int x = (int) event.getrawx(); final int y = (int) event.getrawy(); switch (event.getaction() & motionevent.action_mask) { case motionevent.action_up: break; case motionevent.action_move: relativelayout.layoutparams layoutparams = (relativelayout.layoutparams) view.getlayoutparams(); layoutparams.leftmargin = x - _xdelta; layoutparams.topmargin = y - _ydelta; layoutparams.rightmargin = -50; layoutparams.bottommargin = -50; view.setlayoutparams(layoutparams); break; } mrrootlayout.invalidate(); return true; } }
xml:
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/marco" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" android:clickable="true"> <textview android:id="@+id/txt" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/hello_world" android:gravity="top" /> <imageview android:id="@+id/imageview" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/border" android:contentdescription="@null" android:src="@drawable/ic_launcher" /> <imageview android:id="@+id/imageview2" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/border" android:contentdescription="@null" android:src="@drawable/ic_launcher" /> <imageview android:id="@+id/imageview3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginleft="48dp" android:background="@drawable/border" android:contentdescription="@null" android:src="@drawable/ic_launcher" /> </relativelayout>
you didn't if images overlapped. suppose images block each other, need nothing while moving cause overlapping. modified following codes:
package br.com.example.teste8; import android.app.activity; import android.os.bundle; import android.view.motionevent; import android.view.view; import android.view.viewgroup; import android.widget.imageview; import android.widget.relativelayout; public class mainactivity extends activity implements view.ontouchlistener { private imageview mimageview; private imageview mimages[] = new imageview[3]; private viewgroup mrrootlayout; private int _xdelta; private int _ydelta; relativelayout.layoutparams layoutparams; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mrrootlayout = (viewgroup) findviewbyid(r.id.marco); mimageview = (imageview) mrrootlayout.findviewbyid(r.id.imageview); relativelayout.layoutparams layoutparams = new relativelayout.layoutparams( 50, 50); layoutparams.leftmargin = 0; layoutparams.topmargin = 10; mimageview.bringtofront(); mimageview.setlayoutparams(layoutparams); mimageview.setontouchlistener(this); mimages[0] = mimageview; mimageview = (imageview) mrrootlayout.findviewbyid(r.id.imageview2); relativelayout.layoutparams layoutparams1 = new relativelayout.layoutparams( 50, 50); layoutparams1.leftmargin = 50; layoutparams1.topmargin = 10; mimageview.setlayoutparams(layoutparams1); mimageview.setontouchlistener(this); mimages[1] = mimageview; mimageview = (imageview) mrrootlayout.findviewbyid(r.id.imageview3); relativelayout.layoutparams layoutparams2 = new relativelayout.layoutparams( 50, 50); layoutparams2.leftmargin = 100; layoutparams2.topmargin = 10; mimageview.setlayoutparams(layoutparams2); mimageview.setontouchlistener(this); mimages[2] = mimageview; } public boolean ontouch(view view, motionevent event) { final int x = (int) event.getrawx(); final int y = (int) event.getrawy(); switch (event.getaction() & motionevent.action_mask) { case motionevent.action_up: break; case motionevent.action_move: int left = x - _xdelta; int top = y - _ydelta; boolean overlapped = false; for(int i=0; i<mimages.length; i++ ) { if( view == mimages[i] ) { continue; } relativelayout.layoutparams lp = (relativelayout.layoutparams) mimages[i] .getlayoutparams(); if( left >= lp.leftmargin - lp.width && left <= lp.leftmargin + lp.width && top >= lp.topmargin - lp.height && top <= lp.topmargin + lp.height ) { overlapped = true; break; } } if( overlapped == false ) { relativelayout.layoutparams layoutparams = (relativelayout.layoutparams) view .getlayoutparams(); layoutparams.leftmargin = x - _xdelta; layoutparams.topmargin = y - _ydelta; layoutparams.rightmargin = -50; layoutparams.bottommargin = -50; view.setlayoutparams(layoutparams); } break; } mrrootlayout.invalidate(); return true; } }
Comments
Post a Comment