Imageview of Android learning notes

Source: Internet
Author: User

The imageview class can load images from various sources (such as resources or image libraries). You need to calculate the size of the image, so it can be used in other la S and provide such scaling and coloring (rendering) various display options.

 

<1>

 

Attribute
Attribute name Corresponding Method Description
Android: adjustviewbounds Setadjustviewbounds (Boolean)

Set this attribute to true in imageview
Adjust the aspect ratio of the image to maintain the border. (It must be used with maxwidth and maxheight; otherwise, it will not work if it is used separately .)

Android: Baseline Setbaseline (INT)

Offset of the baseline in the view

Android: baselinealignbottom Setbaselinealignbottom (Boolean)

If true, the image view alignment the baseline with the bottom edge of the parent control.

Android: croptopadding   If it is true, the image will be cut to adapt to the size of the padding.
Android: maxheight Setmaxheight (INT) Provides an optional parameter for the maximum height of a view.
Android: maxwidth Setmaxwidth (INT)

Provides an optional parameter for the maximum width of a view.

Android: scaletype Setscaletype (imageview. scaletype) Control to make images fit
Imageview
How to change the image size or move the image
Android: SRC Setimageresource (INT)

Set the printable object as imageview
Displayed content

Android: tint Setcolorfilter (INT, porterduff. Mode) Set colors for images

<2>

Method:

Public void
Setmaxheight(INT maxheight)

Optional parameter used to set the maximum height supported by the view. Only setadjustviewbounds (Boolean)
Valid if true. To set the maximum image size to 100x100 and keep the original ratio, follow these steps:

1)
Set adjustviewbounds
True;

2)
Set maxwidth
And maxheight are 100;

3)
Set the layout parameter to wrap_content.

Note: If the original image is small, even if this parameter is set, the image is still larger than 100x100.
Small. If you want to set the image size to a fixed value, you must specify the size in the layout parameter and use setscaletype (imageview. scaletype)
Function to detect, how
Adjust it to the appropriate size.

Parameters

Maxheight: the maximum height of the view.

Related XML
Attribute

Android: maxheight

Public voidSetmaxwidth(INT maxwidth)

Optional parameter used to set the maximum width supported by the view. Only setadjustviewbounds (Boolean)
Valid if true. To set the maximum image size to 100x100 and keep the original ratio, follow these steps:

4)
Set adjustviewbounds
True;

5)
Set maxwidth
And maxheight are 100;

6)
Set the layout parameter to wrap_content.

Note: If the original image is small, even if this parameter is set, the image is still larger than 100x100.
Small. If you want to set the image size to a fixed value, you must specify the size in the layout parameter and use setscaletype (imageview. scaletype)
Function to detect, how
Adjust it to the appropriate size.

Parameters

Maxwidth: the maximum width of the view.

Related XML
Attribute

Android: maxwidth

Public void
Setscaletype(Imageview. scaletype)

Controls how images should be scaled and moved to make images with imageview
.

Parameters

Scaletype.

Related XML
Attribute

Android: scaletype

Public void
Setselected(Boolean selected)

Changes the selected view status. The view has two states: Selected and not selected. Note that the selection status is different from the focus.
The typical selected view is like listview.
Adapterview
The selected content is highlighted.

Parameters

If selected is true, the view is set to the selected state; otherwise, the view is false.

Public voidSetimageuri(URI)

Set the specified URI
The content displayed for the imageview.

This operation reads bitmap and
Decoding in the thread, which may lead to slow response. If the response is slow, use setimagedrawable (drawable) and setimagebitmap (Bitmap)
Or bitmapfactory
.

Parameters

The URI Of The URI image.

Public voidSetimagebitmap(Bitmap BM)

Set bitmap as the imageview
.

Parameters

The bitmap set by BM.

Public voidSetimagedrawable(Drawable)

Set the printable object to the imageview.
The displayed content.

Parameters

The callable object set by drawable.

 

Public voidSetadjustviewbounds(Boolean adjustviewbounds)

When you need
Set this value to true when the border is adjusted to keep the ratio of the objects that can be drawn.

Parameters

Adjustviewbounds
Whether to adjust the border to maintain the original proportion of the object that can be drawn.

Related XML
Attribute

Android: adjustviewbounds

Public void
Setalpha(INT alpha)

Set transparency

<3>

Scaletype attributes:

 

Public static final imageview. scaletype
Center

Center the image in the view without scaling.
Syntax that can be used in XML:
Android: scaletype = "center ".

Public static final imageview. scaletype
Center_crop

Balanced scaling of the image (maintaining the original ratio of the image), so that the two coordinates (width and height) of the image are greater than or equal
Corresponding View coordinates (negative padding ). The image is located in the center of the view.
Syntax that can be used in XML: Android: scaletype = "centercrop ".

Public static final imageview. scaletype
Center_inside

Balanced scaling of the image (maintaining the original proportion of the image), so that the two coordinates (width and height) of the image are less than or equal
Corresponding View coordinates (negative padding ). The image is located in the center of the view.
The syntax that can be used in XML: Android: scaletype = "centerinside ".

Public static final imageview. scaletype
Fit_center

Use Center
Method to scale the image. In XML
Syntax: Android: scaletype = "fitcenter ".

Public static final imageview. scaletype
Fit_end

Use end
Method to scale the image. In XML
Android: scaletype = "fitend ".

Public static final imageview. scaletype
Fit_start

Use start
Method to scale the image. In XML
Android: scaletype = "fitstart ".

Public static final imageview. scaletype
Fit_xy

Use fill
Method to scale the image. In XML
Android: scaletype = "fitxy ".

Public static final imageview. scaletype
Matrix

The image matrix is used to scale the image. You can use setimagematrix (matrix) to create an image matrix)
. Syntax that can be used in XML:
Android: scaletype = "matrix ".

Case:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <TextView         android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="scaleType:fitXY"/>    <ImageView        android:id="@+id/image1"        android:adjustViewBounds="true"        android:layout_width= "300dp"        android:layout_height="100dp"        android:src= "@drawable/macos01"        android:scaleType="fitXY"        android:background="#F00"/>    <TextView         android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="scaleType:center"/>     <ImageView        android:id="@+id/image2"        android:adjustViewBounds="true"        android:layout_width= "300dp"        android:layout_height="100dp"        android:src= "@drawable/macos04"        android:scaleType="center"        android:background="#F00"/>     <TextView         android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="scaleType:fitEnd"/>      <ImageView        android:id="@+id/image3"        android:adjustViewBounds="true"        android:layout_width= "300dp"        android:layout_height="100dp"        android:src= "@drawable/macos01"        android:scaleType="fitEnd"        android:background="#F00"/>      <TextView         android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="scaleType:fitCenter"/>       <ImageView        android:id="@+id/image4"        android:adjustViewBounds="true"        android:layout_width= "300dp"        android:layout_height="80dp"        android:src= "@drawable/macos04"        android:scaleType="fitCenter"        android:background="#F00"/></LinearLayout>

 

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.