Android 常用控件 - 按钮、图像视图和图像按钮

一、按钮控件

1、继承关系图

在这里插入图片描述

2、常用属性

  • text:文本内容
  • textSize:文本尺寸
  • textColor:文本颜色
  • onClick:单击事件(用于绑定事件处理方法)

二、图像按钮

1、继承关系图

在这里插入图片描述

src:源(用于设置图片源)
background:背景(用于设置背景图片)
scaleType:缩放类型()
tint(蒙版)

三、图像按钮

1、继承关系图

在这里插入图片描述

2、常用属性

  • src:源(用于设置图片源)
  • background:背景(用于设置背景图片)

四、案例操作

1、创建安卓应用【ZoomImageByButton】

在这里插入图片描述
在这里插入图片描述

2、将两张图片拷贝到drawable目录

在这里插入图片描述

3、字符串资源文件strings.xml

在这里插入图片描述

<resources>
    <string name="app_name">通过按钮缩放图片</string>
    <string name="minus">缩小图片</string>
    <string name="plus">放大图片</string>
</resources>

4、主布局资源文件activity_main.xml

在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:padding="10dp"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center" >

        <Button
            android:id="@+id/btnMinus"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="doMinus"
            android:text="@string/minus" />

        <Button
            android:id="@+id/btnPlus"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="doPlus"
            android:text="@string/plus" />

        <ImageButton
            android:id="@+id/btnExit"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:onClick="doExit"
            android:background="@drawable/exit" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center" >

        <ImageView
            android:id="@+id/ivBear"
            android:layout_width="200dp"
            android:layout_height="300dp"
            android:src="@drawable/bear" />
    </LinearLayout>

</LinearLayout>

5、主界面类MainActivity

在这里插入图片描述

package net.hw.zoom_image;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    
    
    private ImageView ivBear; // 图像控件
    private double imageWidth; // 图像宽度
    private double imageHeight; // 图像高度
    private double screenWidth; // 屏幕宽度
    private double screenHeight; // 屏幕高度
    private double zoomScale = 0.95; // 缩放比例

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        // 利用布局资源文件设置用户界面
        setContentView(R.layout.activity_main);

        // 通过资源标识符获得控件实例
        ivBear = findViewById(R.id.ivBear);

        // 获得屏幕尺寸
        screenWidth = getWindowManager().getDefaultDisplay().getWidth();
        screenHeight = getWindowManager().getDefaultDisplay().getHeight();

        // 获得图像尺寸
        imageWidth = ivBear.getLayoutParams().width;
        imageHeight = ivBear.getLayoutParams().height;
    }

    /**
     * 缩小图片单击事件处理方法
     *
     * @param view
     */
    public void doMinus(View view) {
    
    
        // 获得图像新尺寸
        int newWidth = (int) (imageWidth * zoomScale);
        int newHeight = (int) (imageHeight * zoomScale);
        // 按新尺寸设置图像(不能缩小为零,否则不能再放大)
        if (newWidth > 50) {
    
    
            ivBear.setLayoutParams(new LinearLayout.LayoutParams(newWidth, newHeight));
            // 重新获取图像尺寸
            imageWidth = ivBear.getLayoutParams().width;
            imageHeight = ivBear.getLayoutParams().height;
        } else {
    
    
            Toast.makeText(this, "温馨提示:图片不能再缩小,要不然看不见咯!", Toast.LENGTH_LONG).show();
        }
    }

    /**
     * 放大图片单击事件处理方法
     *
     * @param view
     */
    public void doPlus(View view) {
    
    
        // 获得图像新尺寸
        int newWidth = (int) (imageWidth / zoomScale);
        int newHeight = (int) (imageHeight / zoomScale);
        // 按新尺寸设置图像(不能再放大,否则就出界了)
        if (ivBear.getLayoutParams().width < screenWidth) {
    
    
            ivBear.setLayoutParams(new LinearLayout.LayoutParams(newWidth, newHeight));
            // 重新获取图像尺寸
            imageWidth = ivBear.getLayoutParams().width;
            imageHeight = ivBear.getLayoutParams().height;
        } else {
    
    
            Toast.makeText(this, "温馨提示:图片不能再放大,要不然就出界咯!", Toast.LENGTH_LONG).show();
        }
    }

    /**
     * 退出应用程序
     *
     * @param view
     */
    public void doExit(View view) {
    
    
        finish();
    }
}

五、运行应用,查看效果

请添加图片描述

猜你喜欢

转载自blog.csdn.net/m0_62786921/article/details/128355461