ImageView ScaleType Properties for Android Learning notes

Source: Internet
Author: User

We know that ImageView has a property called ScaleType, which has eight common values, each of which is: Matrix,fitxy. Fitstart,fitcenter. Fitend,center,centercrop. Centerinside.

Let's take a look at these eight kinds of values to see what each means.

I use two pictures to do the demo, the resolution of the two pictures of a small, the picture is called big and small. The original image is as follows:

Big

Small

Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center "/>

OK, the preparation is complete.

Matrix

The matrix indicates that the original artwork was drawn from the upper left corner of the ImageView. Assuming the original image is larger than ImageView, then the extra part is trimmed off, assuming the original is less than ImageView, then do not do whatever processing the original. For example, I have two pictures of different sizes. displayed separately on 96DPX96DP's ImageView, there will be different effects, such as the following code:

    <imageview        android:layout_width= "96DP"        android:layout_height= "96DP"        android:adjustviewbounds= " False "        android:background=" #ffaa77 "        android:scaletype=" Matrix "        android:src=" @drawable/big "/>    <imageview        android:layout_width= "96DP"        android:layout_height= "96DP"        android: Adjustviewbounds= "false"        android:background= "#aa99cc"        android:scaletype= "Matrix"        android:src= "@ Drawable/small "/>

Display effects such as the following:

Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center "/>

As we can see, the big picture is bigger, so there is only the upper left corner to show it. The rest was cut off. The small picture is smaller because of the resolution. So the full display is in the upper left corner of the ImageView.

Fitxy

The goal of Fitxy is to fill the entire imageview. In order to complete this goal, it needs to do some zooming on the picture, in the process of zooming. It does not scale according to the proportions of the original image. For example, with the next chestnut:

    <imageview        android:layout_width= "96DP"        android:layout_height= "96DP"        android:background= "#ffaa77 "        android:scaletype=" Fitxy "        android:src=" @drawable/big "/>    <imageview        android:layout_ Width= "96DP"        android:layout_height= "128DP"        android:background= "#aa99cc"        android:scaletype= "Fitxy"        android:src= "@drawable/small"/>

Display effects such as the following:

Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center "/>

Both pictures were shown, but there was a certain degree of distortion. Because in this case the system only considers filling the imageview, not scaling according to the original proportions.

Fitstart

To scale the picture proportionally to the width or height of the view (whichever is wider and higher), and then to the top or left display (in relation to the previous zoom to width or height), let's look at the following example:

    <imageview        android:layout_width= "48DP"        android:layout_height= "256DP"        android:background= "# Ffaa77 "        android:scaletype=" Fitstart "        android:src=" @drawable/big "/>    <imageview        android: Layout_width= "256DP"        android:layout_height= "36DP"        android:background= "#aa99cc"        android:scaletype= "Fitstart"        android:src= "@drawable/small"/>
Display effect:

Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center "/>

The big picture is scaled to the width of the ImageView and then appears on top. The small picture is the height of the zoom to ImageView and then the left display.

Fitcenter

Fitcenter and Fitstart are basically the same, the only difference is that the fitcenter is centered after zooming the picture proportionally. Look at the following example:

    <imageview        android:layout_width= "48DP"        android:layout_height= "256DP"        android:background= "# Ffaa77 "        android:scaletype=" Fitcenter "        android:src=" @drawable/big "/>    <imageview        android: Layout_width= "256DP"        android:layout_height= "48DP"        android:background= "#aa99cc"        android:scaletype= "Fitcenter"        android:src= "@drawable/small"/>

Display effects such as the following:


Fitend

Fitend and Fitstart are basically the same. The only difference is that fitend the picture proportionally to the right or home display, for example, the following demo:

    <imageview        android:layout_width= "48DP"        android:layout_height= "256DP"        android:background= "# Ffaa77 "        android:scaletype=" fitend "        android:src=" @drawable/big "/>    <imageview        android: Layout_width= "256DP"        android:layout_height= "48DP"        android:background= "#aa99cc"        android:scaletype= "Fitend"        android:src= "@drawable/small"/>

Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center "/>

Center

Center indicates that the original image is centered in the previous size, assuming the original size is larger than the size of the ImageView, then trim off the excess, just show the middle part of the picture, for example, one of the following demo:

    <imageview        android:layout_width= "48DP"        android:layout_height= "256DP"        android:background= "# Ffaa77 "        android:scaletype=" center "        android:src=" @drawable/big "/>    <imageview        android: Layout_width= "256DP"        android:layout_height= "24DP"        android:background= "#aa99cc"        android:scaletype= "Center"        android:src= "@drawable/small"/>
Display effects such as the following:

Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center "/>

Centercrop

Centercrop's goal is to fill the imageview, so scale the original image so that it fills the ImageView. Cut out the extra width or height at the same time, for example, one of the following demos:

    <imageview        android:layout_width= "48DP"        android:layout_height= "256DP"        android:background= "# Ffaa77 "        android:scaletype=" Centercrop "        android:src=" @drawable/big "/>    <imageview        android: Layout_width= "256DP"        android:layout_height= "48DP"        android:background= "#aa99cc"        android:scaletype= "Centercrop"        android:src= "@drawable/small"/>
Display effects such as the following:

Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center "/>

Centerinsid

The goal of Centerinside is to display the original image in its entirety. Therefore, the original image is scaled proportionally. Allows ImageView to fully display the original image, for example, one of the following demo:

    <imageview        android:layout_width= "48DP"        android:layout_height= "256DP"        android:background= "# Ffaa77 "        android:scaletype=" centerinside "        android:src=" @drawable/big "/>    <imageview        Android:layout_width= "256DP"        android:layout_height= "48DP"        android:background= "#aa99cc"        android: Scaletype= "Centerinside"        android:src= "@drawable/small"/>

Display effects such as the following:

Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center "/>


Ok. About the ScaleType attribute we say so much. under normal circumstances, we can use these attributes when we give ImageView a fixed value for the width and height.


Demo download



ImageView ScaleType Properties for Android Learning notes

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.