android .9 图片的全能适配方法
16lz
2021-12-21
public static Drawable getScaleNinePatchDrawable(int resId) { Activity mContext = WaWaSystem.getActivity(); int standardBmpHeight = 413; BitmapFactory.Options optDecodeBounds = new BitmapFactory.Options(); optDecodeBounds.inJustDecodeBounds = true; InputStream isDecodeBounds = mContext.getResources().openRawResource( resId); BitmapFactory.decodeStream(isDecodeBounds, null, optDecodeBounds); System.out.println("原始图片高度:" + optDecodeBounds.outHeight + ",原始图片宽度:" + optDecodeBounds.outWidth); Rect padding = new Rect(); BitmapFactory.Options opt = new BitmapFactory.Options(); opt.inPreferredConfig = Bitmap.Config.ARGB_8888; opt.inPurgeable = true; opt.inInputShareable = true; opt.inTargetDensity = mContext.getResources().getDisplayMetrics().densityDpi; float heightScalePX = ((Activity) mContext).getWindowManager() .getDefaultDisplay().getHeight()/480f; opt.inDensity = (int) (opt.inTargetDensity / (heightScalePX * ((float) standardBmpHeight / optDecodeBounds.outHeight))); InputStream is = mContext.getResources().openRawResource(resId); Bitmap newBmp = BitmapFactory.decodeStream(is, padding, opt); newBmp.setDensity(mContext.getResources().getDisplayMetrics().densityDpi); if (newBmp.getNinePatchChunk() == null || padding == null) { System.out.println("BitmapDrawable"); return new BitmapDrawable(mContext.getResources(), newBmp); } else { System.out.println("NinePatchDrawable"); return new NinePatchDrawable(mContext.getResources(), newBmp, newBmp.getNinePatchChunk(), padding, String.valueOf(resId)); } }
更多相关文章
- android保存预览图片
- android 拖拽图片
- QQ侧滑删除分享(工具类)
- android 布局权重问题(最近布局经常坑爹)
- Android之如何获取视频或者图片的缩略图
- android 获取WebView的网页高度
- Android(安卓)3D图片切换
- android 多点触摸 实现图片缩放 Image Zoom
- android 根据TextView宽度 自动缩小字体大小 以适应其宽度