編輯:關於 Android 程式設計
本文透過實例,講解如何在 Android 中實作圖示拖曳的效果,並針對拖曳過程中的流暢度做了優化。
以下是完整的實作程式碼,有興趣一起探討的朋友歡迎交流。
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 { /** Called when the activity is first created. */ @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>
這個實作的核心在於自訂一個 FrameLayout 子類,透過 onTouchEvent 攔截觸控事件,並即時更新被拖曳 View 的位置,讓圖示能跟隨手指移動。希望本文所述能對大家的 Android 程式設計有所幫助。
Android Root 原理剖析:su 機制、SUID 與 adbd 降權缺陷
「Root 破解」聽起來神秘,但把原始碼攤開來看,它的核心其實只有一個動作:換掉系統裡的 su。以下從機制層面說明它為何可行,以及當年那個著名的提權缺陷是怎麼被補上的。這
Android Dialog 全面分析:五種對話框、創建流程與 PopupWindow
以 Dialog 為引線來看,Android 的彈出式訊息主要有三種:Dialog、Toast、Notification。三者各有所長,這篇的重心放在 Dialog。Di
用 FastBlur 快速實現 Android 毛玻璃效果
從 iOS 開始的模糊風潮 自從 iOS 把毛玻璃(Blur)帶進系統介面,各家平台便爭相跟進。所謂毛玻璃,就是把背景畫面做高階模糊處理,讓前景資訊浮起來,同時保留一點上
Android 5.x Notification 解析:普通、折疊與懸掛式通知
很難想像沒有 Notification 的手機生活:QQ、微信、各種 App 都靠它主動提醒我們,否則每分鐘都得自己解鎖檢查有沒有新訊息。以下介紹三種通知形式,以及 An