重设(Resize)Bitmap大小以及获取到圆形头像(Rounded Avatar)的方法
来源:互联网 发布:淘宝网智能手机 编辑:程序博客网 时间:2024/06/18 05:15
做登陆的时候遇到的用户头像的两个问题。
一:把获取到的用户头像Resize为需要的大小。
二:将用户头像获取为圆形头像,更为美观。
感谢强大的Google,找了一段时间后找到了两个近乎完美的解决方案。
Resize Bitmap:
之前翻译Google的安卓开发文档的时候翻译过一大篇“高效显示Bitmap的文章,回头翻阅之后得到了官方的resize方法:
public static Bitmap decodeBitmapFromSource(Resources res,int resId,int reqWidth,int reqHeight){ final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeResource(res,resId,options); //计算样本大小 options.inSampleSize = calculateInSampleSize(options,reqWidth,reqHeight); //用inSampleSize来decode bitmap options.inJustDecodeBounds = false; return BitmapFactory.decodeResource(res,resId,options); }public static int calculateInSampleSize(BitmapFactory.Options options,int reqWidth,int reqHeight){ //图像的原长宽 final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if(height > reqHeight || width > reqWidth){ final int halfHeight = height/2; final int halfWidth = width/2; while((halfHeight/inSampleSize) > reqHeight && (halfWidth/inSampleSize) > reqWidth){ inSampleSize *= 2; } } return inSampleSize; }
此方法非常不精确,尝试的时候大小只能设置为整十倍数,更精确的大小会直接被忽略,原因不明。
然后Google了几个方法,终于找到了一个可用且效果很棒的。
public static Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight) { int width = bm.getWidth(); int height = bm.getHeight(); float scaleWidth = ((float) newWidth) / width; float scaleHeight = ((float) newHeight) / height; // CREATE A MATRIX FOR THE MANIPULATION Matrix matrix = new Matrix(); // RESIZE THE BIT MAP matrix.postScale(scaleWidth, scaleHeight); // "RECREATE" THE NEW BITMAP Bitmap resizedBitmap = Bitmap.createBitmap( bm, 0, 0, width, height, matrix, false); bm.recycle(); return resizedBitmap; }结果非常精确,也很简单易用。
然后就是获取圆形头像的问题,以下是找到的工具类,附上原文链接。
转载自:http://tutorialsface.blogspot.com/2014/06/how-to-crop-image-view-as-circle-round.html
import android.graphics.Bitmap;import android.graphics.BitmapShader;import android.graphics.Canvas;import android.graphics.ColorFilter;import android.graphics.Paint;import android.graphics.PixelFormat;import android.graphics.Rect;import android.graphics.RectF;import android.graphics.Shader;import android.graphics.drawable.Drawable;public class RoundImage extends Drawable { private final Bitmap mBitmap; private final Paint mPaint; private final RectF mRectF; private final int mBitmapWidth; private final int mBitmapHeight; public RoundImage(Bitmap bitmap) { mBitmap = bitmap; mRectF = new RectF(); mPaint = new Paint(); mPaint.setAntiAlias(true); mPaint.setDither(true); final BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); mPaint.setShader(shader); mBitmapWidth = mBitmap.getWidth(); mBitmapHeight = mBitmap.getHeight(); } @Override public void draw(Canvas canvas) { canvas.drawOval(mRectF, mPaint); } @Override protected void onBoundsChange(Rect bounds) { super.onBoundsChange(bounds); mRectF.set(bounds); } @Override public void setAlpha(int alpha) { if (mPaint.getAlpha() != alpha) { mPaint.setAlpha(alpha); invalidateSelf(); } } @Override public void setColorFilter(ColorFilter cf) { mPaint.setColorFilter(cf); } @Override public int getOpacity() { return PixelFormat.TRANSLUCENT; } @Override public int getIntrinsicWidth() { return mBitmapWidth; } @Override public int getIntrinsicHeight() { return mBitmapHeight; } public void setAntiAlias(boolean aa) { mPaint.setAntiAlias(aa); invalidateSelf(); } @Override public void setFilterBitmap(boolean filter) { mPaint.setFilterBitmap(filter); invalidateSelf(); } @Override public void setDither(boolean dither) { mPaint.setDither(dither); invalidateSelf(); } public Bitmap getBitmap() { return mBitmap; }}
使用方法如下:
Bitmap userAvatar = Utils.getResizedBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.dribbble_default_avatar),180,180);RoundImage roundImage = new RoundImage(userAvatar);userAvatarImageView.setImageDrawable(roundImage);灰常简单。注意传入的参数的bitmap,如果跟我一样是来自drawable的话就用bitmapfactory来decode一下即可。
0 0
- 重设(Resize)Bitmap大小以及获取到圆形头像(Rounded Avatar)的方法
- Bitmap之圆形头像
- 获取bitmap的大小
- bitmap将头像转换为圆形头像
- iOS 得到圆形头像的方法
- 实现圆形头像的几种方法
- bitmap大小以及压缩的常用三种方法
- 关于获取Bitmap图片的大小的方法
- Android获取Bitmap的大小
- AndroidのBitmap之圆形头像
- Android之Bitmap之圆形头像
- Android之Bitmap之圆形头像
- 把bitmap裁剪成圆形bitmap的方法
- 圆形头像的做法
- 圆形的头像
- 圆形的头像
- 简单的圆形头像
- android圆形的头像
- iOS开发篇——UITextField
- 哈希表字符串匹配
- UISlider用法总结
- Codeforces 621A Wet Shark and Odd and Even
- 刚试了一下,浏览器里可以玩 Windows 95
- 重设(Resize)Bitmap大小以及获取到圆形头像(Rounded Avatar)的方法
- A*启发式搜索算法详解 人工智能
- 补充:函数间只能用this
- [android]_[ImageView点击变暗]
- Codeforces Round #341 (Div. 2) C. Wet Shark and Flowers
- resultType跟resultMap不能同时存在
- Codeforces 621B Wet Shark and Bishops(判定对角线点+组合数统计)
- 出现error: unable to create file tests/P_str_escape/str\\escape.rs的解决办法
- openCV学习记录:滤镜:霓虹&曝光