Skip to content

Commit

Permalink
封装Glide图片库
Browse files Browse the repository at this point in the history
  • Loading branch information
sunfusheng committed Feb 10, 2017
1 parent 146c2f5 commit 9a6aba8
Show file tree
Hide file tree
Showing 14 changed files with 246 additions and 182 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import android.view.LayoutInflater;
import android.widget.BaseAdapter;

import com.sunfusheng.StickyHeaderListView.manager.ImageManager;

import java.util.ArrayList;
import java.util.List;

Expand All @@ -14,12 +12,10 @@ public abstract class BaseListAdapter<E> extends BaseAdapter {
private List<E> mList = new ArrayList<E>();
protected Context mContext;
protected LayoutInflater mInflater;
protected ImageManager mImageManager;

public BaseListAdapter(Context context) {
mContext = context;
mInflater = LayoutInflater.from(context);
mImageManager = new ImageManager(context);
}

public BaseListAdapter(Context context, List<E> list) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.sunfusheng.StickyHeaderListView.R;
import com.sunfusheng.StickyHeaderListView.model.ChannelEntity;
import com.sunfusheng.StickyHeaderListView.view.GildeImageView.GlideImageView;

import java.util.List;

Expand Down Expand Up @@ -38,7 +38,8 @@ public View getView(int position, View convertView, ViewGroup parent) {
ChannelEntity entity = getItem(position);

holder.tvTitle.setText(entity.getTitle());
mImageManager.loadCircleImage(entity.getImage_url(), holder.ivImage);
holder.givImage.loadNetCircleImage(entity.getImage_url(), R.color.font_black_6);

if (TextUtils.isEmpty(entity.getTips())) {
holder.tvTips.setVisibility(View.INVISIBLE);
} else {
Expand All @@ -50,8 +51,8 @@ public View getView(int position, View convertView, ViewGroup parent) {
}

static class ViewHolder {
@BindView(R.id.iv_image)
ImageView ivImage;
@BindView(R.id.giv_image)
GlideImageView givImage;
@BindView(R.id.tv_title)
TextView tvTitle;
@BindView(R.id.tv_tips)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.sunfusheng.StickyHeaderListView.R;
import com.sunfusheng.StickyHeaderListView.model.OperationEntity;
import com.sunfusheng.StickyHeaderListView.view.GildeImageView.GlideImageView;

import java.util.List;

Expand Down Expand Up @@ -42,7 +42,8 @@ public View getView(int position, View convertView, ViewGroup parent) {
OperationEntity entity = getItem(position);

holder.tvTitle.setText(entity.getTitle());
mImageManager.loadUrlImage(entity.getImage_url(), holder.ivImage);
holder.givImage.loadNetImage(entity.getImage_url(), R.color.font_black_6);

if (TextUtils.isEmpty(entity.getSubtitle())) {
holder.tvSubtitle.setVisibility(View.INVISIBLE);
} else {
Expand All @@ -54,8 +55,8 @@ public View getView(int position, View convertView, ViewGroup parent) {
}

static class ViewHolder {
@BindView(R.id.iv_image)
ImageView ivImage;
@BindView(R.id.giv_image)
GlideImageView givImage;
@BindView(R.id.tv_title)
TextView tvTitle;
@BindView(R.id.tv_subtitle)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sunfusheng.StickyHeaderListView.R;
import com.sunfusheng.StickyHeaderListView.model.TravelingEntity;
import com.sunfusheng.StickyHeaderListView.util.ToastUtil;
import com.sunfusheng.StickyHeaderListView.view.GildeImageView.GlideImageView;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -99,7 +99,7 @@ public View getView(int position, View convertView, ViewGroup parent) {
final String title = entity.getFrom() + entity.getTitle() + entity.getType();
holder.tvTitle.setText(title);
holder.tvRank.setText("排名:" + entity.getRank());
mImageManager.loadUrlImage(entity.getImage_url(), holder.ivImage);
holder.givImage.loadNetImage(entity.getImage_url(), R.color.font_black_6);

convertView.setOnClickListener(new View.OnClickListener() {
@Override
Expand All @@ -114,8 +114,8 @@ public void onClick(View v) {
static class ViewHolder {
@BindView(R.id.ll_root_view)
LinearLayout llRootView;
@BindView(R.id.iv_image)
ImageView ivImage;
@BindView(R.id.giv_image)
GlideImageView givImage;
@BindView(R.id.tv_title)
TextView tvTitle;
@BindView(R.id.tv_rank)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.sunfusheng.StickyHeaderListView.view;
package com.sunfusheng.StickyHeaderListView.view.GildeImageView;

import android.content.Context;
import android.graphics.Bitmap;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package com.sunfusheng.StickyHeaderListView.view.GildeImageView;

import android.net.Uri;
import android.support.annotation.IdRes;
import android.text.TextUtils;
import android.widget.ImageView;

import com.bumptech.glide.DrawableRequestBuilder;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;

/**
* Created by sunfusheng on 2017/1/23.
*/
public class GlideImageLoader {

public static final String ANDROID_RESOURCE = "android.resource://";
public static final String SEPARATOR = "/";
private ImageView mImageView;

public GlideImageLoader(ImageView view) {
mImageView = view;
}

// 将资源ID转为Uri
public Uri resIdToUri(int resourceId) {
return Uri.parse(ANDROID_RESOURCE + mImageView.getContext().getPackageName() + SEPARATOR + resourceId);
}

// 加载网络图片
public void loadNetImage(String url, int holderResId) {
urlBuilder(url, holderResId).into(mImageView);
}

// 加载drawable图片
public void loadResImage(@IdRes int resId, int holderResId) {
resBuilder(resId, holderResId).into(mImageView);
}

// 加载本地图片
public void loadLocalPathImage(String path, int holderResId) {
urlBuilder("file://" + path, holderResId).into(mImageView);
}

// 加载网络圆型图片
public void loadNetCircleImage(String url, int holderResId) {
urlBuilder(url, holderResId)
.transform(new GlideCircleTransform(mImageView.getContext()))
.into(mImageView);
}

// 加载drawable圆型图片
public void loadLocalResCircleImage(int resId, int holderResId) {
resBuilder(resId, holderResId)
.transform(new GlideCircleTransform(mImageView.getContext()))
.into(mImageView);
}

// 加载本地圆型图片
public void loadLocalPathCircleImage(String path, int holderResId) {
urlBuilder("file://" + path, holderResId)
.transform(new GlideCircleTransform(mImageView.getContext()))
.into(mImageView);
}

// 创建 Res DrawableRequestBuilder
public DrawableRequestBuilder resBuilder(int resId, int holderResId) {
return uriBuilder(resIdToUri(resId), holderResId);
}

// 创建 Uri DrawableRequestBuilder
public DrawableRequestBuilder uriBuilder(Uri uri, int holderResId) {
return Glide.with(mImageView.getContext())
.load(uri)
.crossFade()
.dontAnimate()
.fallback(holderResId)
.error(holderResId)
.diskCacheStrategy(DiskCacheStrategy.SOURCE);
}

// 创建 Url DrawableRequestBuilder
public DrawableRequestBuilder urlBuilder(final String url, int holderResId) {
if (GlideManager.getInstance().isRequestFailedUrl(url)) {
return resBuilder(holderResId, holderResId);
}
return Glide.with(mImageView.getContext())
.load(url)
.crossFade()
.dontAnimate()
.fallback(holderResId)
.error(holderResId)
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.listener(new RequestListener<String, GlideDrawable>() {
@Override
public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
GlideManager.getInstance().putRequestFailedUrl(url);
return false;
}

@Override
public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
return false;
}
});
}

public boolean isGif(String url) {
if (TextUtils.isEmpty(url)) return false;
if (url.endsWith(".gif")) return true;
return false;
}
}
Loading

0 comments on commit 9a6aba8

Please sign in to comment.