最近学了一下Glide,觉得不错,自己试着写了demo测了些东西,在此分享出来。
着重看一下最后的说明!!!
这里我只着重说一下Glide获取图片宽高以及展示。详情的其他内容,推荐大家看一位大神的博客
http://blog.csdn.net/guolin_blog/article/details/53759439
获取到图片宽高,按比例展示图片,请参考我之前写的,基于XUtils的一个博客,拿到宽高后,其他的计算,都是同理的
http://blog.csdn.net/u014620028/article/details/52874646
我这里用的是Glide 4.2.0版本
1、添加依赖
compile 'com.github.bumptech.glide:glide:4.2.0'
2、加载网络图片,肯定是需要网络权限的
<uses-permission android:name="android.permission.INTERNET" />
3、布局
布局很简单,就是一个按钮,点击后去加载图片,
<?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:orientation="vertical"
>
<TextView
android:id="@+id/tv_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
android:background="#55ff0000"
android:padding="5dp"
android:text="加载图片"
android:textSize="25sp"
/>
<ImageView
android:id="@+id/iv_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"/>
</LinearLayout>
4、Activity中的代码
package com.chen.demo;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
public class MainActivity_4 extends Activity {
private TextView tv_4;
private ImageView iv_4;
private String imgUrl = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_4);
tv_4 = findViewById(R.id.tv_4);
iv_4 = findViewById(R.id.iv_4);
imgUrl = "http://pic39.nipic.com/20140312/10600816_105229143000_2.jpg";
tv_4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Glide.with(MainActivity_4.this)
.load(imgUrl)
.into(new SimpleTarget<Drawable>() {
@Override
public void onResourceReady(Drawable resource, Transition<? super Drawable> transition) {
int width = resource.getIntrinsicWidth();
int height = resource.getIntrinsicHeight();
Log.e("width", width + "");
Log.e("height", height + "");
iv_4.setImageDrawable(resource);
}
});
}
});
}
}
日志打印是:
10-27 11:56:57.094 15637-15637/com.chen.demo E/width: 1024
10-27 11:56:57.094 15637-15637/com.chen.demo E/height: 640
我把网络图片另存到电脑上,显示属性:
由此可以看出,改方法可以获取到图片的真实宽高。
说明:
1、我这里用的是Glide的4.2.0版本,如果用之前的版本,如:3.7.0版本
这个时候,into方法中的方法就变了,是如下方法:
Glide.with(at)
.load(iconUrl)
.asBitmap()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.placeholder(R.mipmap.loading)
.error(R.mipmap.loadfailed)
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
});
2、无意中发现,ImageView的setImageDrawable和setImageBitmap方法的区别
看一下源码:
先看setImageBitmap的源码:
/**
* Sets a Bitmap as the content of this ImageView.
*
* @param bm The bitmap to set
*/
@android.view.RemotableViewMethod
public void setImageBitmap(Bitmap bm) {
// Hacky fix to force setImageDrawable to do a full setImageDrawable
// instead of doing an object reference comparison
mDrawable = null;
if (mRecycleableBitmapDrawable == null) {
mRecycleableBitmapDrawable = new ImageViewBitmapDrawable(
mContext.getResources(), bm);
} else {
mRecycleableBitmapDrawable.setBitmap(bm);
}
setImageDrawable(mRecycleableBitmapDrawable);
}
在这个方法下,有setImageDrawable,点进去再看:
/**
* Sets a drawable as the content of this ImageView.
*
* @param drawable the Drawable to set, or {@code null} to clear the
* content
*/
public void setImageDrawable(@Nullable Drawable drawable) {
if (mDrawable != drawable) {
mResource = 0;
mUri = null;
final int oldWidth = mDrawableWidth;
final int oldHeight = mDrawableHeight;
updateDrawable(drawable);
if (oldWidth != mDrawableWidth || oldHeight != mDrawableHeight) {
requestLayout();
}
invalidate();
}
}
现在,我们再直接看一下ImageView的setImageDrawable的源码:
/**
* Sets a drawable as the content of this ImageView.
*
* @param drawable the Drawable to set, or {@code null} to clear the
* content
*/
public void setImageDrawable(@Nullable Drawable drawable) {
if (mDrawable != drawable) {
mResource = 0;
mUri = null;
final int oldWidth = mDrawableWidth;
final int oldHeight = mDrawableHeight;
updateDrawable(drawable);
if (oldWidth != mDrawableWidth || oldHeight != mDrawableHeight) {
requestLayout();
}
invalidate();
}
}
看到了吧,setImageDrawable是一步到位的加载图片的。