Android——Glide加载网络图片

Glide是谷歌开发的图片加载框架,也是Android官方推荐的图片加载框架。

使用Glide之前需要先导入glide库:implementation 'com.github.bumptech.glide:glide:4.13.0‘

Glide的用法很简单,只需一行代码即可将网络图片加载到图像视图界面:Glide.with(活动实例).load(网址字符串).into(图像视图);

ifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.myapplication">

    <!-- 互联网 -->
    <uses-permission android:name="android.permission.INTERNET" />
    <!-- 存储卡 -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MyApplication"
        tools:targetApi="31">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="5dp" >

    <CheckBox
        android:id="@+id/ck_fitxy"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:gravity="left|center"
        android:text="是否填满整个视图"
        android:textColor="#000000"
        android:textSize="17sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="显示方式:"
            android:textColor="@color/black"
            android:textSize="17sp" />

        <Spinner
            android:id="@+id/sp_show_mode"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:spinnerMode="dialog" />

    </LinearLayout>

    <ImageView
        android:id="@+id/iv_network"
        android:layout_width="match_parent"
        android:layout_height="200dp" />
</LinearLayout>

activity:

package com.example.myapplication;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.Spinner;
import com.bumptech.glide.Glide;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity
{
    private ImageView iv_network;

    private String mImageUrl = "https://img0.baidu.com/it/u=1305074959,4101321827&fm=253&fmt=auto&app=120&f=JPEG?w=1280&h=800";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        iv_network = findViewById(R.id.iv_network);

        CheckBox ck_fitxy = findViewById(R.id.ck_fitxy);
        ck_fitxy.setOnCheckedChangeListener((buttonView, isChecked) -> {
            // 设置图像视图的伸展类型
            iv_network.setScaleType(isChecked ? ImageView.ScaleType.FIT_XY : ImageView.ScaleType.FIT_CENTER);
        });

        initModeSpinner(); // 初始化显示方式的下拉框
    }

    // 初始化显示方式的下拉框
    private void initModeSpinner() {
        ArrayAdapter<String> modeAdapter = new ArrayAdapter<String>(this,
                R.layout.item_select, modeArray);
        Spinner sp_show_mode = findViewById(R.id.sp_show_mode);
        sp_show_mode.setPrompt("请选择显示方式");
        sp_show_mode.setAdapter(modeAdapter);
        sp_show_mode.setSelection(0);
        sp_show_mode.setOnItemSelectedListener(new ModeSelectedListener());
    }

    private String[] modeArray = {"默认", "容纳居中fitCenter", "居中剪裁centerCrop", "居中入内centerInside", "圆形剪裁circleCrop"};

    class ModeSelectedListener implements AdapterView.OnItemSelectedListener {
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            showNetworkImage(arg2); // 加载并显示网络图片
        }

        public void onNothingSelected(AdapterView<?> arg0) {
        }
    }

    // 加载并显示网络图片
    private void showNetworkImage(int show_mode) {
        if (show_mode == 0) { // 使用图像视图默认的显示方式
            Glide.with(this).load(mImageUrl).into(iv_network);
        } else if (show_mode == 1) { // 显示方式为容纳居中fitCenter
            Glide.with(this).load(mImageUrl).fitCenter().into(iv_network);
        } else if (show_mode == 2) { // 显示方式为居中剪裁centerCrop
            Glide.with(this).load(mImageUrl).centerCrop().into(iv_network);
        } else if (show_mode == 3) { // 显示方式为居中入内centerInside
            Glide.with(this).load(mImageUrl).centerInside().into(iv_network);
        } else if (show_mode == 4) { // 显示方式为圆形剪裁circleCrop
            Glide.with(this).load(mImageUrl).circleCrop().into(iv_network);
        }
    }

}

  • 10
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android使用 Glide 加载网络视频,你可以按照以下步骤进行操作: 1. 首先,在你的 Android 项目中添加 Glide 的依赖。你可以在项目的 `build.gradle` 文件中的 `dependencies` 块中添加以下代码: ```groovy implementation 'com.github.bumptech.glide:glide:4.12.0' annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0' ``` 2. 确保你已经在 AndroidManifest.xml 文件中添加了网络权限: ```xml <uses-permission android:name="android.permission.INTERNET" /> ``` 3. 在你需要加载网络视频的地方,使用 Glide 的 `VideoViewTarget` 类来加载视频。首先,导入 Glide 相关的类: ```java import com.bumptech.glide.Glide; import com.bumptech.glide.request.RequestOptions; import com.bumptech.glide.request.target.Target; import com.bumptech.glide.request.target.ViewTarget; import com.bumptech.glide.request.transition.Transition; import com.bumptech.glide.request.transition.TransitionFactory; ``` 4. 然后,使用以下代码加载视频: ```java String videoUrl = "Your video URL"; Glide.with(context) .load(videoUrl) .apply(RequestOptions.noTransformation()) .into(new ViewTarget<View, Drawable>(yourVideoView) { @Override public void onResourceReady(@NonNull Drawable resource, Transition<? super Drawable> transition) { if (resource instanceof GifDrawable) { GifDrawable gifDrawable = (GifDrawable) resource; gifDrawable.setLoopCount(GifDrawable.LOOP_FOREVER); gifDrawable.start(); } else if (resource instanceof BitmapDrawable) { BitmapDrawable bitmapDrawable = (BitmapDrawable) resource; Bitmap bitmap = bitmapDrawable.getBitmap(); // Do something with the bitmap } } }); ``` 在上面的代码中,将 "Your video URL" 替换为你要加载网络视频的 URL,同时将 `yourVideoView` 替换为要显示视频的 `VideoView` 或 `SurfaceView`。 这样,使用 Glide 加载网络视频就完成了。请注意,Glide 也可以加载其他类型的图片资源和动画资源。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值