《Android Studio开发实战》学习(三)- 展示图片

本文介绍了如何使用Android Studio开发一个图片展示App。通过将图片添加到资源中,利用ImageView显示并调整图片,关闭App标题显示,以及编写布局和代码文件,实现了点击按钮改变图片拉伸效果的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


背景

在前一篇文章 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和下划线(不能包括点.,否则识别不了),如图所示。
《Android Studio开发实战》学习(三)- 展示图片 - 将图片添加到Android Studio资源中

图像视图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.xmlstyleparent属性设置为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"
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

下唐人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值