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 .9 图片的全能适配方法
最新推荐文章于 2024-04-10 08:18:23 发布