Android應用開發常用知識

來源:互聯網
上載者:User

標籤:android

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訊息發送出去了,就是單擊時間,如果在這個時間內沒有發送出去,就是雙擊事件。

Android應用開發常用知識

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.