重设(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
原创粉丝点击