Android ImageView and imagebutton: picture view and picture button

ImageView is a component for displaying pictures, which is widely used in many occasions. ImageView can display any image and load pictures from various sources (such as resources or picture libraries). It can also calculate the size of the picture for use in any layout, and can provide options such as scaling or coloring for developers. Imagebutton is a subclass of ImageView, which is equivalent to a button indicating that it is a picture rather than text. It is used in exactly the same way as a button. Let's learn how to use these two components through an example. Add a button named imagebuttondemo in the layout file main.xml of the project widgetdemo to start the imagebuttonactivity. Add the following code in main.xml:

Click button and start ratingbaractivity. The code is as follows:

At the same time, declare the activity in the androidmanifest.xml file:

<activity android:name=".ImageButtonActivity"></activity>

The running effect of imagebuttonactivity is shown in Figure 1.

The contents of the layout file imgbtn.xml of imagebuttonactivity are as follows:

The file uses a linear layout with an ImageView component and an imagebutton component. Both components specify the displayed image through the Android: SRC attribute. This example uses two image resources, one is girl and the other is IC_ Launcher, as shown in Figure 2. Because Android will select different resources according to the configuration of mobile devices, for the universality of applications, girl.gif images are placed under the three drawable folders. ic_ Launcher.png is the system's own resource file.

The code of imagebuttonactivity.java is as follows:

Imagebuttonactivity adds a click listener to the imagebutton to handle the event when the user clicks imgbtn. Each time the user clicks the picture button, the width and height of the ImageView component are increased by 3. With the continuous click of the user, the image displayed in ImageView becomes larger and larger, which shows the zoom function of the ImageView component.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>