Android應用開發常用知識

jia635發表於2014-07-21

在其他網站看到的,Mark一下

1、最近開啟的應用不在最近任務列表中顯示

android:excludeFromRecents="true"

設定為true,則排除在最近任務列表之外,不在最近任務列表中顯示

2、判斷一個一個String str 是否為NULL或者是否為空字串

TextUtils.isEmpty(str)

3、android:imeOptions="actionSearch|flagNoFullscreen"的用法

在做一個把EditText放到到ActionBar中作為搜尋框的功能時,設定EditText的屬性為android:imeOptions="actionSearch",會遇到一個問題,當在橫屏時,EditText的寬度會填充掉螢幕上除了軟鍵盤之外的地方,與需求不符,改為android:imeOptions="actionSearch|flagNoFullscreen"後就OK了。

4、改變圖片亮度的方法

1、使用image.setColorFilter(Color.GRAY,PorterDuff.Mode.MULTIPLY);可以使圖片變暗,然後使用image.clearColorFilter();清除濾鏡,恢復到原來的亮度;
2、使用
int brightness = -80;
ColorMatrix matrix = new ColorMatrix(); 
matrix.set(new float[] { 1, 0, 0, 0, brightness, 0, 1, 0, 0, 
brightness, 0, 0, 1, 0, brightness, 0, 0, 0, 1, 0 }); 
v.setColorFilter(new ColorMatrixColorFilter(matrix));
但這種方法會使顏色不太正常,圖片留有黑邊;

5、用Handler來實現有時間間隔事件的判斷

看到Android中GestureDetector.java是用下面程式碼實現手勢的單擊和雙擊判斷的:

public boolean onTouchEvent(MotionEvent ev) {
    ……
        case MotionEvent.ACTION_DOWN:
            if (mDoubleTapListener != null) {
                boolean hadTapMessage = mHandler.hasMessages(TAP);
                if (hadTapMessage) mHandler.removeMessages(TAP);
                if ((mCurrentDownEvent != null) && (mPreviousUpEvent != null) && hadTapMessage &&
                        isConsideredDoubleTap(mCurrentDownEvent, mPreviousUpEvent, ev)) {
                    // This is a second tap
                    mIsDoubleTapping = true;
                    // Give a callback with the first tap of the double-tap
                    handled |= mDoubleTapListener.onDoubleTap(mCurrentDownEvent);
                    // Give a callback with down event of the double-tap
                    handled |= mDoubleTapListener.onDoubleTapEvent(ev);
                } else {
                    // This is a first tap
                    mHandler.sendEmptyMessageDelayed(TAP, DOUBLE_TAP_TIMEOUT);
                }
            }
    ……
}
 
    private boolean isConsideredDoubleTap(MotionEvent firstDown, MotionEvent firstUp,
            MotionEvent secondDown) {
        if (!mAlwaysInBiggerTapRegion) {
            return false;
        }
 
        final long deltaTime = secondDown.getEventTime() - firstUp.getEventTime();
        if (deltaTime > DOUBLE_TAP_TIMEOUT || deltaTime < DOUBLE_TAP_MIN_TIME) {
            return false;
        }
 
        int deltaX = (int) firstDown.getX() - (int) secondDown.getX();
        int deltaY = (int) firstDown.getY() - (int) secondDown.getY();
        return (deltaX * deltaX + deltaY * deltaY < mDoubleTapSlopSquare);
    }
 
    private class GestureHandler extends Handler {
        GestureHandler() {
            super();
        }
 
        GestureHandler(Handler handler) {
            super(handler.getLooper());
        }
 
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case SHOW_PRESS:
                mListener.onShowPress(mCurrentDownEvent);
                break;
                 
            case LONG_PRESS:
                dispatchLongPress();
                break;
                 
            case TAP:
                // If the user's finger is still down, do not count it as a tap
                if (mDoubleTapListener != null) {
                    if (!mStillDown) {
                        mDoubleTapListener.onSingleTapConfirmed(mCurrentDownEvent);
                    } else {
                        mDeferConfirmSingleTap = true;
                    }
                }
                break;
 
            default:
                throw new RuntimeException("Unknown message " + msg); //never
            }
        }
具體可以參考原始碼,這裡是妙用了mHandler.sendEmptyMessageDelayed,如果在DOUBLE_TAP_TIMEOUT時間內mHandler把TAP訊息傳送出去了,就是單擊時間,如果在這個時間內沒有傳送出去,就是雙擊事件。


相關文章