English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
This control shares the specific code for the Android gesture sliding control with everyone for reference, and the specific content is as follows
1Create a custom control class: MyView
public class MyView extends Button{}} //Record the coordinate values after the last slide private int lastX; private int lastY; public MyView(Context context) { super(context); // TODO Auto-generated constructor stub } public MyView(Context context, AttributeSet attrs) { super(context, attrs); } @Override public boolean onTouchEvent(MotionEvent event) { // Get the xy values of the view relative to the phone screen int x = (int) event.getRawX(); int y = (int) event.getRawY(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: break; case MotionEvent.ACTION_MOVE: int deltaX = x-lastX; int deltaY = y-lastY; int translationX = (int) (ViewHelper.getTranslationX(this) + deltaX); int translationY = (int) (ViewHelper.getTranslationY(this) + deltaY); ViewHelper.setTranslationX(this, translationX); ViewHelper.setTranslationY(this, translationY); break; case MotionEvent.ACTION_UP: break; default: break; } lastX = x; lastY = y; return true; }
The above code is a custom button class that overrides the onTouchEvent() method to listen for user sliding. Since we're talking about sliding, there must be a statement about the offset.
translationX and translationY are the offset values of the View's top-left corner relative to the parent layout. Animation sliding is achieved through the third-party library nineoldandroids.
ViewHelper.getTranslationY(this) calculates the offset of the View, the initial value is 0, the offset value to the left is negative, and the offset value to the right is positive.
2.xml layout
<&63;xml version="1.0" encoding="utf-8"&63> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <com.example.administrator.slide.MyView android:id="@"+id/myview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="I can slide"/> </RelativeLayout>
That's all for this article, I hope it will be helpful to everyone's learning, and I also hope everyone will support Yell Tutorial more.
Declaration: The content of this article is from the network, the copyright belongs to the original author, the content is contributed and uploaded by Internet users spontaneously, this website does not own the ownership, does not undergo artificial editing, and does not assume relevant legal liability. If you find any content suspected of copyright infringement, please send an email to notice#w3Please send an email to codebox.com (replace # with @ when sending an email) to report abuse, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.