Android ImageView 使用详解

文章目录

ImageView 是 Android 中用于显示图片的核心控件,下面我将从基本使用到高级功能全面介绍 ImageView 的用法。

一、基本使用

1. XML 中声明 ImageView

xml 复制代码
<ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/my_image"  <!-- 设置图片资源 -->
    android:scaleType="centerCrop"    <!-- 设置缩放类型 -->
    android:adjustViewBounds="true"   <!-- 保持图片宽高比 -->
    android:contentDescription="@string/image_desc" /> <!-- 无障碍描述 -->

2. Java/Kotlin 中设置图片

java 复制代码
ImageView imageView = findViewById(R.id.imageView);

// 设置图片资源
imageView.setImageResource(R.drawable.my_image);

// 设置Bitmap
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.my_image);
imageView.setImageBitmap(bitmap);

// 设置Drawable
Drawable drawable = ContextCompat.getDrawable(this, R.drawable.my_image);
imageView.setImageDrawable(drawable);

二、图片缩放类型 (scaleType)

ImageView 提供了多种缩放方式:

scaleType 值 描述
center 不缩放,居中显示
centerCrop 等比例缩放,填充整个View,可能裁剪
centerInside 等比例缩放,完整显示在View内
fitCenter (默认) 等比例缩放,居中显示
fitStart 等比例缩放,顶部/左边对齐
fitEnd 等比例缩放,底部/右边对齐
fitXY 不等比缩放填满整个View
matrix 使用矩阵变换
java 复制代码
// 代码设置缩放类型
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

三、加载网络图片

1. 使用 Glide (推荐)

添加依赖:

gradle 复制代码
implementation 'com.github.bumptech.glide:glide:4.12.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'

使用示例:

java 复制代码
Glide.with(this)
    .load("https://example.com/image.jpg")
    .placeholder(R.drawable.placeholder)  // 加载中显示
    .error(R.drawable.error)             // 加载失败显示
    .centerCrop()                       // 缩放方式
    .into(imageView);

2. 使用 Picasso

gradle 复制代码
implementation 'com.squareup.picasso:picasso:2.8'
java 复制代码
Picasso.get()
    .load("https://example.com/image.jpg")
    .placeholder(R.drawable.placeholder)
    .error(R.drawable.error)
    .resize(300, 300)  // 调整大小
    .centerCrop()
    .into(imageView);

四、高级功能

1. 圆形图片

使用 Glide 实现圆形图片:

java 复制代码
Glide.with(this)
    .load(imageUrl)
    .apply(RequestOptions.circleCropTransform())
    .into(imageView);

2. 圆角图片

自定义圆角转换器:

java 复制代码
public class RoundedCornersTransformation extends BitmapTransformation {
    private final int radius;
    
    public RoundedCornersTransformation(int radius) {
        this.radius = radius;
    }
    
    // 实现转换逻辑...
}

// 使用
Glide.with(this)
    .load(imageUrl)
    .transform(new RoundedCornersTransformation(20))
    .into(imageView);

3. 图片点击缩放动画

java 复制代码
imageView.setOnClickListener(v -> {
    if (imageView.getScaleType() == ImageView.ScaleType.CENTER_CROP) {
        imageView.animate().scaleX(1.5f).scaleY(1.5f).setDuration(300).start();
    } else {
        imageView.animate().scaleX(1f).scaleY(1f).setDuration(300).start();
    }
});

五、性能优化

  1. 图片压缩

    java 复制代码
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 2; // 缩小为1/2
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.large_image, options);
    imageView.setImageBitmap(bitmap);
  2. 内存回收

    java 复制代码
    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 清除图片引用
        imageView.setImageDrawable(null);
    }
  3. 使用合适的图片格式

    • PNG:适合有透明度需求的图片
    • JPEG:适合照片类图片
    • WebP:更高效的现代格式

六、常见问题解决

  1. OOM(内存溢出)问题

    • 使用图片加载库(Glide/Picasso)
    • 加载适当尺寸的图片
    • 在滚动列表中使用暂停加载功能
  2. 图片变形问题

    • 确保设置正确的scaleType
    • 使用adjustViewBounds="true"
    • 保持图片原始宽高比
  3. 图片模糊问题

    • 提供足够高分辨率的图片
    • 避免过度缩放
    • 使用矢量图(SVG/VectorDrawable)替代位图

通过以上方法,可以充分利用ImageView展示各种图片,并确保良好的性能和用户体验。

相关推荐
DogDaoDao6 小时前
Android 硬件编码器参数完全指南:MediaCodec 深度解析
android·音视频·视频编解码·h264·硬编码·视频直播·mediacodec
二哈赛车手6 小时前
新人笔记---ApiFox的一些常见使用出错
java·笔记·spring
JohnnyDeng947 小时前
Android 自定义 View:Canvas 绘图与事件分发深度解析
android
栗子~~7 小时前
JAVA - 二层缓存设计(本地缓冲+redis缓冲+广播所有本地缓冲失效) demo
java·redis·缓存
YDS8297 小时前
DeepSeek RAG&MCP + Agent智能体项目 —— RAG知识库的搭建和接口实现
java·ai·springboot·agent·rag·deepseek
未若君雅裁9 小时前
MyBatis 一级缓存、二级缓存与清理机制
java·缓存·mybatis
AI人工智能+电脑小能手9 小时前
【大白话说Java面试题 第65题】【JVM篇】第25题:谈谈对 OOM 的认识
java·开发语言·jvm
阿维的博客日记9 小时前
Nacos 为什么能让配置动态生效?(涉及 @RefreshScope 注解)
java·spring
雨辰AI9 小时前
SpringBoot3 + 人大金仓读写分离 + 分库分表 + 集群高可用 全栈实战
java·数据库·mysql·政务
Android小码家10 小时前
Framework之Launcher小窗开发
android·framework·虚拟屏·小窗