Android ImageView ImageButton示例

Android ImageView is used to display an image file. Android also has an ImageButton. As the name suggests, the ImageButton component is a button with an image on. The ImageButton is represented by the Android class android.widget.ImageButton. In this tutorial we’re going to implement both android ImageView and ImageButton in our application.

Android ImageView用于显示图像文件。 Android还具有ImageButton 。 顾名思义, ImageButton组件是一个带有图像的按钮。 ImageButton由Android类android.widget.ImageButton表示 。 在本教程中,我们将在应用程序中实现android ImageView和ImageButton。

Android ImageView (Android ImageView)

Android ImageView is one of the UI widget that is used to display images in the application. It’s defined in the XML layout in the following manner.

Android ImageView是用于在应用程序中显示图像的UI小部件之一。 它是通过以下方式在XML布局中定义的。

<ImageView
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:src="@drawable/ic_launcher" />

Here android:src is used to assign the image file from the drawable resource folder.

在这里android:src用于分配来自drawable资源文件夹的图像文件。

Android ImageView ScaleType (Android ImageView ScaleType)

ImageView in android comes with different configuration options to support different scaleTypes.

android中的ImageView具有不同的配置选项以支持不同的scaleTypes。

scaleType options are used for scaling the bounds of an image to the bounds of image view. Below are the listed scaleType configuration properties supported.

scaleType选项用于将图像的边界缩放到图像视图的边界。 以下是列出的受支持的scaleType配置属性。

  1. CENTER : Displays the image centered in the view with no scaling.

    CENTER :不缩放显示图像在视图中心的位置。
  2. CENTER_CROP : Scales the image such that both the x and y dimensions are greater than or equal to the view, while maintaining the image aspect ratio; centers the image in the view

    CENTER_CROP :缩放图像,使x和y尺寸均大于或等于视图,同时保持图像的长宽比; 使图像在视图中居中
  3. CENTER_INSIDE : Scales the image to fit inside the view, while maintaining the image aspect ratio. If the image is already smaller than the view, then this is the same as center

    CENTER_INSIDE :缩放图像以适合视图内部,同时保持图像的宽高比。 如果图像已经小于视图,则与中心相同
  4. FIT_CENTER : Scales the image to fit inside the view, while maintaining the image aspect ratio. At least one axis will exactly match the view, and the result is centered inside the view

    FIT_CENTER :缩放图像以适合视图内部,同时保持图像的宽高比。 至少一个轴将与视图完全匹配,并且结果在视图内居中
  5. FIT_START : Same as fitCenter but aligned to the top left of the view

    FIT_START :与fitCenter相同,但与视图的左上方对齐
  6. FIT_END : Same as fitCenter but aligned to the bottom right of the view

    FIT_END :与fitCenter相同,但与视图的右下角对齐
  7. FIT_XY : Scales the x and y dimensions to exactly match the view size. dDoes not maintain the image aspect ratio

    FIT_XY :缩放x和y尺寸以完全匹配视图大小。 d不保持图像宽高比
  8. MATRIX : Scales the image using a supplied Matrix class. The matrix can be supplied using the setImageMatrix method. A Matrix class can be used to apply transformations such as rotations to an image

    MATRIX :使用提供的Matrix类缩放图像。 可以使用setImageMatrix方法提供矩阵。 Matrix类可用于将变换(例如旋转)应用于图像

The default scaleType in FIT_CENTER.

FIT_CENTER中的默认FIT_CENTER

Note: The fitXY scale type allows you to set the exact size of the image in your layout. However, there can occur potential distortions of the image due to scaling. For example;

注意fitXY比例类型允许您设置布局中图像的确切尺寸。 但是,由于缩放可能会出现图像的潜在失真。 例如;

<ImageView
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:scaleType="fitXY"
            android:src="@drawable/ic_launcher" />

在Android ImageView中支持多种密度 (Supporting Multiple Densities in android ImageView)

There is a powerful system for selecting the correct image asset for the correct device. There are specific drawable folders for each device density category including: ldpi (low), mdpi (medium), hdpi (high), and xhdpi (extra high) etc. For example, a drawable-mdpi stands for drawable medium dots per inch.

有一个功能强大的系统可以为正确的设备选择正确的图像资产。 每个设备密度类别都有特定的可绘制文件夹,包括:ldpi(低),mdpi(中),hdpi(高)和xhdpi(超高)等。例如, drawable-mdpi表示每英寸可绘制的中点

In this project we’ll show various image scaleTypes in the activity along with an android Image Button.

在这个项目中,我们将在活动中显示各种图像scaleTypes以及一个android图像按钮。

向资源添加图像 (Adding Image to Resources)

We’ll use ImageView to display a “png” image. PNGs are lossless, so they have that advantage over JPG images. We add our images into folder “res/drawable-xdpi“, “res/drawable-mdpi” or “res/drawable-hdpi“ etc. depending on the size of the image.

我们将使用ImageView显示“ png”图像。 PNG是无损的,因此它们比JPG图像具有这一优势。 我们将图像添加到文件夹“ res / drawable-xdpi”,“ res / drawable-mdpi”或“ res / drawable-hdpi”等文件夹中,具体取决于图像的大小。

Android ImageView,ImageButton示例项目结构 (Android ImageView, ImageButton Example Project Structure)

As you can see we’ve added a balloon.png file to drawable folders of respective dimensions.

如您所见,我们已经向相应尺寸的可绘制文件夹中添加了Balloon.png文件。

Android ImageView项目代码 (Android ImageView Project Code)

The xml layout file contains five images with different scaleTypes along with an ImageButton. The layout is a child of a ScrollView (to make the activity scrollable) which we’ll discuss later.

xml布局文件包含五个具有不同scaleTypes的图像以及一个ImageButton。 该布局是ScrollView的子级(使活动可滚动),我们将在后面讨论。

activity_main.xml

activity_main.xml

<ScrollView xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:src="@drawable/balloon" />

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:background="#fff"
            android:padding="3dp"
            android:scaleType="fitXY"
            android:src="@drawable/balloon" />

        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:background="#fff"
            android:paddingBottom="50dp"
            android:paddingLeft="3dp"
            android:paddingRight="3dp"
            android:paddingTop="3dp"
            android:scaleType="fitStart"
            android:src="@drawable/balloon" />


        <ImageView
            android:id="@+id/imageView4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:background="#fff"
            android:paddingBottom="50dp"
            android:paddingLeft="3dp"
            android:paddingRight="3dp"
            android:paddingTop="3dp"
            android:scaleType="fitEnd"
            android:src="@drawable/balloon" />

        <ImageView
            android:id="@+id/imageView5"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:background="#fff"
            android:padding="3dp"
            android:src="@drawable/balloon" />


        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageButton"
            android:layout_gravity="center_horizontal"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"
            android:src="@android:drawable/ic_menu_send"/>

    </LinearLayout>

</ScrollView>

The MainActivity.java is defined below.

MainActivity.java在下面定义。

package com.journaldev.imageview;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // setting image resource from drawable
        ImageView imageView = (ImageView) findViewById(R.id.imageView2);
        imageView.setImageResource(R.drawable.balloon);

        imageView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Toast.makeText(getApplicationContext(), "Clicked Second Image",
                        Toast.LENGTH_SHORT).show();
            }
        });

        ImageButton imageButton=(ImageButton) findViewById(R.id.imageButton);
        imageButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "Clicked Image Button",
                        Toast.LENGTH_SHORT).show();
            }
        });

        Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.balloon);
        Bitmap bMapScaled = Bitmap.createScaledBitmap(bMap, 350, 300, true);
        ImageView image = (ImageView) findViewById(R.id.imageView5);
        image.setImageBitmap(bMapScaled);

    }
}

Here we’ve made the second ImageView clickable to display a Toast.

在这里,我们使第二个ImageView可单击以显示Toast。

Android ImageButton (Android ImageButton)

Android ImageButton has all the properties of a normal button. It has the option to add a resource file instead of text.

Android ImageButton具有普通按钮的所有属性。 它可以选择添加资源文件而不是文本。

使用位图 (Working with Bitmaps)

Bitmaps belong to the class android.graphics.Bitmap.

位图属于android.graphics.Bitmap类。

Bitmapfactory is mainly used for Scaling images from the drawable. If we don’t use BitmapFactory then it leads to insufficient memory allocations.

Bitmapfactory主要用于缩放可绘制图像。 如果我们不使用BitmapFactory则会导致内存分配不足。

We can change the bitmap displayed in an ImageView to a drawable resource as it’s done for imageview2 in the code above.

我们可以将ImageView中显示的位图更改为可绘制资源,就像上面代码中对imageview2所做的那样。

ImageView image = (ImageView) findViewById(R.id.imageview2);
image.setImageResource(R.drawable.balloon);

The imageview5 is not assigned any scaleType so we’ve scaled it using a static method createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter)

imageview5没有分配任何scaleType,因此我们使用静态方法createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter)对其进行了缩放。

To pass any file present in the storage into imageview the following code is implemented.

要将存储中存在的任何文件传递到imageview中,请执行以下代码。

ImageView image = (ImageView) findViewById(R.id.test_image);
Bitmap bMap = BitmapFactory.decodeFile("/sdcard/test.png");
image.setImageBitmap(bMap);

setImageBitmap() sets the bitmap content to the ImageView.

setImageBitmap()将位图内容设置为ImageView。

The image representing the final output is shown below:

android imageview, android imagebutton

代表最终输出的图像如下所示:

  1. The first ImageView is not assigned any scaleType by default. Hence center being the default scaleType is implicitly assigned to it.

    默认情况下,第一个ImageView未被分配任何scaleType。 因此,作为默认scaleType的center被隐式地分配给它。
  2. The second ImageView has the scaleType fitXY. A distortion is seen, hence its not a good practice to use in applications.

    第二个ImageView具有scaleType fitXY。 可以看到失真,因此在应用程序中使用它不是一个好习惯。
  3. The third and fourth ImageViews have scaleType fitStart and fitEnd respectively.

    第三和第四ImageView分别具有scaleType fitStart和fitEnd。
  4. The fifth ImageView has been scaled with custom widths and heights using Bitmaps.

    第五个ImageView已使用位图使用自定义的宽度和高度缩放。

Here is a short animation of our app too.

这也是我们应用程序的简短动画。

This brings an end to this tutorial. You can download the final Android ImageView project from the below link.

本教程到此结束。 您可以从下面的链接下载最终的Android ImageView项目。

Reference: Official Doc

参考: 官方文件

翻译自: https://www.journaldev.com/9474/android-imageview-imagebutton-example

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值