Android development self-study note (Android Studio)-4.3ImageView and its subclass, androidstudio self-study
I. Introduction
ImageView inherits from the View component and is mainly used to display images. In fact, it can display not only images, but also Drawable objects.
ImageView derives components such as ImageButton and ZoomButton. For details about the derivation and inheritance relationships, you can view the API documentation. It is really slow to open it here and it will no longer work.
It was too slow to open the local API. I wanted to give up, but I saw the android api Chinese website on the Internet. The opening speed was good, so the inheritance relationship was as follows:
Here I will also send this API online URL. If it is slow to open, you can try this:
1: API Chinese API
Ii. XML attributes and methods supported by ImageView
XML attributes |
Related Methods |
Description |
Android: adjustViewBounds |
SetAdjustViewBounds (boolean) |
Sets whether to adjust the border of the ImageView to maintain the aspect ratio of the displayed image. |
Android: cropToPadding |
SetCropToPadding (boolean) |
If this attribute is set to true, the component will be intercepted to the padding that retains the ImageView. |
Android: maxHeight |
SetMaxHeight (int) |
Sets the maximum height of an ImageView. |
Android: maxWidth |
SetMaxWidth (int) |
Set the maximum width of the ImageView. |
Android: scaleType |
SetScaleType (ImageView. ScaleType) |
Set how the displayed image scales or moves to fit the size of the ImageView. |
Android: src |
SetImageResource (int) |
Set the ID of the Drawable object displayed by ImageView |
ScaleType is an important attribute and Its configurable values are as follows:
Value |
Description |
ImageView. Scale. MATRIX |
Scale Using Matrix |
ImageView. Scale. FIT_XY |
The image is scaled horizontally and vertically independently, so that the image is fully adapted to ImageView, and the aspect ratio of the image may change. |
ImageView. Scale. FIT_START |
Maintain the aspect ratio to zoom in the image until it is fully displayed in the ImageView, starting from the upper left corner of the ImageView |
ImageView. Scale. FIT_CENTER |
Maintain the aspect ratio to zoom in the image until it is fully displayed in the ImageView, from the middle of the ImageView |
ImageView. Scale. FIT_END |
Maintain the aspect ratio until the image is fully displayed in the ImageView. |
ImageView. Scale. CENTER |
Place the image in the middle of the ImageView without any scaling |
ImageView. Scale. CENTER_CROP |
Maintain the aspect ratio to scale the image so that the image can completely overwrite the ImageView. You only need to show the shortest side of the image. |
ImageView. Scale. CENTER_INSIDE |
Scale the image to make the ImageView fully display. |
To control ImageView, the API provides the following operations:
Method Name |
Description |
SetImageBitmap (Bitmap bm) |
Use the Bitmap object to set the image displayed in ImageView |
SetImageDrawable (Drawable drawable) |
Use the Drawable object to set the image displayed in ImageView |
SetImageResource (int resId) |
Use the resource ID to set the image displayed in ImageView |
SetImageURI (Uri uri) |
Use the URI address of the image to set the image displayed in ImageView |
Iii. ImageView
The ImageView function is relatively simple. Here we use the example of crazy three to make a simple image browser to demonstrate how to use ImageView.
The sample code is as follows:
1. graphic description of the scaletype attribute of ImageView in android
2. Analysis of Android ApiDemos series [View-ImageView/ImageButton]
3. ImageView stack application in Android
4. Android Learning and Development 4-Image Display imageVIew
5. Drag the image with a simple finger and slide the image to display the application-Gallery, BaseAdapter, and ImageView
6. How to display ImageView images in full screen
7. android ImageView draw button
8. Android imageview multi-point touch
9. about the effect of the circular marquee in ImageView Images
5. ImageButton and ZoomButton
The difference between a Button and an ImageButton is that the Button displays text while an image is displayed on ImageButton. It is worth noting that setting the Text attribute for ImageButton does not work, because in essence, ImageButton is an ImageView. Even if the Text attribute is specified, no Text is displayed. If ImageButton is used, the image button can specify the src attribute. This attribute can be used to set static images or custom Drawable objects to implement custom image buttons.
ZoomButton can be seen literally as a button for the zoom-in/zoom-out function. It inherits from ImageButton and is basically similar to ImageButton, by default, android provides two Drawable resources: btn_minus and btn_plus. You only need to assign these two resource values to the src attribute to implement the corresponding button style.
Android also provides a ZoomControls component, which combines the "zoom" and "zoom" buttons and allows them to bind different listeners to each other. (ZoomButton is a single button, and ZoomControls is a combination of two buttons ).
The following sample code demonstrates these components at the same time.
1. the src of the first button is btn_ OK, which is a blue button icon. I download the button from the Internet, which is a fixed button icon.
2. The second button is a variable button. By default, the button is displayed in green. After being pressed, it turns red. After being released, it turns green. The selector resource file definition is used, and the code is pasted below.
3. Use the LinearLayout linear layout, add a ZoomButton to the content, use the system's built-in zoom-In icon to display, and set the Linearlayout layout to horizontally center.
4. defined a ZoomControls component. We can see that it has two buttons.
The btn_Selector code is as follows:
It mainly sets the image displayed when the button gets the focus and the image displayed after the button is pressed, and sets the default image in the last line.
6. QuickContactBadge
QuickContactBadge inherits ImageView, which is essentially an image. You can also specify the image displayed by using the android: src attribute. The additional features of QuickContackBadge are as follows: The image can be associated with a specified contact on your mobile phone. When you click the image, the contact information page of the corresponding contact is displayed.
Because I cannot add a contact to a virtual machine here, I will not perform Code demonstration here. I will provide several learning addresses. For more information, see.
1. Android2.2 API Chinese Document Series (8) -- QuickContactBadge
2. associate a contact with QuickContactBadge
VII. Postscript
For work reasons, learning is also intermittent, but I think I will encourage myself to learn it slowly, share my learning experiences, and deepen my memory. I hope you can give me some suggestions after reading the article and give me some encouragement. Thank you.