首页 > 代码库 > Android应用开发常用知识
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应用开发常用知识