Glide 实现圆形图片 并动态旋转

一、添加Glide依赖

compile 'com.github.bumptech.glide:glide:3.7.0'

二、Glide提供了transform方法来实现图片的转化,animate方法来实现图片的动画。

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);

1、Transform相关实现

  • 进入到transform方法
public DrawableRequestBuilder 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();
        // 要处理的bitmap对象
        // 在X轴处理的效果,Shader.TileMode里有三种模式:CLAMP、MIRROR和REPETA
        // 在Y轴处理的效果,Shader.TileMode里有三种模式:CLAMP、MIRROR和REPETA
        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 图片动画的实现

  • 进入到animate方法
public DrawableRequestBuilder 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);

// img_animation.xml文件 该文件是在res/anim下
"1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    "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);
        }
    };


}

下面看看具体相关图

Glide 实现圆形图片 并动态旋转_第1张图片

你可能感兴趣的:(Android)