Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/203.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何将位图缩放到屏幕大小?_Android - Fatal编程技术网

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

Android 如何将位图缩放到屏幕大小?,android,Android,我想知道如何将位图缩放到屏幕高度和宽度 谁能告诉我怎么做 谢谢 Monali首先获取屏幕高度和宽度: :然后 看看这个: 尝试以下方法解码位图: 其中imagefilepath是图像的路径名,它将使用字符串转换为文件 File photos= new File(imageFilePath); 其中,photo是图像的文件名,现在您可以根据需要设置高度和宽度 public void main(){ Bitmap bitmap = decodeFile(photo); bitmap

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

谁能告诉我怎么做

谢谢
Monali

首先获取屏幕高度和宽度:
:然后
看看这个:

尝试以下方法解码位图:

其中imagefilepath是图像的路径名,它将使用字符串转换为文件

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

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;
}
public void main(){
位图位图=解码文件(照片);
位图=位图。createScaledBitmap(位图,150,150,真);
设置图像位图(位图);
} 
私有位图解码文件(文件f){
试一试{
//解码图像大小
BitmapFactory.Options o=新的BitmapFactory.Options();
o、 inJustDecodeBounds=true;
解码流(新的FileInputStream(f),null,o);
//找到正确的刻度值。它应该是2的幂。
所需的最终int_尺寸=70;
内部宽度=o.向外宽度,高度=o.向外高度;
int标度=1;
while(true){

如果(width_tmp/2我遇到了一个类似的挑战,我想将屏幕的宽度拉伸到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);

您可以使用
位图.createScaledBitmap()
,但请确保使用正确的转换公式:

final float scale = getContext().getResources().getDisplayMetrics().density;
int pixels = (int) (dps * scale + 0.5f);
Bitmap.createScaledBitmap()
中,宽度和高度的单位为“像素”,而您应始终按照所述设置与密度无关的像素(dp单位)大小

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

Bitmap b=decodeScaledFile(f);
最终浮动比例=mContext.getResources().getDisplayMetrics().density;
INTP=(int)(尺寸×刻度+0.5f);
位图b2=位图。createScaledBitmap(b,p,p,true);

您可以基于此代码创建缩放位图

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;
}
方法调用&将缩放图像设置为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"
    />


你有什么?一个带有ImageView或位图/可绘制和画布的布局?如果布局是画布,Cordova会自动缩放画布以适应不同的屏幕大小,还是我们必须自己手动缩放?这比简单的实现更好,占用的内存更少。代码中有一个小错误。应该是
scale*=2;
而不是scale++;我想我遇到了同样的问题,我想我必须使用LayoutParams?请帮助我解决这个问题:不要做所有这些工作来计算inSampleSize的比例,你不能只做
o2.inSampleSize=(int)(Math.min(width\u tmp,height\u tmp)/REQUIRED\u SIZE吗根据BitmapFactory.Options.inSampleSize上的文档,
解码器使用基于2次幂的最终值,任何其他值都将向下舍入到最接近的2次幂
。因此我认为这将为您完成大部分工作。这对于我了解如何仅使用一维进行缩放非常有用(缩放宽度或高度)并保持正确的比率。谢谢!很高兴这有帮助。请欣赏。如果以编程方式设置位图,这将不起作用。相反,您只需使用Imageview并将宽度设置为与父级匹配,将高度设置为包裹内容,请使用scaletype“fitXY”将ViewBounds和adjustViewbounds设置为true。您将获得相同的效果。@AmeyaB哪一个android版本可以使用它,因为这是我尝试的第一件事,而且从来没有像您描述的那样对我起过作用。@LuxuryMode
SIZE\u DP
是图像的大小,以密度无关的像素表示-请参阅使用createScaledBitmap实际上创建了一个全新的位图,因此您使用内存来存储2个位图?例如,如果第一个位图占用x字节,则按2*2=4倍缩放的位图是否会占用原始位图的2*2=4倍,因此您总共使用了大约4x+x=5x字节?