2
4

2 に答える 2

6

これで試してください

public static Bitmap decodeFile(File f,int WIDTH,int HIGHT){
        try {
            //Decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(new FileInputStream(f),null,o);

            //The new size we want to scale to
            final int REQUIRED_WIDTH=WIDTH;
            final int REQUIRED_HIGHT=HIGHT;
            //Find the correct scale value. It should be the power of 2.
            int scale=1;
            while(o.outWidth/scale/2>=REQUIRED_WIDTH && o.outHeight/scale/2>=REQUIRED_HIGHT)
                scale*=2;

            //Decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize=scale;
            return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
        }
            catch (FileNotFoundException e) {}
        return null;
    }

これは、渡す幅と高さに従ってビットマップをスケーリングします

于 2012-11-05T06:20:57.057 に答える
2

このクラスを使用して、画像ビューに適用する前にビットマップを必要なサイズに縮小します。

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;


class BitmapLoader
{
    public static int getScale(int originalWidth,int originalHeight,
    final int requiredWidth,final int requiredHeight)
    {
       //a scale of 1 means the original dimensions 
       //of the image are maintained
      int scale=1;

//calculate scale only if the height or width of 
//the image exceeds the required value. 
if((originalWidth>requiredWidth) || (originalHeight>requiredHeight)) 
{
    //calculate scale with respect to
    //the smaller dimension
    if(originalWidth<originalHeight)
        scale=Math.round((float)originalWidth/requiredWidth);
    else
      scale=Math.round((float)originalHeight/requiredHeight);

}

return scale;
}

public static BitmapFactory.Options getOptions(String filePath,
    int requiredWidth,int requiredHeight)
{

BitmapFactory.Options options=new BitmapFactory.Options();
//setting inJustDecodeBounds to true
//ensures that we are able to measure
//the dimensions of the image,without
//actually allocating it memory
options.inJustDecodeBounds=true;

//decode the file for measurement
BitmapFactory.decodeFile(filePath,options);

//obtain the inSampleSize for loading a 
//scaled down version of the image.
//options.outWidth and options.outHeight 
//are the measured dimensions of the 
//original image
options.inSampleSize=getScale(options.outWidth,
        options.outHeight, requiredWidth, requiredHeight);

//set inJustDecodeBounds to false again
//so that we can now actually allocate the
//bitmap some memory
options.inJustDecodeBounds=false;

return options;

}



public static Bitmap loadBitmap(String filePath,
    int requiredWidth,int requiredHeight){


BitmapFactory.Options options= getOptions(filePath,
        requiredWidth, requiredHeight);

return BitmapFactory.decodeFile(filePath,options);
}
}

次に、アクティビティから呼び出します

Bitmap reqBitmap = loadBitmap(String filePath,int requiredWidth,int requiredHeight) 

SD カードから取得したビットマップのファイルパスを提供し、requiredWidth と requiredHeight をビットマップをスケーリングする寸法に設定するこのクラスのメソッド。ここで reqBitmap を使用します。

于 2012-11-05T06:25:09.617 に答える