Glide 实现圆形图片 并动态旋转
2017-05-16 19:09
141 查看
一、添加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<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(); // 要处理的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<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); // img_animation.xml文件 该文件是在res/anim下 <?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); } }; }
下面看看具体相关图
相关文章推荐
- Android开发:使用Glide动态加载圆形图片和圆角图片
- listview和listFragment详细版,已解决chebox的复用,Glide实现图片的圆形,以及按钮添加数据
- Android自定义View实现旋转的圆形图片
- 使用Glide动态加载圆形图片和圆角图片
- Android开发:使用Glide动态加载圆形图片和圆角图片
- Glide实现圆角和圆形图片
- Android将Glide动态加载不同大小的图片切圆角与圆形的方法
- Android 使用Glide动态加载圆形图片和圆角图片
- 伪类:hover+css3实现图片动态旋转变化
- Android 使用Glide实现圆形和圆角图片
- Glide实现圆角 圆形图片
- Glide动态加载圆形图片和圆角图片
- 使用Glide动态加载圆形图片和圆角图片
- Glide实现圆角和圆形图片
- glide实现圆角和圆形图片
- 使用Glide动态加载圆形图片和圆角图片
- Android自定义ImageView实现图片缩放滑动,双击放大缩小,多点触控旋转,圆角矩形,圆形和仿刮刮卡效果
- Android利用glide加载圆形图片,头像的实现
- Android中 3D 圆形旋转动态实现
- 当下流行播放器,实现图片动态旋转