论坛
潜水/灌水快乐,沉淀知识,认识更多同行。
ToB圈子
加入IT圈,遇到更多同好之人。
朋友圈
看朋友圈动态,了解ToB世界。
ToB门户
了解全球最新的ToB事件
博客
Blog
排行榜
Ranklist
文库
业界最专业的IT文库,上传资料也可以赚钱
下载
分享
Share
导读
Guide
相册
Album
记录
Doing
搜索
本版
文章
帖子
ToB圈子
用户
免费入驻
产品入驻
解决方案入驻
公司入驻
案例入驻
登录
·
注册
只需一步,快速开始
账号登录
立即注册
找回密码
用户名
Email
自动登录
找回密码
密码
登录
立即注册
首页
找靠谱产品
找解决方案
找靠谱公司
找案例
找对的人
专家智库
悬赏任务
圈子
SAAS
IT评测·应用市场-qidao123.com
»
论坛
›
软件与程序人生
›
移动端开发
›
AndroidStudio-图像体现
AndroidStudio-图像体现
刘俊凯
论坛元老
|
2025-1-2 11:27:42
|
显示全部楼层
|
阅读模式
楼主
主题
1007
|
帖子
1007
|
积分
3025
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要
登录
才可以下载或查看,没有账号?
立即注册
x
一、图像视图ImageView
图像视图展示的图片通常位于res/drawable目录,设置图像视图的体现图片有两种方式:
1.在XML文件中,通过属性android:src设置图片资源,属性值格式形如"@drawable/不含扩展名的图片名称”
例如:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/iv_scale"
android:layout_width="match_parent"
android:layout_height="220dp"
android:layout_marginTop="5dp"
android:src="@drawable/xiao"/>
</LinearLayout>
复制代码
2.在Java代码中,调用setlmageResource方法设置图片资源,方法参数格式形如“R.drawable.不
含扩展名的图片名称”
package com.example.chapter01;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
public class ImageScaleActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_scale);
ImageView iv_scale = findViewById(R.id.iv_scale);
iv_scale.setImageResource(R.drawable.xiao);
}
}
复制代码
二、图像视图的缩放类型
lmageView本身默认图片居中体现,若要改变图片的体现方式,可通过scaleType属性设定,该属性的取值阐明如下:
1.fitXY
2.fitStart
3.fitCenter
4.center
5.centerCrop
6.centerInside
centerInside和center的体现效果一模一样,这缘于它们的缩放规则设定。表面上fitCenter、centerInside、center三个类型都是居中体现,且均不越过图像视图的边界。它们之间的区别在于:fitcenter既答应缩小图片、也答应放大图片,centerinside只答应缩小图片、不答应放大图标,而center自始至终保持原始尺寸(既不答应缩小图片、也不答应放大图片)。因此,当图片尺寸大于视图宽高,centerInside与fitCenter都会缩小图片,此时它俩的体现效果相同;当图片尺寸小于视图宽高,centerInside与center都保持图片大小不变,此时它俩的体现效果相同。
三、Java代码中设置图片大小
例如:
package com.example.chapter01;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
public class ImageScaleActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_scale);
ImageView iv_scale = findViewById(R.id.iv_scale);
iv_scale.setImageResource(R.drawable.xiao);
iv_scale.setScaleType(ImageView.ScaleType.CENTER);
}
}
复制代码
四、图像按钮ImageButton
lmageButton是体现图片的图像按钮,但它继续自lmageView,而非继续Button。
lmageButton和Button之间的区别有:
1.Button既可体现文本也可体现图片,ImageButton只能体现图片不能体现文本。
2.lmageButton上的图像可按比例缩放,而Button通过配景设置的图像会拉伸变形
3.Button只能靠配景体现一张图片,而lmageButton可分别在远景和配景体现图片,从而实现两张图片叠加的效果。
例如:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageButton
android:layout_width="match_parent"
android:layout_height="80dp"
android:src="@drawable/sqrt"
android:scaleType="fitCenter"/>
</LinearLayout>
复制代码
lmageButton的使用场所:在某些场所,有的字符无法由输入法打出来,或者某些笔墨以特别字体展示,就适合先切图再放到ImageButton。例如:开平方符号、,等等。
lmageButton与lmageView之间的区别有:
1.lmageButton有默认的按钮配景,lmageView默认无配景:
2.lmageButton默认的缩放类型为center,而ImageView默认的缩放类型为fitCenter。
五、同时展示文本和图像
同时展示文本与图像的可能途径包括:
1.使用LinearLayout对lmageView和TextView组合结构
2.通过按钮控件Button的drawable属性设置文本周围的图标。
(1)drawableTop:指定笔墨上方的图片。
(2)drawableBottom:指定笔墨下方的图片。
(3)drawableLeft:指定笔墨左边的图片。
(4)drawableRight:指定笔墨右边的图片
(5)drawablePadding:指定图片与笔墨的间距。
设置如下视图:
代码如下:
1.drawableLeft:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/img"
android:drawablePadding="5dp"
android:text="图标在左" />
</LinearLayout>
复制代码
我们发现配景颜色默认是紫色
如果要修改配景颜色,就要在res/values目录下的themes.xml文件内继续于bridge
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复
使用道具
举报
0 个回复
正序浏览
返回列表
快速回复
高级模式
B
Color
Image
Link
Quote
Code
Smilies
您需要登录后才可以回帖
登录
or
立即注册
本版积分规则
发表回复
回帖并转播
发新帖
回复
刘俊凯
论坛元老
这个人很懒什么都没写!
楼主热帖
牛客SQL刷题第三趴——SQL大厂面试真题 ...
IDEA中集成Git操作以及关于Git中分支说 ...
SAP MM 使用两个STO实现免关税跨国公 ...
github上fork2.4k,star8.7k的这款状态 ...
Bug驱动开发探讨
MySQL ——select语句 一条龙服务 ...
袋鼠云春季生长大会最新议程来啦!4月2 ...
哈工大信息安全概论期末复习 ...
事务
2023H1中国超融合市场第二!深信服超融 ...
标签云
AI
运维
CIO
存储
服务器
快速回复
返回顶部
返回列表