編輯:關於Android編程
package cn.patience7; import android.os.AsyncTask; import android.os.Bundle; import android.view.GestureDetector; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.view.ViewTreeObserver; import android.view.ViewTreeObserver.OnPreDrawListener; import android.widget.RelativeLayout; import android.app.Activity; import android.content.Context; /** * Demo描述: * 滑動菜單SlidingMenu完整詳細示例 * * 布局文件: * 采用相對布局,兩個界面是重疊在一起的,分別為aboveView和belowView. * * 實現原理: * 對aboveView的Touch事件進行監聽 * 即mBboveView.setOnTouchListener(new TouchListenerImpl()) * 在TouchListenerImpl中: * 1 ACTION_UP時將aboveView彈回到屏幕的左右兩側 * 2 除ACTION_UP之外的Action交給手勢GestureDetector處理 * * 所以通過aboveView的移動來遮掩或者顯示belowView,從而達到 側滑菜單的效果 * * * 備注說明: * 該Demo與前面兩個側滑菜單實現原理是差不多但在代碼實現上使用了GestureDetector */ public class MainActivity extends Activity { private View mAboveView; private View mBelowView; private float scrollX = 0; private Context mContext; private int screenWidth = 0; private boolean isMeasured = false; private int MAX_SCROLL_DISTANCE = 0; private GestureDetector mGestureDetector; private GestureListenerImpl mGestureListenerImpl; private SlowlyMoveAsyncTask mSlowlyMoveAsyncTask; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); initView(); } private void initView() { mContext = this; mGestureListenerImpl = new GestureListenerImpl(); mGestureDetector = new GestureDetector(mContext, mGestureListenerImpl); mGestureDetector.setIsLongpressEnabled(false); mAboveView = findViewById(R.id.aboveLinearLayout); mAboveView.setOnTouchListener(new TouchListenerImpl()); mBelowView = findViewById(R.id.belowLinearLayout); initData(); } /** * 1 將aboveView的寬度設置為屏幕的寬度,從而完全遮掩belowView * 2 MAX_SCROLL_DISTANCE為aboveView向屏幕左側的最大滑動距離 */ private void initData() { ViewTreeObserver viewTreeObserver = mAboveView.getViewTreeObserver(); viewTreeObserver.addOnPreDrawListener(new OnPreDrawListener() { @Override public boolean onPreDraw() { if (!isMeasured) { screenWidth = getWindowManager().getDefaultDisplay().getWidth(); RelativeLayout.LayoutParams aboveViewLayoutParams =(RelativeLayout.LayoutParams) mAboveView.getLayoutParams(); aboveViewLayoutParams.width = screenWidth; mAboveView.setLayoutParams(aboveViewLayoutParams); MAX_SCROLL_DISTANCE = mBelowView.getWidth(); isMeasured = true; } return true; } }); } private class TouchListenerImpl implements OnTouchListener { @Override public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_UP) { RelativeLayout.LayoutParams aboveViewLayoutParams =(RelativeLayout.LayoutParams) mAboveView.getLayoutParams(); if (aboveViewLayoutParams.leftMargin > (-screenWidth / 2)) { // 手指往左滑時,未及屏幕一半時抬起.歸位 mSlowlyMoveAsyncTask = new SlowlyMoveAsyncTask(); mSlowlyMoveAsyncTask.execute(20); } else { // 手指往左滑時,超過屏幕一半時抬起.歸位 mSlowlyMoveAsyncTask = new SlowlyMoveAsyncTask(); mSlowlyMoveAsyncTask.execute(-20); } } return mGestureDetector.onTouchEvent(event); } } private class GestureListenerImpl implements GestureDetector.OnGestureListener { @Override public boolean onDown(MotionEvent arg0) { return true; } @Override public boolean onFling(MotionEvent arg0, MotionEvent arg1, float arg2,float arg3) { return false; } @Override public void onLongPress(MotionEvent arg0) { } @Override public boolean onScroll(MotionEvent arg0, MotionEvent arg1,float distanceX, float distanceY) { scrollX = scrollX + distanceX; RelativeLayout.LayoutParams aboveViewLayoutParams =(RelativeLayout.LayoutParams) mAboveView.getLayoutParams(); aboveViewLayoutParams.leftMargin = (int) (aboveViewLayoutParams.leftMargin - scrollX); // 手指往右邊滑動的極限,防止越界 if (aboveViewLayoutParams.leftMargin >= 0) { aboveViewLayoutParams.leftMargin = 0; } // 手指往左邊滑動的極限,防止越界 if (-aboveViewLayoutParams.leftMargin >= MAX_SCROLL_DISTANCE) { aboveViewLayoutParams.leftMargin = -MAX_SCROLL_DISTANCE; } mAboveView.setLayoutParams(aboveViewLayoutParams); return false; } @Override public void onShowPress(MotionEvent motionEvent) { } @Override public boolean onSingleTapUp(MotionEvent motionEvent) { return false; } } // 以下為異步任務,負責處理手指抬起時布局向屏幕左右兩側彈回 private class SlowlyMoveAsyncTask extends AsyncTask{ @Override protected Void doInBackground(Integer... params) { RelativeLayout.LayoutParams aboveViewLayoutParams = (RelativeLayout.LayoutParams) mAboveView.getLayoutParams(); int leftMargin = aboveViewLayoutParams.leftMargin; // 需要移動的次數 int move_times = 0; // 總共需要彈回的距離 int all_move_distance = 0; // 每次彈回的距離 int every_move_distance = Math.abs(params[0]); // 往屏幕右邊移動 if (params[0] > 0) { all_move_distance = Math.abs(leftMargin); // 往屏幕左邊移動 } else { all_move_distance = MAX_SCROLL_DISTANCE - Math.abs(leftMargin); } // 計算需要移動的次數 if (all_move_distance % every_move_distance == 0) { move_times = all_move_distance / every_move_distance; } else { move_times = all_move_distance / every_move_distance + 1; } System.out.println("--> all_move_distance=" + all_move_distance); System.out.println("--> every_move_distance=" + every_move_distance); System.out.println("--> move_times=" + move_times); // 移動的過程 for (int i = 0; i < move_times; i++) { publishProgress(params[0]); try { Thread.sleep(20); } catch (Exception e) { } } return null; } @Override protected void onProgressUpdate(Integer... values) { super.onProgressUpdate(values); int every_move_distance = values[0]; RelativeLayout.LayoutParams aboveViewLayoutParams = (RelativeLayout.LayoutParams) mAboveView.getLayoutParams(); if (every_move_distance > 0) { if (aboveViewLayoutParams.leftMargin < 0) { aboveViewLayoutParams.leftMargin += every_move_distance; // 處理最後一次滑動後可能越界的情況 if (aboveViewLayoutParams.leftMargin > 0) { aboveViewLayoutParams.leftMargin = 0; } mAboveView.setLayoutParams(aboveViewLayoutParams); } } else { if (aboveViewLayoutParams.leftMargin > (-MAX_SCROLL_DISTANCE)) { aboveViewLayoutParams.leftMargin -= (-every_move_distance); // 處理最後一次滑動後可能越界的情況 if (aboveViewLayoutParams.leftMargin < -MAX_SCROLL_DISTANCE) { aboveViewLayoutParams.leftMargin = -MAX_SCROLL_DISTANCE; } mAboveView.setLayoutParams(aboveViewLayoutParams); } } } } }
一、效果圖 二、代碼 復制代碼 代碼如下: public class TextSubView extends TextView { private TextPaint m
一、著色游戲概述近期群裡偶然看到一哥們在群裡聊不規則圖像填充什麼四聯通、八聯通什麼的,就本身好學務實的態度去查閱了相關資料。對於這類著色的資料,最好的就是去搜索些相關ap
由於剛踏入Android逆向分析領域,因此有許多的不懂,所以得不斷地去學習。因為是入門,我又有一定的Android應用開發基礎,所以先從一些簡單工程的反編譯開始入手,先了
最近在進行android開發過程中,在將 Intent傳遞給調用的組件並完成組件的調用時遇到點困難,並且之前對Intent的學習也是一知半解,最近特意為此拿出一些時間,對