Android应用开发经常使用知识
在其它站點看到的,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 tapmIsDoubleTapping = true;// Give a callback with the first tap of the double-taphandled |= mDoubleTapListener.onDoubleTap(mCurrentDownEvent);// Give a callback with down event of the double-taphandled |= mDoubleTapListener.onDoubleTapEvent(ev);} else {// This is a first tapmHandler.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());}@Overridepublic 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 tapif (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消息發送出去了。就是單擊時間,假設在這個時間內沒有發送出去,就是雙擊事件。
轉載于:https://www.cnblogs.com/bhlsheji/p/5207701.html
總結
以上是生活随笔為你收集整理的Android应用开发经常使用知识的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: css隐藏文字
- 下一篇: Android优化——UI优化(二) 使