安卓常用控件---按钮、图像视图与图像按钮
(一)按钮控件
常用属性
属性 | 作用 |
---|---|
text | 文本内容 |
textSize | 文本尺寸 |
textColor | 文本颜色 |
onClick | 单击事件(用于绑定事件处理方法) |
(二)图像视图
常用属性
属性 | 作用 |
---|---|
src | 源(用于设置图片源) |
background | 背景(用于设置背景图片) |
scaleType | 缩放类型() |
tint | 蒙版 |
(三)图像按钮
常用属性
属性 | 作用 |
---|---|
src | 源(用于设置图片源) |
background | 背景(用于设置背景图片) |
(四)案例演示:通过按钮缩放图片
1、创建安卓应用
- 基于Empty Activity创建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.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:background="@drawable/backgroud"
android:orientation="vertical"
android:padding="10dp"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_shrink_image"
android:layout_marginRight="10dp"
android:onClick="doShrinkImage"
android:text="@string/shrink_image"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_enlarge_image"
android:layout_marginRight="10dp"
android:onClick="doEnlargeImage"
android:text="@string/enlarge_image"/>
<ImageButton
android:id="@+id/btn_exit"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@drawable/exit"
android:onClick="doExit"/>
</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.xml
- 声明变量
- 通过资源标识符获取控件实例
- 获取屏幕尺寸
- 获取图像尺寸
- 编写【缩小图片】按钮单击事件
- 编写【放大图片】按钮单击事件
- -编写【关闭】图像按钮单击事件
- 查看完整代码
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;
}
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 doEixt(View view){
finish();
}
}