English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
يقدم هذا التحكم للمستخدمين رمز التحكم في اللمس السريع لـ Android كمرجع، والتفاصيل التالية
1.إنشاء فئة التحكم المخصص: MyView
public class MyView extends Button{ //يُسجل آخر موقع تحرك اللمس 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 value 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, rewriting the onTouchEvent() method to listen for user sliding, since we are talking about sliding, there must be a statement about the offset.
translationX and translationY are the offset amounts of the View's top-left corner relative to the parent layout. Animation sliding is implemented through the third-party library nineoldandroids.
ViewHelper.getTranslationY(this) calculates the offset of the View, the initial value is 0, the offset to the left is negative, and the offset to the right is positive.
2.xml النسخة
<?xml version="1.0" encoding="utf-8"?> <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="أستطيع السحب"/> </RelativeLayout>
هذا هو نهاية محتويات هذا المقال، آمل أن يكون قد ساعدكم في التعلم، وأتمنى أن تدعموا وتعطوا صوتًا لتدريبات呐喊.
بيان: محتويات هذا المقال تم جمعها من الإنترنت، ملكية المحتويات للملكين الأصليين، تم جمع المحتويات من قبل المستخدمين عبر الإنترنت بشكل متعاوني وتحميلها بشكل مستقل، لا تملك هذا الموقع حقوق الملكية، لم يتم تعديل المحتويات بشكل يدوي، ولا يتحمل هذا الموقع أي مسؤولية قانونية متعلقة بذلك. إذا اكتشفت محتويات مشكوك فيها في حقوق النسخ، فلا تتردد في إرسال بريد إلكتروني إلى: notice#oldtoolbag.com (عند إرسال البريد الإلكتروني، يرجى استبدال # ب @) لإبلاغنا، وقدم الأدلة ذات الصلة، وسنقوم بإزالة المحتويات المشكوك فيها فور التحقق منها.