文章目录
一、按钮控件
常用属性
text:文本内容
textSize:文本尺寸
textColor:文本颜色
onClick:单击事件(用于绑定事件处理方法)
二、图像视图
常用属性
src
:源(用于设置图片源)
background
:背景(用于设置背景图片)
scaleType
:缩放类型()
tint
(蒙版)
三、图像按钮
常用属性
src
:源(用于设置图片源)
background
:背景(用于设置背景图片)
四、案例演示
(1)获得图像尺寸
- 通过图像视图对象的
getLayoutParams()
方法得到布局参数对象,然后再利用布局参数对象提供的width与height属性即可获得图像的尺寸。 imageWidth = ivBear.getLayoutParams().width;
imageHeight = ivBear.getLayoutParams().height;
(2)设置图像尺寸
- 通过图像视图对象的
setLayoutParams()
方法来设置,必须要传入一个布局参数对象,而该布局参数对象初始化时传入新的图像尺寸。
ivBear.setLayoutParams(new LinearLayout.LayoutParams(newWidth, newHeight));
(3)步骤
1、创建安卓应用【ZoomImageByButton】
2、添加图片
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"
android:orientation="vertical"
android:gravity="center_horizontal"
android:background="@drawable/background"
android:padding="10dp"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<Button
android:id="@+id/btn_enlarge_image"
android:onClick="doEnlargeImage"
android:text="@string/enlarge_image"
android:layout_marginRight="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btn_shrink_image"
android:onClick="doShrinkImage"
android:text="@string/shrink_image"
android:layout_marginRight="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageButton
android:id="@+id/btn_exit"
android:onClick="doExit"
android:layout_width="40dp"
android:layout_height="40dp"
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/mickey"/>
</LinearLayout>
</LinearLayout>
5、主界面类MainActivity
package net.ls.zoomimagebybutton;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
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 doEnlargeImage(View view){
int newWidth = (int)(imageWidth / scale);
int newHeight = (int) (imageHeight / scale);
if (ivMickey.getLayoutParams().width<screenWidth){
ivMickey.setLayoutParams(new LinearLayout.LayoutParams(newWidth,newHeight));
imageWidth = ivMickey.getLayoutParams().width;
imageHeight = ivMickey.getLayoutParams().height;
}else {
Toast.makeText(this,"温馨提示:图片不能再放大,要不然就出界咯!",Toast.LENGTH_LONG).show();
}
}
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_LONG).show();
}
}
/**
* 退出应用程序
*
* @param view
*/
public void doExit(View view){
finish();
}
}
6、启动应用,查看效果
五、练习任务(按钮(手势)切换图片,zoomctrols实现缩放)
1、创建应用ZoomAndSwitch
2、放入图片
3、string文件
<resources>
<string name="app_name">图片缩放与切换</string>
<string name="switcher">切换图片</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="20dp"
android:gravity="center_horizontal"
tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="imageSwitch"
android:layout_marginBottom="20dp"
android:text="@string/switcher"/>
<ZoomControls
android:id="@+id/zoomContols"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageView
android:id="@+id/imgControls"
android:layout_width="200dp"
android:layout_height="300dp"
android:layout_marginTop="20dp"
android:src="@drawable/img1"/>
</LinearLayout>
5、主界面代码
-
声明控件变量
-
实例化控件
-
缩小图片
-
放大图片
-
按钮切换图片
-
手势切换图片
-
源代码查看
package net.ls.zoomandswitch;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
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 ZoomControls zoomControls;
private ImageView imgControls;
private int addcount;
private double imageWidth;
private double imageHeight;
private double screenWidth;
private double screenHeight;
private double zoomScale=0.95;
private GestureDetector detector;
private int[] imgIds;
private int imgIndex;
private final static int IMG_COUNT=10;
private final static String TAG="hwork18";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取实例化对象
zoomControls = findViewById(R.id.zoomContols);
imgControls = findViewById(R.id.img_Controls);
//获取图像尺寸
imageWidth = imgControls.getLayoutParams().width;
imageHeight = imgControls.getLayoutParams().height;
//获取屏幕尺寸
screenWidth = getWindowManager().getDefaultDisplay().getWidth();
screenHeight = getWindowManager().getDefaultDisplay().getHeight();
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();
}
}
});
imgIds = new int[IMG_COUNT];
for (int i=0;i<IMG_COUNT;i++){
imgIds[i] = getResources().getIdentifier("img"+(i+1),
"drawable",
"net.ls.zoomandswitch");
}
detector = new GestureDetector(new GestureDetector.OnGestureListener() {
@Override
public boolean onDown(MotionEvent e) {
Log.i(TAG,"onDown");
return false;
}
@Override
public void onShowPress(MotionEvent e) {
Log.i(TAG,"onShowPress");
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
Log.i(TAG,"onSingleTapup");
return false;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
Log.i(TAG,"onScroll");
return false;
}
@Override
public void onLongPress(MotionEvent e) {
Log.i(TAG,"onLongPress");
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
Log.i(TAG,"onFling");
if (e2.getX()<e1.getX()-10){
if (imgIndex<IMG_COUNT-1){
imgIndex++;
}
else {
imgIndex = 0;
}
}
if (e2.getX()>e1.getX()+10){
if (imgIndex>0){
imgIndex--;
}
else {
imgIndex = IMG_COUNT-1;
}
}
imgControls.setImageResource(imgIds[imgIndex]);
return false;
}
});
}
/**
* 将触摸事件交给手势侦测器来处理
*/
public boolean onTouchEvent(MotionEvent event){
return detector.onTouchEvent(event);
}
public void imageSwitch(View view){
int [] imgs = new int[10];
for (int i=0;i<imgs.length;i++){
imgs[i]=getResources().getIdentifier("img"+(i+1),
"drawable",
"net.ls.zoomandswitch");
}
int index= ++addcount % imgs.length;
imgControls.setImageResource(imgs[index]);
}
}