Android 从res\drawable hdpi文件夹缩放和加载图像
在我的Android应用程序中,为了避免OOM(位图内存不足错误),我正在缩放并从assets文件夹加载图像。我的一些图像位于res\drawable hdpi文件夹中。是否有任何我可以缩放和加载它,就像我对资产文件夹中的图像所做的那样Android 从res\drawable hdpi文件夹缩放和加载图像,android,Android,在我的Android应用程序中,为了避免OOM(位图内存不足错误),我正在缩放并从assets文件夹加载图像。我的一些图像位于res\drawable hdpi文件夹中。是否有任何我可以缩放和加载它,就像我对资产文件夹中的图像所做的那样 @SuppressLint("NewApi") public Drawable getAssetImage(String filename) throws IOException { int dWidth,dHeight;
@SuppressLint("NewApi")
public Drawable getAssetImage(String filename) throws IOException {
int dWidth,dHeight;
Display display = getWindowManager().getDefaultDisplay();
if ( Integer.valueOf(android.os.Build.VERSION.SDK_INT) < 13 ) {
dWidth = display.getWidth();
dHeight = display.getHeight();
} else {
Point size = new Point();
display.getSize(size);
dWidth = size.x;
dHeight = size.y;
}
AssetManager assets = getApplicationContext().getResources().getAssets();
InputStream buffer = null;
try {
buffer = new BufferedInputStream((assets.open(filename + ".png")));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPurgeable = true;
if (tabletSize) {
} else {
int tempSampleSize = calculateInSampleSize(options, (int)dWidth, (int)dHeight);
Log.i("ClassicalMemoryGame", "dWidth " + dWidth );
Log.i("ClassicalMemoryGame", "dHeight " + dHeight );
Log.i("ClassicalMemoryGame", "sample size - " + tempSampleSize );
if (tempSampleSize > 1) {
options.inSampleSize = tempSampleSize;
}
}
options.inJustDecodeBounds = false;
Bitmap temp = BitmapFactory.decodeStream(buffer, null, options);
Bitmap finalImage = Bitmap.createScaledBitmap(temp, (int) dWidth, (int) dHeight, true);
Drawable d = new BitmapDrawable(getResources(),finalImage);
return d;
}
@SuppressLint(“NewApi”)
公共可绘制getAssetImage(字符串文件名)引发IOException{
整数宽,整数高;
Display Display=getWindowManager().getDefaultDisplay();
if(Integer.valueOf(android.os.Build.VERSION.SDK_INT)<13){
dWidth=display.getWidth();
dhheight=display.getHeight();
}否则{
点大小=新点();
display.getSize(size);
dWidth=大小x;
dHeight=尺寸y;
}
AssetManager资产=getApplicationContext().getResources().getAssets();
InputStream缓冲区=空;
试一试{
buffer=新的BufferedInputStream((assets.open(filename+“.png”));
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
BitmapFactory.Options=new-BitmapFactory.Options();
options.inpurgable=true;
如果(表格大小){
}否则{
int tempSampleSize=calculateInSampleSize(选项,(int)dWidth,(int)dHeight);
Log.i(“ClassicalMemoryGame”、“dWidth”+dWidth);
Log.i(“ClassicalMemoryGame”,“dHeight”+dHeight);
Log.i(“ClassicalMemoryGame”,“样本大小-”+tempSampleSize);
如果(tempSampleSize>1){
options.inSampleSize=tempSampleSize;
}
}
options.inJustDecodeBounds=false;
位图温度=BitmapFactory.decodeStream(缓冲区、null、选项);
位图finalImage=Bitmap.createScaledBitmap(temp,(int)dWidth,(int)dHeight,true);
Drawable d=新的位图Drawable(getResources(),finalImage);
返回d;
}
您可以使用以下方法执行此操作:
public static Bitmap decodeScaledSampleBitmap(Resources res,
int resourceId,int width, int height)
{
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resourceId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options,
width, height);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
Bitmap b = Bitmap.createScaledBitmap(
BitmapFactory.decodeResource(res, resourceId, options),
width, height, true);
return b;
}