I want to move an imageview along with finger using onTouchListener() in android......
I have created an xml -->
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/rel_slide"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/background_dark" >
<ImageView
android:id="@+id/img_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"/>"
</RelativeLayout>
</RelativeLayout>
Now in Activity file I implemented onTouchListener to this imageView: img_arrow
here is the code:
((ImageView) findViewById(R.id.img_arrow)).setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
switch (arg1.getAction()) {
case MotionEvent.ACTION_MOVE:
LayoutParams layoutParams = (LayoutParams)v.getLayoutParams();
layoutParams.leftMargin=(int)arg1.getX();
layoutParams.topMargin=(int)arg1.getY();
v.setLayoutParams(layoutParams);
break;
}
return true;
}
});
But this results in flikering of imageView showing 2 at a time may be because of very fast setting of layoutParams when ACTION_MOVE called.....plz help
Hey I would suggest you to not to play with the view. Its better to set an animation on the view and play with that animation object to performing all the actions. Just invisible the view on onTouchEvent and let the animation object work done for u. You can see the example of swipeToDismiss in list view https://github.com/47deg/android-swipelistview or any other swiping examples of top developers. Happy Coding!!
[New] I also done trick. I wanna make an swipe to unlock feature so I used a seekbar and customized it. It worked great or me.
Try one thing remove
android:src="@drawable/ic_launcher"
from
<ImageView
android:id="@+id/img_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"/>
and set the image from the class itself on run time. I hope this helps you a bit.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.