【问题标题】:Bitmap.createScaledBitmap won't scale to screen sizeBitmap.createScaledBitmap 不会缩放到屏幕大小
【发布时间】:2014-08-07 15:42:58
【问题描述】:

在使用 createScaledBitmap 时遇到了一些问题。它不适合我的屏幕尺寸,左右还有空间。

为什么会有这段代码

    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int width = size.x;
    int height = size.y;

    Resources res = getResources();

    Bitmap source = BitmapFactory.decodeResource(res, R.drawable.red);
    Bitmap overlay = Bitmap.createScaledBitmap(source, width, height, false);

这个屏幕的结果:

有什么想法吗?提前致谢!

【问题讨论】:

    标签: android bitmap


    【解决方案1】:

    您的位图实际上比您的ImageView 的高度更大,因为它是屏幕大小而不是视图大小,这意味着正在强制执行ImageView 缩放类型并缩小您的位图。在 XML 或代码中将视图上的比例类型设置为 Matrix。它不会执行任何缩放,因为 Matrix 默认设置为单位矩阵。

    【讨论】:

    • 我们赢了! fitXY 也适合我。 fitXY 和矩阵之间的主要区别是什么?非常感谢!
    • "matrix" 在这种情况下不会应用任何缩放。 “fitxy”将缩放它以适应视图。见developer.android.com/reference/android/widget/…
    【解决方案2】:

    您可以在任何活动中使用以下代码来获取屏幕尺寸:

        public static int displayWidth,displayHeight;
    
    
    
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        displayWidth = dm.widthPixels;
        displayHeight = dm.heightPixels;
    

    并将 displayWidth 和 height 变量传递给

    Bitmap.createScaledBitmap

    方法

    【讨论】:

    • 谢谢,但它会导致相同的行为。两种获取屏幕尺寸的方法都会导致高度和宽度为 1920 x 1080,并且在这两种情况下仍然是侧面的白条:(
    【解决方案3】:

    试试这个解码位图:

    其中 imagefilepath 是图片的路径名,它会在 String 中被使用转换为 File

    File photos= new File(imageFilePath); 其中 photo 是图像的文件名,现在您可以根据需要设置高度和宽度。

    Bitmap bitmap = decodeFile(photo);
                bitmap = Bitmap.createScaledBitmap(bitmap,150, 150, true);
                imageView.setImageBitmap(bitmap);
    
        private Bitmap decodeFile(File f){
        try {
            //decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(new FileInputStream(f),null,o);              
            //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=1;
            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;
            return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
        } catch (FileNotFoundException e) {}
        return null;
    }
    

    【讨论】:

    • 我在获取图像的文件路径时遇到了问题,即使我将其放在根文件夹中也是如此。但是,我检查了 Bitmapfactory.Options,这不是缩放以保持正确的纵横比吗?
    【解决方案4】:

    我不确定,但可能是因为您没有考虑操作栏的高度

    试试这个代码

        //this for getting actionbar height
        TypedValue tv = new TypedValue();
        int actionBarHeight = 0;
        if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
        {
            actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, getResources().getDisplayMetrics());
        }
        ////////
        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        int width = size.x;
        //
        int height = size.y - actionBarHeight;
    
        Resources res = getResources();
    
        Bitmap source = BitmapFactory.decodeResource(res, R.drawable.red);
        Bitmap overlay = Bitmap.createScaledBitmap(source, width, height, false);
    

    【讨论】:

    • 嗨。我认为可能是这种情况,所以我只是将主题切换为没有标题栏。不过还是一样的结果。我更新了问题中的图像。不过谢谢:)
    • 然后尝试隐藏标题link
    • 哦,标题栏不见了(见图)。我所说的“相同结果”是位图没有正确缩放宽度
    猜你喜欢
    • 1970-01-01
    • 2023-03-22
    • 2016-07-16
    • 2011-09-18
    • 1970-01-01
    • 1970-01-01
    • 2018-12-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多