Android封装ImageGetter接口

Html.ImageGetter接口实现类NetworkImageGetter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
public final class NetworkImageGetter implements Html.ImageGetter {

private static final String TAG = "yezhou";

private Context context;
private NetworkImageAsyncLoader loader;

public NetworkImageGetter(Context context, NetworkImageAsyncLoader loader) {
this.context = context;
this.loader = loader;
}

@Override
public Drawable getDrawable(String source) {
Drawable drawable = null;
// 封装路径
Log.i(TAG, "source: " + source);
String imageName = source.substring(source.lastIndexOf('/') + 1);
Log.i(TAG, "imageName: " + imageName);
File file = new File(EnvironmentUtil.getInstance().getRootDirectory(), "/images/" + imageName);
// 判断是否以http开头
if (source.startsWith("http")) {
// 判断路径是否存在
if (file.exists()) {
// 存在即获取drawable
drawable = Drawable.createFromPath(file.getAbsolutePath());
int dpWidth = drawable.getIntrinsicWidth();
int dpHeight = drawable.getIntrinsicHeight();
int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpWidth, context.getResources().getDisplayMetrics());
int heigth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpHeight, context.getResources().getDisplayMetrics());
drawable.setBounds(0, 0, width, heigth);
} else {
// 不存在即开启异步任务加载网络图片
loader.execute(source, imageName);
}
}
return drawable;
}
}

网络图片资源异步下载类NetworkImageAsyncLoader

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
public class NetworkImageAsyncLoader extends AsyncTask<String, Integer, Void> {

private static final String TAG = "yezhou";

private String imageName;

@Override
protected Void doInBackground(String... params) {
// 加载网络图片
loadNetPic(params[0]);
imageName = params[1];
return null;
}

@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// 图片加载完成回调
mOnNetworkImageLoader.onImageLoaded();
}

/**加载网络图片*/
private void loadNetPic(String... params) {
String urlStr = params[0];
File file = new File(Environment.getExternalStorageDirectory(), "/images/" + imageName);
InputStream in = null;
FileOutputStream out = null;
try {
URL url = new URL(urlStr);
HttpURLConnection connUrl = (HttpURLConnection) url.openConnection();
connUrl.setConnectTimeout(5000);
connUrl.setRequestMethod("GET");
if (connUrl.getResponseCode() == 200) {
in = connUrl.getInputStream();
out = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int len;
while ((len = in.read(buffer))!= -1) {
out.write(buffer, 0, len);
}
} else {
Log.i(TAG, connUrl.getResponseCode() + "");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if(in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

private OnNetworkImageLoader mOnNetworkImageLoader = null;

public void setOnNetworkImageLoader(OnNetworkImageLoader loader) {
mOnNetworkImageLoader = loader;
}

public interface OnNetworkImageLoader {
void onImageLoaded();
}
}

Activity调用:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class MainActivity extends AppCompatActivity implements NetworkImageAsyncLoader.OnNetworkImageLoader {

private NetworkImageGetter mImageGetter;
private NetworkImageAsyncLoader mImageLoader;

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

mImageLoader = new NetworkImageAsyncLoader();
mImageGetter = new NetworkImageGetter(this, mImageLoader);
mImageLoader.setOnNetworkImageLoader(this);
}

private void updateData() {
mTvNewsContent.setText(Html.fromHtml(mNews.getContent(), mImageGetter, null));
}

@Override
public void onImageLoaded() {
// 当执行完成后再次为其设置一次
mTvNewsContent.setText(Html.fromHtml(mNews.getContent(), mImageGetter, null));
}

}

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :