Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android 編程實現圖標拖動效果的方法

編輯:Android 開發實例

本文分享一個實現圖標拖動效果的例子,供大家參考交流。

最近在優化圖標拖動時的響應速率,稍微有了一點心得,索性把代碼直接貼出來,感興趣一起探討的朋友可以給我留言。

實現思路是:自定義一個 FrameLayout 子類 DragView,在其中放置一個可拖動的 View,透過 onInterceptTouchEvent 攔截觸摸事件,並在 onTouchEvent 中根據手指坐標實時調用 layout() 方法移動這個 View,從而達到跟手拖動的效果。

DragView.java:

 package com.android.dragtest; import android.content.Context; import android.util.AttributeSet; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.widget.FrameLayout; public class DragView extends FrameLayout { private static final String TAG = "DragView"; private float X; private float Y; private View mDragView; public DragView(Context context) { this(context, null); } public DragView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public DragView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); mDragView = new View(context); mDragView.setLayoutParams(new LayoutParams(60, 60)); mDragView.setBackgroundDrawable(getResources().getDrawable(R.drawable.gamecenter)); mDragView.setVisibility(View.INVISIBLE); addView(mDragView); } public boolean onInterceptTouchEvent(MotionEvent ev) { final int action = ev.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: Log.d(TAG, "===============>onInterceptTouchEvent ACTION_DOWN"); break; case MotionEvent.ACTION_MOVE: Log.d(TAG, "===============>onInterceptTouchEvent ACTION_MOVE"); break; case MotionEvent.ACTION_UP: Log.d(TAG, "===============>onInterceptTouchEvent ACTION_UP"); break; } return true; } public boolean onTouchEvent(MotionEvent ev) { final int action = ev.getAction(); X = ev.getX(); Y = ev.getY(); switch (action) { case MotionEvent.ACTION_DOWN: Log.d(TAG, "onTouchEvent ACTION_DOWN"); mDragView.layout((int)X - 30, (int)Y - 30, (int)X + 30, (int)Y + 30); mDragView.setVisibility(View.VISIBLE); break; case MotionEvent.ACTION_MOVE: Log.d(TAG, "onTouchEvent ACTION_MOVE x:" + X + " Y:" + Y); mDragView.layout((int)X - 30, (int)Y - 30, (int)X + 30, (int)Y + 30); break; case MotionEvent.ACTION_UP: Log.d(TAG, "onTouchEvent ACTION_UP"); mDragView.setVisibility(View.INVISIBLE); break; } return true; } } 

DragTestActivity.java:

 package com.android.dragtest; import android.app.Activity; import android.os.Bundle; public class DragTestActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } } 

main.xml:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <com.android.dragtest.DragView android:layout_width="match_parent" android:layout_height="match_parent"> </com.android.dragtest.DragView> </LinearLayout> 

關鍵在於 ACTION_MOVE 時不斷用新的手指坐標去刷新 mDragView 的位置,讓它始終居中於觸摸點,這樣拖動起來就比較跟手了。

希望本文所述對大家的 Android 程序設計有所幫助。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved