下面列出了android.view.View#clearFocus ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
ChatInputView chatInputView = mChatView.getChatInputView();
if (chatInputView.getMenuState() == View.VISIBLE) {
chatInputView.dismissMenuLayout();
}
mChatView.setMsgListHeight(true);
try {
View v = getCurrentFocus();
if (mImm != null && v != null) {
mImm.hideSoftInputFromWindow(v.getWindowToken(), 0);
mWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
view.clearFocus();
}
} catch (Exception e) {
e.printStackTrace();
}
break;
case MotionEvent.ACTION_UP:
view.performClick();
break;
}
return false;
}
/**
* 从一个view创建Bitmap:
* 注意点:绘制之前要清掉 View 的焦点,因为焦点可能会改变一个 View 的 UI 状态
* 来源:https://github.com/tyrantgit/ExplosionField
*
* @param view
* @return
*/
public static Bitmap createBitmapFromView(View view, float scale) {
if (view instanceof ImageView) {
Drawable drawable = ((ImageView) view).getDrawable();
if (drawable != null && drawable instanceof BitmapDrawable) {
return ((BitmapDrawable) drawable).getBitmap();
}
}
view.clearFocus();
Bitmap bitmap = createBitmapSafely((int) (view.getWidth() * scale),
(int) (view.getHeight() * scale), Bitmap.Config.ARGB_8888, 1);
if (bitmap != null) {
synchronized (sCanvas) {
Canvas canvas = sCanvas;
canvas.setBitmap(bitmap);
canvas.save();
canvas.drawColor(Color.WHITE); // 防止 View 上面有些区域空白导致最终 Bitmap 上有些区域变黑
canvas.scale(scale, scale);
view.draw(canvas);
canvas.restore();
canvas.setBitmap(null);
}
}
return bitmap;
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
View v = getCurrentFocus();
if (v instanceof EditText) {
Rect outRect = new Rect();
v.getGlobalVisibleRect(outRect);
if (!outRect.contains((int) event.getRawX(), (int) event.getRawY())) {
v.clearFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
}
}
return super.dispatchTouchEvent(event);
}
public static Bitmap createBitmapFromView(View view) {
if (view instanceof ImageView) {
Drawable drawable = ((ImageView) view).getDrawable();
if (drawable != null && drawable instanceof BitmapDrawable) {
return ((BitmapDrawable) drawable).getBitmap();
}
}
view.clearFocus();
Bitmap bitmap = createBitmapSafely(view.getWidth(),
view.getHeight(), Bitmap.Config.ARGB_8888, 1);
if (bitmap != null) {
synchronized (sCanvas) {
Canvas canvas = sCanvas;
canvas.setBitmap(bitmap);
view.draw(canvas);
canvas.setBitmap(null);
}
}
return bitmap;
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
View myView = getCurrentFocus();
if (myView instanceof EditText) {
Rect rect = new Rect();
myView.getGlobalVisibleRect(rect);
if (!rect.contains((int) event.getRawX(), (int) event.getRawY())) {
myView.clearFocus();
}
}
}
return super.dispatchTouchEvent(event);
}
@Override
public void recordKeyboardStatus(Window window) {
final View focusView = window.getCurrentFocus();
if (focusView == null) {
return;
}
if (isKeyboardShowing) {
saveFocusView(focusView);
} else {
focusView.clearFocus();
}
}
@Override
public void recordKeyboardStatus(Window window) {
final View focusView = window.getCurrentFocus();
if (focusView == null) {
return;
}
if (isKeyboardShowing) {
saveFocusView(focusView);
} else {
focusView.clearFocus();
}
}
/**
* Draw the view into a bitmap.
*/
private Bitmap getViewBitmap(View v) {
v.clearFocus();
v.setPressed(false);
boolean willNotCache = v.willNotCacheDrawing();
v.setWillNotCacheDrawing(false);
// Reset the drawing cache background color to fully transparent
// for the duration of this operation
int color = v.getDrawingCacheBackgroundColor();
v.setDrawingCacheBackgroundColor(0);
if (color != 0) {
v.destroyDrawingCache();
}
v.buildDrawingCache();
Bitmap cacheBitmap = v.getDrawingCache();
if (cacheBitmap == null) {
Log.e(TAG, "failed getViewBitmap(" + v + ")", new RuntimeException());
return null;
}
Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);
// Restore the view
v.destroyDrawingCache();
v.setWillNotCacheDrawing(willNotCache);
v.setDrawingCacheBackgroundColor(color);
return bitmap;
}
/**
* Hide the panel and the keyboard.
*
* @param panelLayout the layout of panel.
*/
public static void hidePanelAndKeyboard(final View panelLayout) {
final Activity activity = (Activity) panelLayout.getContext();
final View focusView = activity.getCurrentFocus();
if (focusView != null) {
KeyboardUtil.hideKeyboard(activity.getCurrentFocus());
focusView.clearFocus();
}else{
KeyboardUtil.hideKeyboard(panelLayout);
}
panelLayout.setVisibility(View.GONE);
}
/**
* 把一个View的对象转换成bitmap
*
* @param view View
* @return Bitmap
*/
public static Bitmap getBitmapFromView2(View view) {
if (view == null) {
return null;
}
view.clearFocus();
view.setPressed(false);
// 能画缓存就返回false
boolean willNotCache = view.willNotCacheDrawing();
view.setWillNotCacheDrawing(false);
int color = view.getDrawingCacheBackgroundColor();
view.setDrawingCacheBackgroundColor(0);
if (color != 0) {
view.destroyDrawingCache();
}
view.buildDrawingCache();
Bitmap cacheBitmap = view.getDrawingCache();
if (cacheBitmap == null) {
XPrintUtils.e( "failed getViewBitmap(" + view + ") -->"+
new RuntimeException());
return null;
}
Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);
// Restore the view
view.destroyDrawingCache();
view.setWillNotCacheDrawing(willNotCache);
view.setDrawingCacheBackgroundColor(color);
return bitmap;
}
/**
* 通过 View Cache 绘制为 Bitmap
* @param view {@link View}
* @return {@link Bitmap}
*/
public static Bitmap getBitmapFromViewCache(final View view) {
if (view == null) return null;
try {
// 清除视图焦点
view.clearFocus();
// 将视图设为不可点击
view.setPressed(false);
// 获取视图是否可以保存画图缓存
boolean willNotCache = view.willNotCacheDrawing();
view.setWillNotCacheDrawing(false);
// 获取绘制缓存位图的背景颜色
int color = view.getDrawingCacheBackgroundColor();
// 设置绘图背景颜色
view.setDrawingCacheBackgroundColor(0);
if (color != 0) { // 获取的背景不是黑色的则释放以前的绘图缓存
view.destroyDrawingCache(); // 释放绘图资源所使用的缓存
}
// 重新创建绘图缓存, 此时的背景色是黑色
view.buildDrawingCache();
// 获取绘图缓存, 注意这里得到的只是一个图像的引用
Bitmap cacheBitmap = view.getDrawingCache();
if (cacheBitmap == null) return null;
Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);
// 释放位图内存
view.destroyDrawingCache();
// 回滚以前的缓存设置、缓存颜色设置
view.setWillNotCacheDrawing(willNotCache);
view.setDrawingCacheBackgroundColor(color);
return bitmap;
} catch (Exception e) {
LogPrintUtils.eTag(TAG, e, "getBitmapFromViewCache");
}
return null;
}
/**
* @param view
* @return
* @deprecated use {@link #getCacheBitmap(View,Bitmap.Config)}
* <br>感觉这个不好 会清除一些状态</br>
* 仅仅为了得到的位图是RGB 565吧
* 但是 结果来看 大部分还是RGB8888 要和 View.mUse32BitDrawingCache相关
*/
public static Bitmap getCacheBitmap_ConifgByWindow(View view) {
// 将一个View转化成一张图片
view.clearFocus(); // 清除视图焦点
view.setPressed(false);// 将视图设为不可点击
boolean willNotCache = view.willNotCacheDrawing(); // 返回视图是否可以保存他的画图缓存
view.setWillNotCacheDrawing(false);
// Reset the drawing cache background color to fully transparent
// for the duration of this operation //将视图在此操作时置为透明
int color = view.getDrawingCacheBackgroundColor(); // 获得绘制缓存位图的背景颜色
view.setDrawingCacheBackgroundColor(0); // 设置绘图背景颜色
if (color != 0) { // 如果获得的背景不是黑色的则释放以前的绘图缓存
view.destroyDrawingCache(); // 释放绘图资源所使用的缓存
}
view.buildDrawingCache(); // 重新创建绘图缓存,此时的背景色是黑色
Bitmap cacheBitmap = view.getDrawingCache(); // 将绘图缓存得到的,注意这里得到的只是一个图像的引用
if (cacheBitmap == null)
return failLog(view);
Bitmap bitmap = Bitmap.createBitmap(cacheBitmap); // 将位图实例化
// Restore the view //恢复视图
view.destroyDrawingCache();// 释放位图内存
view.setWillNotCacheDrawing(willNotCache);// 返回以前缓存设置
view.setDrawingCacheBackgroundColor(color);// 返回以前的缓存颜色设置
return bitmap;
}
public void recordKeyboardStatus(Window window) {
View focusView = window.getCurrentFocus();
if (focusView != null) {
if (this.isKeyboardShowing) {
saveFocusView(focusView);
} else {
focusView.clearFocus();
}
}
}
/**
* 清除editText的焦点
*
* @param v 焦点所在View
* @param ids 输入框
*/
public void clearViewFocus(View v, int... ids) {
if (null != v && null != ids && ids.length > 0) {
for (int id : ids) {
if (v.getId() == id) {
v.clearFocus();
break;
}
}
}
}
/**
* 关闭软键盘
* @param context
*/
public static void closeSoftKeyboard(Context context) {
if (context == null || !(context instanceof Activity) || ((Activity) context).getCurrentFocus() == null) {
return;
}
try{
View view = ((Activity) context).getCurrentFocus();
InputMethodManager imm = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
view.clearFocus();
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
} catch (Exception e){
e.printStackTrace();
}
}
@Test
public void testClickEmptyFocusEditText() {
View editText = activity.findViewById(R.id.edittext_body);
editText.clearFocus();
assertThat(editText).isNotFocused();
activity.findViewById(R.id.empty).performClick();
assertThat(editText).isFocused();
}
private void hideSoftInput(View view) {
view.clearFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
@Override
public void cleaFocus() {
for (View view : views) {
view.clearFocus();
}
}
@Override
public void cleaFocus() {
for (View view : views) {
view.clearFocus();
}
}
private boolean arrowScrollImpl(int direction) {
if (getChildCount() <= 0) {
return false;
}
boolean needToRedraw;
View focused;
View selectedView = getSelectedView();
int selectedPos = this.mSelectedPosition;
int nextSelectedPosition = lookForSelectablePositionOnScreen(direction);
int amountToScroll = amountToScroll(direction, nextSelectedPosition);
ArrowScrollFocusResult focusResult = this.mItemsCanFocus ? arrowScrollFocused(direction) : null;
if (focusResult != null) {
nextSelectedPosition = focusResult.getSelectedPosition();
amountToScroll = focusResult.getAmountToScroll();
}
if (focusResult != null) {
needToRedraw = true;
} else {
needToRedraw = false;
}
if (nextSelectedPosition != -1) {
boolean z;
if (focusResult != null) {
z = true;
} else {
z = false;
}
handleNewSelectionChange(selectedView, direction, nextSelectedPosition, z);
setSelectedPositionInt(nextSelectedPosition);
setNextSelectedPositionInt(nextSelectedPosition);
selectedView = getSelectedView();
selectedPos = nextSelectedPosition;
if (this.mItemsCanFocus && focusResult == null) {
focused = getFocusedChild();
if (focused != null) {
focused.clearFocus();
}
}
needToRedraw = true;
checkSelectionChanged();
}
if (amountToScroll > 0) {
if (direction != 33) {
amountToScroll = -amountToScroll;
}
scrollListItemsBy(amountToScroll);
needToRedraw = true;
}
if (this.mItemsCanFocus && focusResult == null && selectedView != null && selectedView.hasFocus()) {
focused = selectedView.findFocus();
if (!isViewAncestorOf(focused, this) || distanceToView(focused) > 0) {
focused.clearFocus();
}
}
if (!(nextSelectedPosition != -1 || selectedView == null || isViewAncestorOf(selectedView, this))) {
selectedView = null;
hideSelector();
this.mResurrectToPosition = -1;
}
if (!needToRedraw) {
return false;
}
if (selectedView != null) {
positionSelector(selectedPos, selectedView);
this.mSelectedLeft = selectedView.getLeft();
}
if (!awakenScrollBars()) {
invalidate();
}
invokeOnItemScrollListener();
return true;
}