android图片的缩放、圆角处理

android中图片缩放方法有三种:1,bitmapFactory;2,bitmap+metrix;3,thumbUtil

方法一:bitmapFactory:

public static Bitmap resizeBitmapByFactory(String path, int w, int h) {

        BitmapFactory.Options option = new BitmapFactory.Options();

        option.inJustDecodeBounds = true;

        Bitmap bitmap = BitmapFactory.decodeFile(path, option);

        int outWidth = option.outWidth;

        int outHeight = option.outHeight;

        option.inDither = false;

        option.inPreferredConfig = Bitmap.Config.ARGB_8888;



        if (outWidth != 0 && outHeight != 0 && w != 0 && h != 0) {

            int sampleSize = (outWidth / w + outHeight / h) / 2;

            option.inSampleSize = sampleSize;

        }



        option.inJustDecodeBounds = false;

        return BitmapFactory.decodeFile(path, option);



    }

方法二:bitmap+metrix

public static Bitmap resizeBitmapByMetrix(Bitmap bitmap, int w, int h) {

        Bitmap BitmapOrg = bitmap;

        int width = BitmapOrg.getWidth();

        int height = BitmapOrg.getHeight();

        int newWidth = w;

        int newHeight = h;



        float scaleWidth = ((float) newWidth) / width;

        float scaleHeight = ((float) newHeight) / height;



        Matrix matrix = new Matrix();

        matrix.postScale(scaleWidth, scaleHeight);

        // if you want to rotate the Bitmap

        // matrix.postRotate(45);

        Bitmap resizedBitmap = Bitmap.createBitmap(BitmapOrg, 0, 0, width,

                height, matrix, true);

        return resizedBitmap;

    }

方法三,使用系统自带的thumbutil:

 

public static Bitmap resizeBitmapByUtil(Bitmap bitmap, int w, int h) {

        return ThumbnailUtils.extractThumbnail(bitmap, w, h);

    }

三个方法消耗的时间为:72,9,13不过怀疑方法一消耗的时间有可能是由于从文件中加载图片所致。这点待定

二、实现图片的圆角效果

public static Bitmap toRoundCornerBitmap(Bitmap bitmap, int cornerPixel) {

        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),

                bitmap.getHeight(), Config.ARGB_8888);



        Canvas canvas = new Canvas(output);



        final int color = 0xff424242;



        final Paint paint = new Paint();



        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());



        final RectF rectF = new RectF(rect);



        final float roundPx = cornerPixel;



        paint.setAntiAlias(true);



        canvas.drawARGB(0, 0, 0, 0);



        paint.setColor(color);



        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);



        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));



        canvas.drawBitmap(bitmap, rect, rect, paint);

        return output;

    }

三、实现图片缩放,背景为圆角图片,则这个背景可以用图形资源文件来实现。

<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android" 

    android:shape="rectangle">

    <gradient  

        android:angle="90"  

        android:endColor="#00000000"  

        android:startColor="#00000000" />  

  

    <corners  

        android:bottomLeftRadius="12dp"  

        android:bottomRightRadius="12dp"  

        android:topLeftRadius="12dp"  

        android:topRightRadius="12dp" />  

  

    <stroke  

        android:width="1dip"  

        android:color="#eee" />  

    <solid

        android:color="#000"/>

    <padding 

        android:top="5dp"

        android:bottom="5dp"/>

    



</shape>

 

 

 

 

 

你可能感兴趣的:(android)