【问题标题】:Enlarging pictures programmatically in Android在 Android 中以编程方式放大图片
【发布时间】:2012-04-06 13:08:07
【问题描述】:

我能够从我的手机中检索图片并将它们存储在一个数组中。之后,我将它们显示在屏幕上。但它们都有不同的形状和大小。我想以相同的大小和形状显示它们。有什么想法吗?

photoPaths = new ArrayList<String>();   
     getAllPhotos(Environment.getExternalStorageDirectory(), photoPaths);
     images = new Bitmap[photoPaths.size()];


         apa = (AnimationPhotoView)findViewById(R.id.animation_view);
        for(int i=0;i<photoPaths.size();i++)
        {
            File imgFile = new  File(photoPaths.get(0));

            if(imgFile.exists())
            {

                images[0] = decodeFile(imgFile);}

【问题讨论】:

    标签: android bitmap android-imageview android-image


    【解决方案1】:
        Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),R.drawable.tedd);
    
        int width = bitmapOrg.getWidth();
    
        int height = bitmapOrg.getHeight();
    
    
        int newWidth = 200;
    
        int newHeight  = 200;
    
        // calculate the scale - in this case = 0.4f
    
         float scaleWidth = ((float) newWidth) / width;
    
         float scaleHeight = ((float) newHeight) / height;
    
         Matrix matrix = new Matrix();
    
         matrix.postScale(scaleWidth, scaleHeight);
         matrix.postRotate(x);
         // this will create image with new size
         Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,width, height, matrix, true);
    
         iv.setScaleType(ScaleType.CENTER);
         iv.setImageBitmap(resizedBitmap);
    

    【讨论】:

    • 完美运行!!我需要等待才能接受。谢谢 Mac
    • 其实我在使用这个时遇到异常:IllegalArgumentException: bitmap size exceeds 32bits,请查看我的答案。
    • 使用this 这样你就不会得到Bitmap size exceeds
    【解决方案2】:

    我正在使用:

    Bitmap bitmap = //Your source
    
    int newWidth = //compute new width
    int newHeight = //compute new height
    
    bitmap = Bitmap.createScaledBitmap(bitmap, scaleWidth, scaleHeight, true);
    

    最后一个booleanfilter,让你的画面更流畅。

    上面 MAC 提出的解决方案给了我一个IllegalArgumentException: bitmap size exceeds 32bits

    这只是缩放位图而不是旋转它。

    【讨论】:

      【解决方案3】:

      如果您只想显示 jpg 图像或 android“已知”的图像格式,您可以简单地使用 MediaStore.Images 并检索缩略图,这样更快并且需要更少的内存,并且图像应该已经全部相同的格式(宽度+高度)。

      http://developer.android.com/reference/android/provider/MediaStore.Images.html

      【讨论】:

        【解决方案4】:

        我想你也可以试试这个。

        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=200;
                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*=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;
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2010-11-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多