如何将位图缩放到屏幕大小?

时间:2011-06-20 11:20:02

标签: android

我想知道如何将位图缩放到屏幕高度和宽度?

任何人都可以告诉我该怎么做。

由于 Monali

5 个答案:

答案 0 :(得分:28)

尝试此解码位图:

其中imagefilepath是图像的路径名,它将在String中隐藏,使用

转换为File
File photos= new File(imageFilePath);

照片图片的文件名,现在您可以根据自己的要求设置高度和宽度。

public void main(){
    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;
}

答案 1 :(得分:15)

我有一个类似的挑战,我想将宽度拉伸到100%的屏幕,但保持宽度/高度比完整。所以我这样做了..

创建一个扩展ImageView的ScalableImageView类:

public class ScalableImageView extends ImageView {
    public boolean isMeasured = true; 

    public ScalableImageView(Context context) {
        super(context);
    }

    public ScalableImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ScalableImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        try
        {
            Drawable drawable = getDrawable();

            if (drawable == null)
            {
                setMeasuredDimension(0, 0);
            }
            else
            {
                int width = MeasureSpec.getSize(widthMeasureSpec);
                int height = width * drawable.getIntrinsicHeight() / drawable.getIntrinsicWidth();
                setMeasuredDimension(width, height);
            }
        }
        catch (Exception e)
        {
            isMeasured = false;
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }
}

在布局XML文件中,我有一个占位符,用于定义如下图像:

<ScalableImageView android:id="@+id/image1" 
    android:layout_centerHorizontal="true"
    android:src="@drawable/cam_image_placeholder" 
    android:scaleType="fitCenter" 
    android:layout_gravity="center_horizontal" 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" 
    android:adjustViewBounds="true"
    android:visibility="invisible" 
    android:layout_marginBottom="6px">
</ScalableImageView>

然后像这样加载/设置:

ScalableImageView imgView = null;
imgView = (ScalableImageView)findViewById(imgViewResource);
imgView.setImageDrawable(myDrawable);
imgView.setVisibility(ScalableImageView.VISIBLE);

答案 2 :(得分:8)

您可以使用Bitmap.createScaledBitmap(),但请确保在使用时使用正确的转化公式:

final float scale = getContext().getResources().getDisplayMetrics().density;
int pixels = (int) (dps * scale + 0.5f);

Bitmap.createScaledBitmap()中,宽度和高度的单位是“像素”,而您应始终按照here所述的与密度无关的像素(dp单位)设置尺寸。

因此,如果您想要显示您的图像保持密度独立性 - 即您的图像将在任何设备上占据相同的屏幕部分 - 您需要使用类似于以下的代码:

Bitmap b = decodeScaledFile(f);

final float scale = mContext.getResources().getDisplayMetrics().density;
int p = (int) (SIZE_DP * scale + 0.5f);

Bitmap b2 = Bitmap.createScaledBitmap(b, p, p, true);

答案 3 :(得分:1)

首先获得屏幕高度和宽度:
Android: How to get screen dimensions 1Get screen dimensions in pixels然后
看看这个: Resizing a Bitmap

答案 4 :(得分:0)

您可以根据此代码创建scaledBitmap。

private Bitmap getScaledBitMapBaseOnScreenSize(Bitmap bitmapOriginal){

    Bitmap scaledBitmap=null;
    try {
        DisplayMetrics metrics = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);


        int width = bitmapOriginal.getWidth();
        int height = bitmapOriginal.getHeight();

        float scaleWidth = metrics.scaledDensity;
        float scaleHeight = metrics.scaledDensity;

        // create a matrix for the manipulation
        Matrix matrix = new Matrix();
        // resize the bit map
        matrix.postScale(scaleWidth, scaleHeight);

        // recreate the new Bitmap
        scaledBitmap = Bitmap.createBitmap(bitmapOriginal, 0, 0, width, height, matrix, true);
    }
    catch (Exception e){
        e.printStackTrace();
    }
    return scaledBitmap;
}

方法调用&amp;将缩放图像设置为imageview

 Bitmap scaleBitmap=getScaledBitMapBaseOnScreenSize(originalBitmapImage);
    if(scaleBitmap!=null) {
    imageView.setImageBitmap(scaleBitmap);
    }

我对imageview的xml代码,

 <ImageView
    android:id="@+id/image_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:scaleType="center"
    />