Android Scales the picture proportionally to the screen width

Source: Internet
Author: User

ImageView has the ScaleType property to scale the picture so that it fills the width of the screen, but compression or clipping occurs.

The properties of the ImageView ScaleType are matrix (default), center, Centercrop, Centerinside, Fitcenter, Fitend, Fitstart, Fitxy, respectively.

Android:scaletype= "Center"

Keep the size of the original, displayed in the center of the ImageView. When the size of the original image is larger than the size of ImageView, the partial cropping is exceeded.

Android:scaletype= "Centercrop"

To fill the whole imageview for the purpose, the center of the original image to the center of the ImageView, and so on to enlarge the original image until the filling imageview (refers to the width and height of the ImageView to fill), the original version of more than the imageview part of the cut.

Android:scaletype= "Centerinside"

For the purpose of full display of the original image, the content of the picture is completely centered, and the size width (height) of the original is reduced by proportionally to or less than the width of the ImageView (high). If the size of the original is smaller than the size of ImageView, the size of the original is not processed and centered on the ImageView.

Android:scaletype= "Matrix"

Do not change the size of the original, from the upper left corner of the ImageView to draw the original image, the original more than the ImageView part for clipping.

Android:scaletype= "Fitcenter"

Enlarge or narrow the original image to the height of the imageview of the ImageView, centering on the display

Android:scaletype= "Fitend"

Enlarge (shrink) The original image to the height of the ImageView, shown in the lower part of the ImageView

Android:scaletype= "Fitstart"

Enlarge (shrink) The original image to the height of the ImageView, shown in the upper part of the ImageView

Android:scaletype= "Fitxy"

The original image is displayed in the view according to the specified size, stretched to show the picture, does not maintain the original proportions, fills the ImageView.


Implement the picture to fit the screen width: Get the screen size and dynamically set the height of the control. Implementation code:

Package com.penngo.image;import android.app.activity;import android.graphics.bitmap;import  android.graphics.BitmapFactory;import android.os.Bundle;import android.util.Log;import  Android.view.viewgroup;import android.widget.imageview;public class mainactivity extends  Activity {     @Override     protected void  OnCreate (bundle savedinstancestate)  {        super.oncreate ( Savedinstancestate);         setcontentview (R.layout.activity_main);         bitmap bitmap = bitmapfactory.decoderesource ( Getresources (),  r.drawable.b206_125);         int bwidth  = bitmap.getwidth ();        int bheight =  Bitmap.getheight ();    &Nbsp;    int width = screen.getscreenwidth (This);         LOG.E ("= = =", bwidth +  " "  + bHeight +  "  " + width);        imageview img5 =  ( ImageView) This.findviewbyid (R.ID.IMG5);         int height =  width * bHeight / bwidth;         Viewgroup.layoutparams para = img5.getlayoutparams ();         para.height = height;        img5.setlayoutparams (para );     }}
<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"      xmlns:tools= "Http://schemas.android.com/tools"     android:layout_width= "Match_parent"     android:layout_height= "Match_parent"     android:layout_margin= " 5dip "    tools:context=". Mainactivity ">    <imageview        android: Id= "@+id/img4"         android:src= "@drawable/b206_125"          android:scaletype= "Fitxy"          Android:layout_width= "match_parent"         android:layout_height= "wrap _content " />    <view        android:id = "@+id/view"         android:layout_margintop= "5DP" &NBsp;       android:layout_marginbottom= "5DP"          android:layout_below= "@+id/img4"         android: Layout_width= "Match_parent"         android:layout_height= "1dip"          android:background= "#c3c3c3"          android:visibility= "Visible"  />    <ImageView         android:id= "@+id/img5"         android: src= "@drawable/b206_125"         android:layout_below= "@+id/view"          android:layout_width= "Match_parent"          android:layout_height= "Wrap_content"  />    <View         android:id= "@+id/view1"         android:layout_margintop= "5DP"         android:layout_marginbottom= "5DP"          android:layout_below= "@+id/img5"          Android:layout_width= "Match_parent"         android:layout_height= "1dip "        android:background=" #c3c3c3 "         android:visibility= "Visible"  />    <ImageView         android:id= "@+id/img6"          android:src= "@drawable/b206_125"         android:scaletype= "CenterCrop"         android:layout_below= "@+id/view1"          android:layout_wIdth= "Match_parent"         android:layout_height= "Wrap_content"  / ></RelativeLayout>


Android Scales the picture proportionally to the screen width

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.