《Android Studio开发实战》学习(三)- 展示图片
背景
在前一篇文章 1中实现了使用Android Studio开发一个简易聊天室App,熟悉了简单控件TextView的使用 2,在这里继续研究Android Studio的使用方法。本文的目的是介绍图像视图ImageView,并且开发一个图片展示App,实现点击按钮,将图片拉伸到整个窗口的功能。
问题描述
现在想要设计一个单页面图片展示工具,页面布局是
- 图片显示窗口,宽度是70%,高度100%,用来展示图片(事先把一张照片上传到Android Studio中);
- 按钮面板,宽度是30%,高度为100%,包括3行2列6个按钮,每个按钮宽高一致,每个按钮对应一个不同的拉伸类型,点击即可对上面窗口的图片进行拉伸操作。
将图片添加到Android Studio资源中
将图片添加到Android Studio资源中的方法很简单 3,只需要将图片拷贝到AndroidStudioProjects文件夹下当前工程的drawable文件夹内,在Android Studio界面的资源列表中就能看到,可以这样
cp island.JPG ~/AndroidStudioProjects/ImageView/app/src/main/res/drawable/
然后在资源列表中打开res/drawable目录,就能看到刚才添加的图片。注意目前Android Studio还不支持中文名称的图片,图片的名称只能是小写字母a-z,数字0-9和下划线(不能包括点.
,否则识别不了),如图所示。
图像视图ImageView的使用
ImageView
是图像显示控件 2,可以在布局文件activity_main.xml
中设置它的src
属性来把它和要展示的图片链接起来 4:
<ImageView
android:id="@+id/iv_scale"
android:layout_width="match_parent"
android:layout_height="400dp"
android:src="@drawable/island" />
关闭APP中标题的显示
想要在生成的应用APP中关闭当前工程的标题 5,可以把res/values/themes/themes.xml
中style
的parent
属性设置为parent="Theme.*.NoActionBar"
。
图片展示工具布局文件的编写
activity_main.xml
的代码如下所示:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.constraintlayout.widget.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/guideline_3"
app:layout_constraintGuide_percent=".33"
android:orientation="horizontal"/>
<androidx.constraintlayout.widget.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/guideline_4"
app:layout_constraintGuide_percent=".67"
android:orientation="horizontal"/>
<androidx.constraintlayout.widget.Guideline
android:layout_width="wrap_content"