AS imageview 代码 简单实现

本文详细介绍了一个使用RelativeLayout进行布局设计的示例,展示了如何通过XML定义布局,并利用Glide库从本地和网络加载图片到ImageView中。同时,文章还提供了完整的Java代码实现,包括Glide的配置和使用方法。

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

一、layout代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:padding="15dp"
android:background="#ff3300">
<ImageView
    android:id="@+id/iv_01"
    android:layout_width="300dp"
    android:layout_height="100dp"
    android:background="#eee000"
    android:src="@drawable/blackhole"
    android:scaleType="fitXY"
    />
<ImageView
    android:id="@+id/iv_02"
    android:layout_width="200dp"
    android:layout_height="100dp"
    android:background="#eee000"
    android:src="@drawable/blackhole"
    android:layout_below="@+id/iv_01"
    android:scaleType="fitCenter"
    android:layout_marginTop="10dp"
    />
<ImageView
    android:id="@+id/iv_06"
    android:layout_width="200dp"
    android:layout_height="100dp"
    android:background="#eee000"
    android:layout_below="@+id/iv_02"
    android:scaleType="fitCenter"
    android:layout_marginTop="10dp"
    />
</RelativeLayout>

二、java代码

package com.relativelayout;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ImageView;

import com.bumptech.glide.Glide;

public class image_view extends AppCompatActivity {
private ImageView iv_06;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_image_view);
    iv_06 = findViewById(R.id.iv_06);
    Glide.with(this).load("https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1585812265&di=694a14e7e63eb3835e588dbe7d067c37&src=http://a3.att.hudong.com/14/75/01300000164186121366756803686.jpg").into(iv_06);
}
}

三、运行结果
在这里插入图片描述
注意

图一、二为本地图片只需要将图片保存在drawable下引用即可。
图三为网络图片,需要依赖glide工具包,在build.gradle中添加工具包在这里插入图片描述
具体实现部分参照java代码。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值