Android 如何正确显示位图?

Android 如何正确显示位图?,android,bitmap,Android,Bitmap,我正在处理两个问题,试图在我的Android应用程序中显示来自图库的照片。我想做的很简单:从图库中获取一张照片,并将其放在主活动中的ImageView(100dp*100dp)中 第一个问题是,在一些手机上,如索尼Xperia,当照片设置在ImageView上时,照片会旋转。为了解决这个问题,我在答案中找到了这段代码: public Bitmap decodeFile(String path) { int orientation; try { if (path

我正在处理两个问题,试图在我的Android应用程序中显示来自图库的照片。我想做的很简单:从图库中获取一张照片,并将其放在主活动中的ImageView(100dp*100dp)中

第一个问题是,在一些手机上,如索尼Xperia,当照片设置在ImageView上时,照片会旋转。为了解决这个问题,我在答案中找到了这段代码:

public Bitmap decodeFile(String path) 
{
    int orientation;

    try {
        if (path == null) {
            return null;
        }
        // decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        // Find the correct scale value. It should be the power of 2.
        final int REQUIRED_SIZE = 70;
        int width_tmp = o.outWidth, height_tmp = o.outHeight;
        int scale = 0;

        while (true) {
            if (width_tmp / 2 < REQUIRED_SIZE
                    || height_tmp / 2 < REQUIRED_SIZE)
                break;
            width_tmp /= 2;
            height_tmp /= 2;
            scale++;
        }
        // decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        Bitmap bm = BitmapFactory.decodeFile(path, o2);
        Bitmap bitmap = bm;

        ExifInterface exif = new ExifInterface(path);

        orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);

        Log.e("ExifInteface .........", "rotation =" + orientation);

        // exif.setAttribute(ExifInterface.ORIENTATION_ROTATE_90, 90);

        Log.e("orientation", "" + orientation);
        Matrix m = new Matrix();

        if ((orientation == ExifInterface.ORIENTATION_ROTATE_180)) {
            m.postRotate(180);
            // m.postScale((float) bm.getWidth(), (float) bm.getHeight());
            // if(m.preRotate(90)){
            Log.e("in orientation", "" + orientation);
            bitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),
                    bm.getHeight(), m, true);
            return bitmap;
        } else if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
            m.postRotate(90);
            Log.e("in orientation", "" + orientation);
            bitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),
                    bm.getHeight(), m, true);
            return bitmap;
        } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
            m.postRotate(270);
            Log.e("in orientation", "" + orientation);
            bitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),
                    bm.getHeight(), m, true);
            return bitmap;
        }
        return bitmap;
    } catch (Exception e) {
        return null;
    }
}
公共位图解码文件(字符串路径)
{
智力定向;
试一试{
if(路径==null){
返回null;
}
//解码图像大小
BitmapFactory.Options o=新的BitmapFactory.Options();
o、 inJustDecodeBounds=true;
//找到正确的刻度值。它应该是2的幂。
所需的最终int_尺寸=70;
内部宽度=o.向外宽度,高度=o.向外高度;
整数比例=0;
while(true){
如果(宽度\u tmp/2<要求的\u尺寸
||高度(tmp/2<所需尺寸)
打破
宽度_tmp/=2;
高度_tmp/=2;
scale++;
}
//用inSampleSize解码
BitmapFactory.Options o2=新的BitmapFactory.Options();
o2.inSampleSize=刻度;
位图bm=BitmapFactory.decodeFile(路径,o2);
位图=bm;
ExifInterface exif=新的ExifInterface(路径);
方向=exif.getAttributeInt(ExifInterface.TAG_方向,1);
Log.e(“ExifInteface…………”、“rotation=“+方向”);
//setAttribute(ExifInterface.ORIENTATION\u ROTATE\u 90,90);
Log.e(“方向”,“方向+方向”);
矩阵m=新矩阵();
如果((方向==ExifInterface.orientation\u ROTATE\u 180)){
m、 旋转后(180);
//m.postScale((float)bm.getWidth(),(float)bm.getHeight());
//如果(m.预旋转(90)){
Log.e(“方向内”,“方向+方向”);
bitmap=bitmap.createBitmap(bm,0,0,bm.getWidth(),
bm.getHeight(),m,true);
返回位图;
}else if(方向==ExifInterface.orientation\u ROTATE\u 90){
m、 旋转后(90);
Log.e(“方向内”,“方向+方向”);
bitmap=bitmap.createBitmap(bm,0,0,bm.getWidth(),
bm.getHeight(),m,true);
返回位图;
}else if(方向==ExifInterface.orientation\u ROTATE\u 270){
m、 旋转后(270);
Log.e(“方向内”,“方向+方向”);
bitmap=bitmap.createBitmap(bm,0,0,bm.getWidth(),
bm.getHeight(),m,true);
返回位图;
}
返回位图;
}捕获(例外e){
返回null;
}
}
这很好,但我也希望图像是正方形,现在不是这样

为此,在对位图使用第一种方法后,我还调用了该方法:

public static Bitmap cropToSquare(Bitmap bitmap)
{
    int width  = bitmap.getWidth();
    int height = bitmap.getHeight();
    int newWidth = (height > width) ? width : height;
    int newHeight = (height > width)? height - ( height - width) : height;
    int crop = (width - height) / 2;
    crop = (crop < 0)? 0: crop;
    Bitmap cropImg = Bitmap.createBitmap(bitmap, crop, 0, newWidth, newHeight);

    return cropImg;
}
公共静态位图cropToSquare(位图)
{
int width=bitmap.getWidth();
int height=bitmap.getHeight();
int newWidth=(高度>宽度)?宽度:高度;
int newHeight=(高度>宽度)?高度-(高度-宽度):高度;
整数裁剪=(宽度-高度)/2;
作物=(作物<0)?0:作物;
位图cropImg=Bitmap.createBitmap(位图、裁剪、0、新宽度、新高度);
返回cropImg;
}
它确实会将位图变成正方形,但问题是它会剪切照片,而不是重新缩放照片。(基本上,图像的一半会丢失)

我很确定我想做的很简单,我怎么做呢?

而不是使用

Bitmap.createBitmap(bitmap, crop, 0, newWidth, newHeight);
使用下面这行

Bitmap.createScaledBitmap(bitmap, newWidth, newHeight, false);

不管怎样,公文是最好的老师,检查一下