按钮、图像视图和图像按钮

1、创建安卓应用【ZoomImageByButton】

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

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

在这里插入图片描述

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

在这里插入图片描述

代码

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

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"
    tools:context=".MainActivity"
    android:padding="10dp"
    android:orientation="vertical">

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

        <Button
            android:id="@+id/btn_shrink_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="10dp"
            android:text="@string/shrink_image"
            android:onClick="doShrinkImage">

        </Button>

        <Button
            android:id="@+id/btn_enlarge_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="10dp"
            android:text="@string/enlarge_image"
            android:onClick="doEnlargeImage">

        </Button>

        <ImageButton
            android:id="@+id/btn_exit"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:background="@drawable/img"
            android:onClick="doExit">

        </ImageButton>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center">
        <ImageView
            android:id="@+id/iv_mickey"
            android:layout_width="200dp"
            android:layout_height="300dp"
            android:background="@drawable/mickey">
        </ImageView>
    </LinearLayout>
</LinearLayout>

5、主界面类MainActivity

在这里插入图片描述
在这里插入图片描述
获取屏幕尺寸
在这里插入图片描述
获取图像尺寸
在这里插入图片描述

编写按钮

缩小
在这里插入图片描述
放大
在这里插入图片描述

代码

package net.hw.zoom_image;

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity {

    private ImageView ivMickey;
    private double imageWidth;
    private double imageHeight;
    private double screenWidth;
    private double screenHeight;
    private double scale = 0.95;


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

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

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

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

    /**
     *缩小图片按钮
     *
     * @param view
     */
    public void doShrinkImage(View view){
        //获取图片新尺寸
        int newWidth = (int) (imageWidth * scale);
        int newHeight = (int) (imageHeight * scale);
        //按新尺寸设置图像(不能缩小为0)
        if(newWidth>50){
            //按照新尺寸设置图像
            ivMickey.setLayoutParams(new LinearLayout.LayoutParams(newWidth,newHeight));
            //更新图像尺寸变量
            imageWidth = ivMickey.getLayoutParams().width;
            imageHeight = ivMickey.getLayoutParams().height;
        }else {
            Toast.makeText(this,"温馨提示:图片不能再缩小了,要不然看不见了哦",Toast.LENGTH_SHORT).show();
        }

    }

    /**
     * 放大图片按钮
     *
     * @param view
     */
    public void doEnlargeImage(View view){
        //获取图片新尺寸
        int newWidth = (int) (imageWidth / scale);
        int newHeight = (int) (imageHeight / scale);
        //按新尺寸设置图像(不能缩小为0)
        if(newWidth < screenWidth){
            //按照新尺寸设置图像
            ivMickey.setLayoutParams(new LinearLayout.LayoutParams(newWidth,newHeight));
            //更新图像尺寸变量
            imageWidth = ivMickey.getLayoutParams().width;
            imageHeight = ivMickey.getLayoutParams().height;
        }else {
            Toast.makeText(this,"温馨提示:图片不能再放大了,要不然出界了哦",Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 关闭按钮
     *
     * @param view
     */
    public void doExit(View view){
        finish();
    }
}

演示

QQ录屏20221107201550

作业

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值