Scaling ImageView to device width

前端 未结 9 922
清酒与你
清酒与你 2020-12-05 00:54

In my Android App I have a Activity which show images which have following size 244 x 330. I want to show those images in full device width.

My layout f

9条回答
  •  囚心锁ツ
    2020-12-05 01:34

    I tried really every ScaleType in my ImageView with fill_parent and wrap_content but no of them worked. I also tried everything what I found on Google but nothing worked for me either so came up with something on my own.

    It was clear that the ImageView is not scaling my image like I wanted to be scaled so I had to scale it on my own. After scaling the bitmap I would set the new Bitmap as the image source to the ImageView. This works pretty good and looks very good on the G1 and on the Motorola Milestone 2.

    And here is all pieces of my code

    Layout:

    
    
        
    
                
    
        
    
    
    

    Activity

    public class ScalingImages extends Activity {
    
        private ImageView imageView;
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.test_margin);
    
            this.imageView = (ImageView)findViewById(R.id.news_image);
    
            // The image is coming from resource folder but it could also 
            // load from the internet or whatever
            Drawable drawable = getResources().getDrawable(R.drawable.img);
            Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
    
            // Get scaling factor to fit the max possible width of the ImageView
            float scalingFactor = this.getBitmapScalingFactor(bitmap);
    
            // Create a new bitmap with the scaling factor
            Bitmap newBitmap = Util.ScaleBitmap(bitmap, scalingFactor);
    
            // Set the bitmap as the ImageView source
            this.imageView.setImageBitmap(newBitmap);
        }
    
        private float getBitmapScalingFactor(Bitmap bm) {
            // Get display width from device
            int displayWidth = getWindowManager().getDefaultDisplay().getWidth();
    
            // Get margin to use it for calculating to max width of the ImageView
            LinearLayout.LayoutParams layoutParams = 
                (LinearLayout.LayoutParams)this.imageView.getLayoutParams();
            int leftMargin = layoutParams.leftMargin;
            int rightMargin = layoutParams.rightMargin;
    
            // Calculate the max width of the imageView
            int imageViewWidth = displayWidth - (leftMargin + rightMargin);
    
            // Calculate scaling factor and return it
            return ( (float) imageViewWidth / (float) bm.getWidth() );
        }
    }
    

    Util class

    public class Util {
    
        public static Bitmap ScaleBitmap(Bitmap bm, float scalingFactor) {
            int scaleHeight = (int) (bm.getHeight() * scalingFactor);
            int scaleWidth = (int) (bm.getWidth() * scalingFactor);
    
            return Bitmap.createScaledBitmap(bm, scaleWidth, scaleHeight, true);
        }
    
    }
    

    If there is an better or more accurate way to accomplish the same scaling please let me know because I can't believe that such a trivial thing is so hard to accomplish.

    I'm really hoping to see a better way to do this.

    Thank you for reading.

提交回复
热议问题