Android应用中在对头像进行圆形处理

发布时间:2020-12-04 17:08:37 作者:Leah
来源:亿速云 阅读:118

本篇文章给大家分享的是有关Android应用中在对头像进行圆形处理,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

Glide实现圆形图像

Glide.with(mContext)
  .load(R.drawable.iv_image_header)
  .error(R.drawable.ic_error_default)
  .transform(new GlideCircleTransform(mContext))
  .into(mImage);

其中load后为载入的图像,error后为出错时载入的图像,transform是对其修改,我们也是通过这个GlideCirTransForm来修改的,使用的话要把mContext替换为你自己的activty,mImage为图片载入的位置

使用之前的准备

1.添加项目依赖

compile 'org.greenrobot:eventbus:3.0.0'
compile 'com.squareup.retrofit2:retrofit:2.0.2'
compile 'com.squareup.retrofit2:converter-gson:2.0.2'
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'org.jetbrains:annotations-java5:15.0'
compile 'in.srain.cube:ultra-ptr:1.0.11'
compile 'com.wang.avi:library:1.0.5'

2.导入GlideCircleTransform.java文件

GlideCircleTransform.java代码如下:

package com.sina.weibo.sdk.demo.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;

import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;

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);
  float r = size / 2f;
  canvas.drawCircle(r, r, r, paint);
  return result;
 }
 @Override
 public String getId() {
  return getClass().getName();
 }
}

以上就是Android应用中在对头像进行圆形处理,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

推荐阅读:
  1. Vue中怎么实现头像处理
  2. 怎么在Android中使用PhotoView实现头像/圆形裁剪控件

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android roi

上一篇:Android 应用中的轮播图怎么利用ViewPager实现

下一篇:怎么对hibernate4进行配置

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》