案例演示
1、创建应用
2、准备图片素材
3、字符串资源文件
<resources>
<string name="app_name">通过按钮缩放图片</string>
<string name="enlarge_image">放大图片</string>
<string name="shrink_image">缩小图片</string>
</resources>
4、主布局资源文件
将默认的约束布局改成线性布局,设置相关属性
添加两个线性布局
在第一个子线性布局里添加两个按钮和一个图像按钮
在第二个子线性布局里添加一个图像控件,显示米老鼠
查看完整代码
<?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:orientation="vertical"
android:padding="10dp"
android:background="@drawable/background1"
tools:context=".MainActivity">
<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:text="@string/shrink_image"
android:onClick="doShrinkImage"
android:layout_marginRight="10dp"/>
<Button
android:id="@+id/btn_enlarge_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/enlarge_image"
android:onClick="doEnlargeImage"
android:layout_marginRight="10dp"/>
<ImageButton
android:id="@+id/btn_exit"
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/iv_mickey"
android:layout_width="200dp"
android:layout_height="300dp"
android:background="@drawable/mickey"/>
</LinearLayout>
</LinearLayout>
5、主布局资源文件
主布局资源文件-MainActivity
声明变量
获取控件实例
获取图像尺寸
编写【缩小图片】 按钮单击事件处理方法
编写放大图片按钮单击事件处理方法
编写关闭图像按钮单击事件处理方法
查看完整代码
package net.zhx.zoomlmage;
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);
//按新尺寸设置图像(不能缩小为零,否则不能再放大)
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();
}
}
/**
*放大图片按钮单击事件处理方法
*/
public void doEnlargeImage(View view){
//按比例缩小图片尺寸
int newWidth = (int) (imageWidth / scale);
int newHeight = (int) (imageHeight / scale);
//按新尺寸设置图像(不能再放大了,否则不能看不见了)
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();
}
}
/**
* 关闭图像按钮单击事件处理方法
*/
public void doExit(View view){
finish();//关闭当前窗口
}
}
6、启动应用,查看效果