1. 程式人生 > >Android應用開發經常使用知識

Android應用開發經常使用知識

filter orf 沒有 power nal err align 父類 layout

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

6、ImageView的setImageResources方法

ImageView的setImageResources方法不僅能夠接受drawable的資源,還能夠接受color資源:

imageView.setImageResource(R.drawable.XX);
imageView.setImageResource(R.color.XX);

7、android:addStatesFromChildren

屬性說明該viewgroup的drawable屬性是否把它的子類的drawable的state包括進來.測試中linearlayout假設不包括該屬性(false),當子widget被點擊時不會出現被選中的狀態。也就是子類的state不會被傳遞給父類了。
比方要實現一個LinearLayout中包括一個TextView和一個EditText,在用戶點的時候實現焦點的效果。將android:addStatesFromChildren設為true,當組中的EditText或是Button獲取焦點時,將Layout的Background設置成對應EditText或的Button的Drawable ,這樣看上去該linear中的view是一個總體。


8、android:descendantFocusability

該屬性是當一個為view獲取焦點時,定義viewGroup和其子控件兩者之間的關系。

屬性的值有三種:
beforeDescendants:viewgroup會優先其子類控件而獲取到焦點
afterDescendants:viewgroup僅僅有當其子類控件不須要獲取焦點時才獲取焦點
blocksDescendants:viewgroup會覆蓋子類控件而直接獲得焦點

9、手機息屏控制

			PowerManager pm = (PowerManager) mActivity
					.getSystemService(Context.POWER_SERVICE);
			WakeLock mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK,
					TAG);
			mWakeLock.acquire();
			//做你想做的時。

。 mWakeLock.release();


newWakeLock的第一個參數有三種不同選擇:
PowerManager.PARTIAL_WAKE_LOCK
PowerManager.SCREEN_DIM_WAKE_LOCK
PowerManager.SCREEN_BRIGHT_WAKE_LOCK

10、android:windowSoftInputMode

1、當有焦點產生時,軟鍵盤是隱藏還是顯示
2、是否降低活動主窗體大小以便騰出空間放軟鍵盤

11、RelativeLayout 實現類似 LinearLayout layout_weight 的效果:

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:text="Test1"
        android:background="@android:color/holo_blue_light"
        android:textSize="16dp"/>

    <TextView
        android:id="@+id/text2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_toEndOf="@id/text1"
        android:text="Test2"
        android:background="@android:color/holo_red_light"
        android:textSize="16dp"/>

</RelativeLayout>

12、Handler.removeCallbacksAndMessages(null)

在使用Handler中,假設Activity退出之後我們不再須要發送消息或者運行Runnable,那麽就要在onDestory中運行這段代碼。否則可能會引起報錯。由於可能在onDestory之後才會這些Handler發出的動作。




Android應用開發經常使用知識