Android--Glide的基本使用

具体的可以看Glide的文档(有中文文档喔)
Glide


依赖:

  implementation 'com.github.bumptech.glide:glide:4.11.0'
  annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'

有一点要注意的就是,高版本不支持http明文。所以地址要写https

简单使用,点击按钮,执行各种方法。例子:


    //加载网络图片
    public void one(View view) {
        Glide.with(this).load("https://seopic.699pic.com/photo/50008/9194.jpg_wh1200.jpg").into(imageView);
    }

    //加载资源文件
    public void two(View view){
        Glide.with(this).load(R.drawable.jd).into(imageView);
    }

    //加载本地图片(SD卡)
    public void three(View view){
        String path= Environment.getExternalStorageDirectory()+"a.jpg";
        File file=new File(path);
        Uri uri=Uri.fromFile(file);
        Glide.with(this).load(uri).into(imageView);
    }

    //加载网络gif
    public void four(View view){
        String path="https://c-ssl.duitang.com/uploads/item/201410/23/20141023144603_8Mfxm.gif";
        //第三个是占位符,没加载过来之前先显示R.mipmap.ic_launcher
        Glide.with(this).load(path).placeholder(R.mipmap.ic_launcher).into(imageView);
    }

    //加载资源gif
    public void five(View view){
        Glide.with(this).load(R.drawable.a).into(imageView);
    }

    //加载本地gif
    public void six(View view){
        String path=Environment.getExternalStorageDirectory()+"a.gif";
        File file=new File(path);
        Glide.with(this).load(file).into(imageView);
    }

    //加载本地的小视频和快照
    public void seven(View view){
        String path=Environment.getExternalStorageDirectory()+"a.mp4";
        File videoFile=new File(path);
        Glide.with(this).load(Uri.fromFile(videoFile)).placeholder(R.mipmap.ic_launcher).into(imageView);
    }

    //设置缩略图比例,然后先加载缩略图,再加载原图
    public void eight(View view){
        String path="https://seopic.699pic.com/photo/50157/0224.jpg_wh1200.jpg";
        Glide.with(this).load(path).thumbnail(0.1f).centerCrop().placeholder(R.mipmap.ic_launcher).into(imageView);
    }

    //先建立一个缩略图对象,然后加载缩略图,再加载原图
    public void nine(View view){
        String path="https://seopic.699pic.com/photo/50157/0220.jpg_wh1200.jpg";
        String path2="https://seopic.699pic.com/photo/50157/0224.jpg_wh1200.jpg";
        RequestBuilder<Drawable> load = Glide.with(this).load(path2);
        Glide.with(this).load(path).thumbnail(load).centerCrop().placeholder(R.mipmap.ic_launcher).into(imageView);

    }

和Recyclerview的使用:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {


    private Context context;

    public MyAdapter(Context context) {
        this.context = context;
    }

    @NonNull
    @Override
    public MyAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view=View.inflate(context, R.layout.glide_item, null);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyAdapter.MyViewHolder holder, int position) {

        Glide.with(context).load(ImagePath.path[position]).into(holder.iv_item);
    }

    @Override
    public int getItemCount() {
        return ImagePath.path.length;
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {

        private ImageView iv_item;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            iv_item=(ImageView)itemView.findViewById(R.id.iv_item);
        }
    }
}

public class ImagePath {

    public static String[] path={
            "https://seopic.699pic.com/photo/50008/9194.jpg_wh1200.jpg",
            "https://c-ssl.duitang.com/uploads/item/201410/23/20141023144603_8Mfxm.gif",
            "https://seopic.699pic.com/photo/50157/0224.jpg_wh1200.jpg"
    };
}

public class MyGlideRecycler extends AppCompatActivity {

    private RecyclerView recyclerView;
    private MyAdapter adapter;

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

    }
    public void initData(View view) {
        recyclerView=(RecyclerView)findViewById(R.id.rv_glide);
        adapter=new MyAdapter(this);
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值