|
| 1 | +package com.jlj.pmd; |
| 2 | + |
| 3 | +import android.app.Activity; |
| 4 | +import android.app.Dialog; |
| 5 | +import android.content.Context; |
| 6 | +import android.content.res.Resources; |
| 7 | +import android.graphics.Bitmap; |
| 8 | +import android.graphics.Canvas; |
| 9 | +import android.graphics.Matrix; |
| 10 | +import android.graphics.PixelFormat; |
| 11 | +import android.graphics.Point; |
| 12 | +import android.graphics.drawable.BitmapDrawable; |
| 13 | +import android.graphics.drawable.Drawable; |
| 14 | +import android.os.IBinder; |
| 15 | +import android.support.v4.content.ContextCompat; |
| 16 | +import android.util.DisplayMetrics; |
| 17 | +import android.view.Display; |
| 18 | +import android.view.MotionEvent; |
| 19 | +import android.view.View; |
| 20 | +import android.view.WindowManager; |
| 21 | +import android.view.inputmethod.InputMethodManager; |
| 22 | +import android.widget.EditText; |
| 23 | +import android.widget.TextView; |
| 24 | + |
| 25 | + |
| 26 | +import static android.content.Context.INPUT_METHOD_SERVICE; |
| 27 | + |
| 28 | + |
| 29 | +public class DisplayUtil { |
| 30 | + // 状态栏高度 |
| 31 | + private static int mStatusHeight = -1; |
| 32 | + // 屏幕像素点 |
| 33 | + private static final Point screenSize = new Point(); |
| 34 | + |
| 35 | + public static float getDensity(Context context) { |
| 36 | + Resources resources = context.getResources(); |
| 37 | + DisplayMetrics dm = resources.getDisplayMetrics(); |
| 38 | + return dm.density; |
| 39 | + } |
| 40 | + |
| 41 | + public static int getDensityWdith(Context context) { |
| 42 | + Resources resources = context.getResources(); |
| 43 | + DisplayMetrics dm = resources.getDisplayMetrics(); |
| 44 | + return dm.widthPixels; |
| 45 | + } |
| 46 | + |
| 47 | + public static int getDensityHeight(Context context) { |
| 48 | + Resources resources = context.getResources(); |
| 49 | + DisplayMetrics dm = resources.getDisplayMetrics(); |
| 50 | + return dm.heightPixels; |
| 51 | + } |
| 52 | + |
| 53 | + // 1.代码中设置setXXSize的都是px单位,都需要把布局中的dp,sp转成px才能使用 |
| 54 | + |
| 55 | + /** |
| 56 | + * 根据手机分辨率从 px(像素) 单位 转成 dp |
| 57 | + */ |
| 58 | + public static int px2dip(Context context, float pxValue) { |
| 59 | + final float scale = context.getResources().getDisplayMetrics().density; |
| 60 | + return (int) (pxValue / scale + 0.5f); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * 根据手机分辨率从 dp 单位 转成 px(像素) |
| 65 | + */ |
| 66 | + public static int dip2px(Context context, float dpValue) { |
| 67 | + final float scale = context.getResources().getDisplayMetrics().density; |
| 68 | + return (int) (dpValue * scale + 0.5f); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * 将px值转换为sp值,保证文字大小不变 |
| 73 | + * |
| 74 | + * @param pxValue (DisplayMetrics类中属性scaledDensity) |
| 75 | + */ |
| 76 | + public static int px2sp(Context context, float pxValue) { |
| 77 | + final float fontScale = context.getResources().getDisplayMetrics().scaledDensity; |
| 78 | + return (int) (pxValue / fontScale + 0.5f); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * 将sp值转换为px值,保证文字大小不变 |
| 83 | + * |
| 84 | + * @param spValue (DisplayMetrics类中属性scaledDensity) |
| 85 | + */ |
| 86 | + public static int sp2px(Context context, float spValue) { |
| 87 | + final float fontScale = context.getResources().getDisplayMetrics().scaledDensity; |
| 88 | + return (int) (spValue * fontScale + 0.5f); |
| 89 | + } |
| 90 | + |
| 91 | + public static int getWidth(Context context, int liangbianjuli, int num, |
| 92 | + int picjuli) { |
| 93 | + // 屏幕宽度-两边的距离-图片中间的距离 除以列数 |
| 94 | + return (getDensityWdith(context) |
| 95 | + - DisplayUtil.dip2px(context, liangbianjuli) * 2 - dip2px( |
| 96 | + context, picjuli) * (num - 1)) |
| 97 | + / num; |
| 98 | + } |
| 99 | + |
| 100 | + public static int getWidth(Context context, int num, |
| 101 | + int jian) { |
| 102 | + // 屏幕宽度-两边的距离-图片中间的距离 除以列数 |
| 103 | + return (getDensityWdith(context) - DisplayUtil.dip2px(context, jian)) |
| 104 | + / num * 2; |
| 105 | + } |
| 106 | + |
| 107 | + public static int getWidth(Context context, int percent) { |
| 108 | + // 获取屏幕的高度 |
| 109 | + return Integer.parseInt((getDensityWdith(context) * percent) / 100 + ""); |
| 110 | + } |
| 111 | + |
| 112 | + public static int getHeight(Context context, int fenmu) { |
| 113 | + // 获取屏幕的高度 |
| 114 | + return (getDensityHeight(context) / fenmu); |
| 115 | + } |
| 116 | + |
| 117 | + |
| 118 | + public static void setDrawbleLeft(Context context, TextView view, int rsd, int demens) { |
| 119 | + Drawable drawable = ContextCompat.getDrawable(context, rsd); |
| 120 | + // / 这一步必须要做,否则不会显示. |
| 121 | + drawable.setBounds(0, 0, drawable.getMinimumWidth(), |
| 122 | + drawable.getMinimumHeight()); |
| 123 | + view.setCompoundDrawables(drawable, null, null, null); |
| 124 | + view.setCompoundDrawablePadding(dip2px(context, demens)); |
| 125 | + } |
| 126 | + public static void setDrawbleLeft(Context context, TextView view, Drawable drawable, int demens,int width) { |
| 127 | + drawable.setBounds(0, 0, width, |
| 128 | + width); |
| 129 | + view.setCompoundDrawables(drawable, null, null, null); |
| 130 | + view.setCompoundDrawablePadding(dip2px(context, demens)); |
| 131 | + } |
| 132 | + |
| 133 | + public static void setDrawbleLeftAndRight(Context context, TextView view, int rsd, int rrsd,int demens) { |
| 134 | + Drawable drawable = ContextCompat.getDrawable(context, rsd); |
| 135 | + Drawable rdrawable = ContextCompat.getDrawable(context, rrsd); |
| 136 | + // / 这一步必须要做,否则不会显示. |
| 137 | + drawable.setBounds(0, 0, drawable.getMinimumWidth(), |
| 138 | + drawable.getMinimumHeight()); |
| 139 | + rdrawable.setBounds(0, 0, rdrawable.getMinimumWidth(), |
| 140 | + rdrawable.getMinimumHeight()); |
| 141 | + view.setCompoundDrawables(drawable, null, rdrawable, null); |
| 142 | + view.setCompoundDrawablePadding(dip2px(context, demens)); |
| 143 | + } |
| 144 | + |
| 145 | + public static void setDrawbleRight(Context context, TextView view, int rsd, int demens) { |
| 146 | + Drawable drawable = context.getResources().getDrawable(rsd); |
| 147 | + // / 这一步必须要做,否则不会显示. |
| 148 | + drawable.setBounds(0, 0, drawable.getMinimumWidth(), |
| 149 | + drawable.getMinimumHeight()); |
| 150 | + view.setCompoundDrawables(null, null, drawable, null); |
| 151 | + view.setCompoundDrawablePadding(demens); |
| 152 | + } |
| 153 | + |
| 154 | + public static void setDrawbleTop(Context context, TextView view,Drawable drawable, int demens) { |
| 155 | + |
| 156 | + // / 这一步必须要做,否则不会显示. |
| 157 | + drawable.setBounds(0, 0, drawable.getMinimumWidth(), |
| 158 | + drawable.getMinimumHeight()); |
| 159 | + view.setCompoundDrawables(null, drawable, null, null); |
| 160 | + view.setCompoundDrawablePadding(dip2px(context, demens)); |
| 161 | + } |
| 162 | + |
| 163 | + public static Point getScreenSize(Activity context) { |
| 164 | + if (context == null) { |
| 165 | + return screenSize; |
| 166 | + } |
| 167 | + WindowManager wm = (WindowManager) context |
| 168 | + .getSystemService(Context.WINDOW_SERVICE); |
| 169 | + if (wm != null) { |
| 170 | + DisplayMetrics mDisplayMetrics = new DisplayMetrics(); |
| 171 | + Display diplay = wm.getDefaultDisplay(); |
| 172 | + if (diplay != null) { |
| 173 | + diplay.getMetrics(mDisplayMetrics); |
| 174 | + int W = mDisplayMetrics.widthPixels; |
| 175 | + int H = mDisplayMetrics.heightPixels; |
| 176 | + if (W * H > 0 && (W > screenSize.x || H > screenSize.y)) { |
| 177 | + screenSize.set(W, H); |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | + return screenSize; |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + * 处理点击非 EditText 区域时,自动关闭键盘 |
| 186 | + * |
| 187 | + * @param isAutoCloseKeyboard 是否自动关闭键盘 |
| 188 | + * @param currentFocusView 当前获取焦点的控件 |
| 189 | + * @param motionEvent 触摸事件 |
| 190 | + * @param dialogOrActivity Dialog 或 Activity |
| 191 | + */ |
| 192 | + public static void handleAutoCloseKeyboard(boolean isAutoCloseKeyboard, View currentFocusView, MotionEvent motionEvent, Object dialogOrActivity) { |
| 193 | + if (isAutoCloseKeyboard && motionEvent.getAction() == MotionEvent.ACTION_DOWN && (currentFocusView instanceof EditText) && dialogOrActivity != null) { |
| 194 | + int[] leftTop = {0, 0}; |
| 195 | + currentFocusView.getLocationInWindow(leftTop); |
| 196 | + int left = leftTop[0]; |
| 197 | + int top = leftTop[1]; |
| 198 | + int bottom = top + currentFocusView.getHeight(); |
| 199 | + int right = left + currentFocusView.getWidth(); |
| 200 | + if (!(motionEvent.getX() > left && motionEvent.getX() < right && motionEvent.getY() > top && motionEvent.getY() < bottom)) { |
| 201 | + if (dialogOrActivity instanceof Dialog) { |
| 202 | + closeKeyboard((Dialog) dialogOrActivity); |
| 203 | + } else if (dialogOrActivity instanceof Activity) { |
| 204 | + closeKeyboard((Activity) dialogOrActivity); |
| 205 | + } |
| 206 | + } |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + /** |
| 211 | + * 关闭dialog中打开的键盘 |
| 212 | + * |
| 213 | + */ |
| 214 | + public static void closeKeyboard(Dialog dialog) { |
| 215 | + View view = dialog.getWindow().peekDecorView(); |
| 216 | + if (view != null) { |
| 217 | + InputMethodManager inputMethodManager = (InputMethodManager) dialog.getContext().getSystemService(INPUT_METHOD_SERVICE); |
| 218 | + if (null != inputMethodManager) { |
| 219 | + inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0); |
| 220 | + } |
| 221 | + } |
| 222 | + } |
| 223 | + /** |
| 224 | + * 关闭activity中打开的键盘 |
| 225 | + */ |
| 226 | + public static void closeKeyboard(Activity activity) { |
| 227 | + View view = activity.getWindow().peekDecorView(); |
| 228 | + if (view != null) { |
| 229 | + InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(INPUT_METHOD_SERVICE); |
| 230 | + if (null != inputMethodManager) { |
| 231 | + inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0); |
| 232 | + } |
| 233 | + } |
| 234 | + } |
| 235 | + public static Bitmap drawableToBitmap(Drawable drawable) |
| 236 | + { |
| 237 | + int width = drawable.getIntrinsicWidth(); |
| 238 | + int height = drawable.getIntrinsicHeight(); |
| 239 | + Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565; |
| 240 | + Bitmap bitmap = Bitmap.createBitmap(width, height, config); |
| 241 | + Canvas canvas = new Canvas(bitmap); |
| 242 | + drawable.setBounds(0, 0, width, height); |
| 243 | + drawable.draw(canvas); |
| 244 | + return bitmap; |
| 245 | + } |
| 246 | + public static Drawable zoomDrawable(Drawable drawable, int w, int h) { |
| 247 | + int width = drawable.getIntrinsicWidth(); |
| 248 | + int height = drawable.getIntrinsicHeight(); |
| 249 | + Bitmap oldbmp = drawableToBitmap(drawable); |
| 250 | + Matrix matrix = new Matrix(); |
| 251 | + float scaleWidth = ((float) w / width); |
| 252 | + float scaleHeight = ((float) h / height); |
| 253 | + matrix.postScale(scaleWidth, scaleHeight); |
| 254 | + Bitmap newbmp = Bitmap.createBitmap(oldbmp, 0, 0, width, height, matrix, true); // 建立新的 bitmap ,其内容是对原 bitmap 的缩放后的图 |
| 255 | + return new BitmapDrawable(newbmp); |
| 256 | + } |
| 257 | + /** |
| 258 | + * 获取控件的高度或者宽度 isHeight=true则为测量该控件的高度,isHeight=false则为测量该控件的宽度 |
| 259 | + */ |
| 260 | + public static int getViewHeight(View view, boolean isHeight){ |
| 261 | + int result; |
| 262 | + if(view==null)return 0; |
| 263 | + if(isHeight){ |
| 264 | + int h = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED); |
| 265 | + view.measure(h,0); |
| 266 | + result =view.getMeasuredHeight(); |
| 267 | + }else{ |
| 268 | + int w = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED); |
| 269 | + view.measure(0,w); |
| 270 | + result =view.getMeasuredWidth(); |
| 271 | + } |
| 272 | + return result; |
| 273 | + } |
| 274 | + |
| 275 | + /** |
| 276 | + * 根据EditText所在坐标和用户点击的坐标相对比,来判断是否隐藏键盘,因为当用户点击EditText时则不能隐藏 |
| 277 | + * |
| 278 | + * @param v |
| 279 | + * @param event |
| 280 | + * @return |
| 281 | + */ |
| 282 | + public static boolean isShouldHideKeyboard(View v, MotionEvent event) { |
| 283 | + if (v != null && (v instanceof EditText)) { |
| 284 | + int[] l = {0, 0}; |
| 285 | + v.getLocationInWindow(l); |
| 286 | + int left = l[0], |
| 287 | + top = l[1], |
| 288 | + bottom = top + v.getHeight(), |
| 289 | + right = left + v.getWidth(); |
| 290 | + if (event.getX() > left && event.getX() < right |
| 291 | + && event.getY() > top && event.getY() < bottom) { |
| 292 | + // 点击EditText的事件,忽略它。 |
| 293 | + return false; |
| 294 | + } else { |
| 295 | + return true; |
| 296 | + } |
| 297 | + } |
| 298 | + // 如果焦点不是EditText则忽略,这个发生在视图刚绘制完,第一个焦点不在EditText上,和用户用轨迹球选择其他的焦点 |
| 299 | + return false; |
| 300 | + } |
| 301 | + |
| 302 | + /** |
| 303 | + * 获取InputMethodManager,隐藏软键盘 |
| 304 | + * @param token |
| 305 | + */ |
| 306 | + public static void hideKeyboard(IBinder token) { |
| 307 | + if (token != null) { |
| 308 | + InputMethodManager im = (InputMethodManager) AppContext.instance.getSystemService(INPUT_METHOD_SERVICE); |
| 309 | + im.hideSoftInputFromWindow(token, InputMethodManager.HIDE_NOT_ALWAYS); |
| 310 | + } |
| 311 | + } |
| 312 | + |
| 313 | + /** |
| 314 | + * * 显示键盘 |
| 315 | + * * |
| 316 | + * * @param et 输入焦点 |
| 317 | + * */ |
| 318 | + public static void showInput(final EditText et) { |
| 319 | + InputMethodManager imm = (InputMethodManager) AppContext.instance.getSystemService(INPUT_METHOD_SERVICE); |
| 320 | + imm.showSoftInput(et, InputMethodManager.SHOW_IMPLICIT); |
| 321 | + } |
| 322 | + |
| 323 | +} |
0 commit comments