ImageLoader简单使用

2023-11-04

1.导包

 implementation 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'

2.权限

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


    <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:name=".MyApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

3.初始化

package com.example.imageloader2;

import android.app.Application;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        ImageLoaderConfiguration configuration = ImageLoaderConfiguration.createDefault(this);
        ImageLoader.getInstance().init(configuration);
    }
}

4.使用
注意:要先申请存储权限,否则可能出现图片下载失败

package com.example.imageloader2;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.GridView;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.Arrays;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = MainActivity.class.getSimpleName();
    private GridView mGridView;
    private ImageAdapter mImageAdapter;
    private String[] mImages = new String[]{
            "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2477101223,88513767&fm=26&gp=0.jpg",
            "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2477101223,88513767&fm=26&gp=0.jpg",
            "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2477101223,88513767&fm=26&gp=0.jpg",
            "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2477101223,88513767&fm=26&gp=0.jpg",
            "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2477101223,88513767&fm=26&gp=0.jpg",
            "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2477101223,88513767&fm=26&gp=0.jpg",
            "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2477101223,88513767&fm=26&gp=0.jpg",
            "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2477101223,88513767&fm=26&gp=0.jpg",
            "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2477101223,88513767&fm=26&gp=0.jpg",
            "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2477101223,88513767&fm=26&gp=0.jpg",
            "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2477101223,88513767&fm=26&gp=0.jpg",
            "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2477101223,88513767&fm=26&gp=0.jpg",
            "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2477101223,88513767&fm=26&gp=0.jpg",
            "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2477101223,88513767&fm=26&gp=0.jpg",
            "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2477101223,88513767&fm=26&gp=0.jpg",
            "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2477101223,88513767&fm=26&gp=0.jpg",
            "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2477101223,88513767&fm=26&gp=0.jpg",
            "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2477101223,88513767&fm=26&gp=0.jpg",
            "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2477101223,88513767&fm=26&gp=0.jpg",
            "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2477101223,88513767&fm=26&gp=0.jpg",
            "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2477101223,88513767&fm=26&gp=0.jpg",
            "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2477101223,88513767&fm=26&gp=0.jpg",
            "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2477101223,88513767&fm=26&gp=0.jpg",
            "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2477101223,88513767&fm=26&gp=0.jpg",
            "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2477101223,88513767&fm=26&gp=0.jpg",
            "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2477101223,88513767&fm=26&gp=0.jpg",
            "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2477101223,88513767&fm=26&gp=0.jpg"
    };

    private List<String> mImageList;
    private boolean isGridViewIdle = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mImageList = Arrays.asList(mImages);
        mImageAdapter = new ImageAdapter(this, mImageList);
        mGridView = (GridView)findViewById(R.id.grid_view);
        mGridView.setOnScrollListener(new AbsListView.OnScrollListener(){

            @Override
            public void onScrollStateChanged(AbsListView absListView, int i) {
                if(i == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
                    isGridViewIdle = true;
                    mImageAdapter.notifyDataSetChanged();
                }else {
                    isGridViewIdle = false;
                }
            }

            @Override
            public void onScroll(AbsListView absListView, int i, int i1, int i2) {

            }
        });
        mGridView.setAdapter(mImageAdapter);
        //确认存储权限
        boolean exit = checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
        if(!exit) {
            requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        boolean permit = true;
        for(int result : grantResults){
            if(result == -1) {
                permit = false;
                break;
            }
        }
        Log.d(TAG, "permit-:"+permit);
        if(permit) {
            mImageAdapter.notifyDataSetChanged();
        }
    }


    class ImageAdapter extends BaseAdapter {
        private Context mContext;
        private List mImages;

        public ImageAdapter(Context mContext, List mImages) {
            this.mContext = mContext;
            this.mImages = mImages;
        }

        @Override
        public int getCount() {
            return mImages.size();
        }

        @Override
        public Object getItem(int i) {
            return mImages.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ViewHolder holder = null;
            if(view == null) {
                view = getLayoutInflater().inflate(R.layout.image_item, null);
                holder = new ViewHolder();
                holder.imageView = view.findViewById(R.id.square_item);
                view.setTag(holder);
            }else {
                holder = (ViewHolder) view.getTag();
            }
            final String uri = (String) getItem(i);
            ImageView imageView = holder.imageView;

            /*final String tag = (String) holder.imageView.getTag();
            if(uri.equals(tag)) {
                imageView.setImageResource(R.drawable.ic_launcher_background);
            }
            if(isGridViewIdle) {
                imageView.setTag(uri);
                ImageLoader.getInstance().displayImage(uri, imageView);
            }*/

            if(isGridViewIdle) {
                ImageLoader.getInstance().displayImage(uri, imageView);
            }
            return view;
        }

        class ViewHolder {
            SquareImageView imageView;
        }
    }
}

5.布局

  1. activity_main.xml

     <?xml version="1.0" encoding="utf-8"?>
    
     <LinearLayout 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"
         tools:context=".MainActivity"
         android:orientation="vertical">
     
         <GridView
             android:id="@+id/grid_view"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:gravity="center"
             android:horizontalSpacing="5dp"
             android:verticalSpacing="5dp"
             android:listSelector="@android:color/transparent"
             android:numColumns="3"
             android:stretchMode="columnWidth"
             android:visibility="visible"/>
     
     </LinearLayout>
    
  2. image_item.xml

     <?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:gravity="center">
     
         <com.example.imageloader2.SquareImageView
             android:id="@+id/square_item"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:scaleType="centerCrop"
             android:src="@mipmap/ic_launcher"/>
     
     </LinearLayout>
    

6.自定义宽高相同的ImageView

package com.example.imageloader2;

import android.content.Context;
import android.util.AttributeSet;
import androidx.annotation.Nullable;

public class SquareImageView extends androidx.appcompat.widget.AppCompatImageView {
    public SquareImageView(Context context) {
        super(context);
    }

    public SquareImageView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public SquareImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, widthMeasureSpec);
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ImageLoader简单使用 的相关文章

随机推荐

  • 【C++11新特性】auto、范围for语句、nullptr

    文章目录 1 auto 2 范围for语句 2 1 遍历数组 2 2 遍历字符串 2 3 遍历STL容器 3 nullptr 1 auto auto 可以在声明变量的时候根据变量初始值的类型自动为此变量选择匹配的类型 也就是说 声明时必须要
  • jenkins war版的在Linux下的安装与卸载

    文章目录 目录 文章目录 前言 一 安装 二 卸载 总结 前言 Jenkins是一个开源软件项目 是基于Java开发的一种持续集成工具 用于监控持续重复的工作 集成 该软件可以集成其他软件 来完成相应的功能 一 安装 因为jenkins本身
  • 【实习】vue input下拉及搜索功能

    一个需求input实现下拉及搜索 实习练手不让用ui模板 首先百度得出2种方法 一个是input select 一个是input datalist input组合select改css写js 最后bug太多放弃了 input datalist
  • 突破神奇的Cloudflare防火墙

    背景 最近碰到一个神奇的网站 在浏览器可以打开 但是通过 curl 或者 代码访问就直接 403 我估摸着这肯定是做了UA校验 于是请求的时候把浏览器的 UA 给带上 然后访问发现还是 403 不过这也难不倒我 肯定是还有校验其它的请求头
  • 双线性卷积神经网络_全卷积网络Fully Convolutional Networks (FCN)实战

    全卷积网络Fully Convolutional Networks FCN 实战 使用图像中的每个像素进行类别预测的语义分割 全卷积网络 FCN 使用卷积神经网络将图像像素转换为像素类别 与之前介绍的卷积神经网络不同 FCN通过转置卷积层将
  • MOS管烧毁,90%以上的硬件工程师都会遇到的问题!

    MOS管烧毁 我相信90 以上的硬件工程师在职场生涯中都会遇到这类问题 然而这类问题也总是让人防不胜防 那么今天小白就给大家讲解一下MOS管烧毁的几个常见原因 在讲解前 小白给大家画一下MOS管的等效模型 以我最熟悉的N MOS管举例 给大
  • Thinkphp6.0框架远程调试配置

    首先需要安装think socketlog扩展 composer require topthink think socketlog 只需要在log php配置文件中加入如下配置 默认日志记录通道 记得在env中配置参数 default gt
  • type-aliases-package的用法

    type aliases package作用 在Mybatis的mapper xml文件中resultType的type或者paramterType会返回自定义entity 此时可以用全类名名来指定这些实体 举例
  • facechain环境部署

    环境安装 创建虚拟环境facechain conda create n facechain python 3 8 conda activate facechain 克隆 GIT LFS SKIP SMUDGE 1 git clone htt
  • SpringBoot入门篇--对于JSON数据的返回以及处理一

    在后台的开发过程中不可避免的就是一系列对JSON数据的返回 需要我们进行的就是提供各种各样的数据 一般情况下数据类型最常用的就是JSON以及XML 在这里我们就讲讲在SpringBoot里面我们怎样进行JSON数据的返回以及数据一些特殊情况
  • Ubuntu18.04安装indicator-sysmonitor显示实时网速

    系统环境 Ubuntu18 04 6 LTS 1 添加源 sudo add apt repository ppa fossfreedom indicator sysmonitor 2 更新源 sudo apt get update 3 安装
  • 最长公共子序列(输出公共序列)

    给出两个字符串A B 求A与B的最长公共子序列 子序列不要求是连续的 比如两个串为 abcicba abdkscab ab是两个串的子序列 abc也是 abca也是 其中abca是这两个字符串最长的子序列 Input 第1行 字符串A 第2
  • python 基础篇 day 4 选择结构—— if 结构

    文章目录 if 基础结构 单 if 语句 if else 语句 if elif else 语句 嵌套的 if 语句 if 进阶用法 使用比较运算符 使用逻辑运算符 使用 in 关键字 range 函数 使用 is 关键字 使用 pass 语
  • 【数据库管理】十分钟了解啥是三级封锁协议、X锁和S锁

    一 为什么要用锁 在多用户共享系统中 许多事务可能同时对同一数据进行操作 称为 并发操作 此时数据库管理系统的并发控制子系统负责协调并发事务的执行 保证数据库的完整性不受破坏 同时避免用户得到不正确的数据 如果并发不受控制 则数据库的多个事
  • [HNOI2019]校园旅行(生成树+二分图+DP+回文性质)

    神仙好题啊 膜拜出题者 洛谷题目传送门 题目描述 给定一张无向图 每个点有权值v 0或1 q次询问 每次给出两个点 判断两点间是不是有一条路径 使得路径上经过的点的权值构成一个回文串 每个点 每条边可以重复走 解题思路 30pts 我们形式
  • 信息安全技术 信息安全风险评估规范

    风险处理计划 对不可接受的风险应根据导致该风险的脆弱性制定风险处理计划 风险处理计划中明确应采取的弥 补弱点的安全措施 预期效果 实施条件 进度安排 责任部门等 安全措施的选择应从管理与技术两 个方面考虑 安全措施的选择与实施应参照信息安全
  • Figma实用小技巧

    1 介绍 原作 草帽 视频地址 文字版整理 岩鸣杨子 2 快速拖拽调整数值 按住option可在数值上拖拽调整 不按option时只能在X Y W H 旋转 圆角等的图标上拖拽调整 不能调整透明度的数值 3 属性粘贴复制 shift X 互
  • java web选择题_Servlet、JSP选择题

    Java EE软件工程师认证考试 试题库 选择题 一 选择题 包括单选和双选 1 A 对于B S架构的工作流程 以下说法正确的是 选择一项 A 浏览器接受用户的输入 gt 浏览器向服务器端发送请求 gt 服务器端数据处理 gt 响应数据至客
  • shell命令:在windows的git bash执行ln -s 无效

    在windows的git bash执行ln s 无效 网上的方法试过 但是没用 仍然是创建文件及目录的拷贝而不是创建软链接 windows n WINDIR function common link if windows then echo
  • ImageLoader简单使用

    1 导包 implementation com nostra13 universalimageloader universal image loader 1 9 5 2 权限