一、添加Glide依赖
compile 'com.github.bumptech.glide:glide:3.7.0'
1、看看实现总代码
// 下面代码写在activity中
iv_1 = (ImageView) findViewById(R.id.iv_1)
Glide.with(this)
.load(imgUrl)
.transform(new GlideCircleTransform(this))
.placeholder(R.drawable.img1)
.animate(mAnimator)
.into(iv_1)
// 由于glide具有缓存作用,当再次进入界面,如果没有下面的代码,就不会旋转了
iv_1.startAnimation(mAnimation)
public DrawableRequestBuilder<ModelType> transform(BitmapTransformation... transformations) {
return bitmapTransform(transformations);
}
- 可以看到需要传入的类型为BitmapTransformation,下面我们实现GlideCircleTransform类
public class GlideCircleTransform extends BitmapTransformation {
public GlideCircleTransform(Context context) {
super(context);
}
@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
return circleCrop(pool, toTransform);
}
private static Bitmap circleCrop(BitmapPool pool, Bitmap source) {
if (source == null) return null;
int size = Math.min(source.getWidth(), source.getHeight());
int x = (source.getWidth() - size) / 2;
int y = (source.getHeight() - size) / 2;
Bitmap squared = Bitmap.createBitmap(source, x, y, size, size);
Bitmap result = pool.get(size, size, Bitmap.Config.ARGB_8888);
if (result == null) {
result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
}
Canvas canvas = new Canvas(result);
Paint paint = new Paint();
paint.setShader(new BitmapShader(squared, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
paint.setAntiAlias(true);
double r = (double) size / 2;
canvas.drawCircle((float) r, (float)r, (float)r, paint);
return result;
}
@Override
public String getId() {
return getClass().getName();
}
}
该类实现了将图片转化成圆形的功能。
2、Animator 图片动画的实现
public DrawableRequestBuilder<ModelType> animate(ViewPropertyAnimation.Animator animator) {
super.animate(animator);
return this;
}
- 可以看到需要传入Animator类型对象,下面看Animator对象实现
private ViewPropertyAnimation.Animator mAnimator = new ViewPropertyAnimation.Animator() {
@Override
public void animate(View view) {
Log.i("lvjie","Animator-->animate()...");
view.startAnimation(mAnimation);
}
};
private Animation mAnimation;
mAnimation = AnimationUtils.loadAnimation(this, R.anim.img_animation);
LinearInterpolator lin = new LinearInterpolator();
mAnimation.setInterpolator(lin);
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:duration="12000"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="-1"
android:repeatMode="restart"
android:toDegrees="360" />
</set>
完成以上两大步骤,基本上就能完成使用glide加载图片并实现圆形动态旋转功能了。
3、下面附上activity中的完整代码
public class GlideDemoActivity extends AppCompatActivity {
private String imgUrl = "http://r.v1.e.101.com/s/p/1016/847ed000400d4de098f434207ceeb099.jpg";
private ImageView iv_1;
private Animation mAnimation;
public static void startActivity(Context context){
Intent intent = new Intent(context, GlideDemoActivity.class);
context.startActivity(intent);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_glide_demo);
initView();
}
private void initView(){
mAnimation = AnimationUtils.loadAnimation(this, R.anim.img_animation);
LinearInterpolator lin = new LinearInterpolator();
mAnimation.setInterpolator(lin);
iv_1 = (ImageView) findViewById(R.id.iv_1);
Glide.with(this)
.load(imgUrl)
.transform(new GlideCircleTransform(this))
.placeholder(R.drawable.img1)
.animate(mAnimator)
.into(iv_1);
iv_1.startAnimation(mAnimation);
}
private ViewPropertyAnimation.Animator mAnimator = new ViewPropertyAnimation.Animator() {
@Override
public void animate(View view) {
Log.i("lvjie","Animator-->animate()...");
view.startAnimation(mAnimation);
}
};
}
下面看看具体相关图
![这里写图片描述](https://img-blog.csdn.net/20170516190758693?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMDM0OTY0NA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)