安卓讲课笔记5.3按钮、图像视图与图像按钮——上机操作

(一)实现步骤

1、创建安卓应用【Switch_Image_By_Button】

在这里插入图片描述

  • 单击【finish】
    在这里插入图片描述

2、将图片拷贝到drawable目录

在这里插入图片描述

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

在这里插入图片描述

4、主布局界面activity_main.xml

在这里插入图片描述

在这里插入图片描述

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

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="doPrevious"
        android:layout_marginRight="10dp"
        android:text="上一张"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="doNext"
        android:text="下一张"/>
    </LinearLayout>

    <ZoomControls
        android:id="@+id/zoomControls"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <ImageView
        android:id="@+id/imgControls"
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:paddingTop="20dp"
        android:background="@drawable/img1"/>

</LinearLayout>

5、主界面类MainActivity

  • 定义常量
    在这里插入图片描述
package dirnet.zh.switch_image_by_button;

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity {

    private ImageView imgControls; // 图像控件
    private ZoomControls zoomControls;
    private int[] imgIds; // 图像资源标识符数组
    private int imgIndex; // 图像索引,在图像资源标识符数组的位置
    private final int IMG_COUNT = 8; // 图像总数
    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);
        // 通过资源标识符获取控件实例
        imgControls = findViewById(R.id.imgControls);
        zoomControls = findViewById(R.id.zoomControls);
        //获得屏幕尺寸
        screenWidth = getWindowManager().getDefaultDisplay().getWidth();
        screenHeight = getWindowManager().getDefaultDisplay().getHeight();
        //获得图像尺寸
        imageWidth = imgControls.getLayoutParams().width;
        imageHeight = imgControls.getLayoutParams().height;
        //初始化图像资源标识符数组
        imgIds = new int[IMG_COUNT];
        for (int i = 0; i < IMG_COUNT; i++) {
            imgIds[i] = getResources().getIdentifier(
                    "img" + (i + 1), // 标识符名称
                    "drawable", // 定义类型
                    "dirnet.zh.switch_image_by_button" // 定义包名
            );
        }
        /**
         * 缩放图片
         */
        zoomControls.setOnZoomOutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取图像新尺寸
                int newWidth = (int)(imageWidth * zoomScale);
                int newHeight = (int)(imageHeight*zoomScale);
                //按新尺寸设置图像
                if(newWidth>50){
                    imgControls.setLayoutParams(new LinearLayout.LayoutParams(newWidth,newHeight));
                    //重新获得图像尺寸
                    imageWidth = imgControls.getLayoutParams().width;
                    imageHeight = imgControls.getLayoutParams().height;
                }else {
                    Toast.makeText(MainActivity.this,"温馨提示:图片不能再缩小,要不然看不见咯!",Toast.LENGTH_LONG).show();
                }
            }
        });
        zoomControls.setOnZoomInClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取图像新尺寸
                int newWidth = (int)(imageWidth / zoomScale);
                int newHeight = (int)(imageHeight/zoomScale);
                //按新尺寸设置图像
                if(newWidth<screenWidth){
                    imgControls.setLayoutParams(new LinearLayout.LayoutParams(newWidth,newHeight));
                    //重新获得图像尺寸
                    imageWidth = imgControls.getLayoutParams().width;
                    imageHeight = imgControls.getLayoutParams().height;
                }else {
                    Toast.makeText(MainActivity.this,"温馨提示:图片不能再放大,要不然看不见咯!",Toast.LENGTH_LONG).show();
                }
            }
        });
    }
    /**
     * 上一张按钮单击事件处理方法
     *
     * @param view
     */
    public void doPrevious(View view) {
        if (imgIndex > 0) {
            imgIndex--; // 切换到上一张
        } else {
            imgIndex = IMG_COUNT - 1; // 切换到最后一张
        }
        // 根据新索引切换照片
        imgControls.setBackgroundResource(imgIds[imgIndex]);
    }

    /**
     * 下一张按钮单击事件处理方法
     *
     * @param view
     */
    public void doNext(View view) {
        if (imgIndex < IMG_COUNT - 1) {
            imgIndex++; // 切换到下一张
        } else {
            imgIndex = 0; // 回到第1张
        }
        // 根据新索引切换照片
        imgControls.setBackgroundResource(imgIds[imgIndex]);
    }
}

6、启动应用,查看效果

在这里插入图片描述

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值