隨筆 - 22  文章 - 467  trackbacks - 0
          <2025年5月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          常用鏈接

          留言簿(25)

          隨筆分類(74)

          文章分類(1)

          收藏夾(277)

          在線工具

          在線資料

          最新隨筆

          搜索

          •  

          積分與排名

          • 積分 - 217585
          • 排名 - 261

          最新評論

          閱讀排行榜

          評論排行榜

          今天下午弄了半天實現了圖片變黑白和圓角效果,發出來大家共享一下~
          其中思路主要來自www.stackoverflow.com

          不多說,直接貼代碼,歡迎大家交流自己的方法~

           1/**
           2 * 處理圖片的工具類.
           3 * 
           4 */

           5public class ImageTools {
           6    
           7    /**
           8     * 圖片去色,返回灰度圖片
           9     * @param bmpOriginal 傳入的圖片
          10     * @return 去色后的圖片
          11     */

          12    public static Bitmap toGrayscale(Bitmap bmpOriginal) {
          13        int width, height;
          14        height = bmpOriginal.getHeight();
          15        width = bmpOriginal.getWidth();    
          16
          17        Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
          18        Canvas c = new Canvas(bmpGrayscale);
          19        Paint paint = new Paint();
          20        ColorMatrix cm = new ColorMatrix();
          21        cm.setSaturation(0);
          22        ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
          23        paint.setColorFilter(f);
          24        c.drawBitmap(bmpOriginal, 00, paint);
          25        return bmpGrayscale;
          26    }

          27    
          28    
          29    /**
          30     * 去色同時加圓角
          31     * @param bmpOriginal 原圖
          32     * @param pixels 圓角弧度
          33     * @return 修改后的圖片
          34     */

          35    public static Bitmap toGrayscale(Bitmap bmpOriginal, int pixels) {
          36        return toRoundCorner(toGrayscale(bmpOriginal), pixels);
          37    }

          38    
          39    /**
          40     * 把圖片變成圓角
          41     * @param bitmap 需要修改的圖片
          42     * @param pixels 圓角的弧度
          43     * @return 圓角圖片
          44     */

          45    public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) {
          46
          47        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap
          48                .getHeight(), Config.ARGB_8888);
          49        Canvas canvas = new Canvas(output);
          50
          51        final int color = 0xff424242;
          52        final Paint paint = new Paint();
          53        final Rect rect = new Rect(00, bitmap.getWidth(), bitmap.getHeight());
          54        final RectF rectF = new RectF(rect);
          55        final float roundPx = pixels;
          56
          57        paint.setAntiAlias(true);
          58        canvas.drawARGB(0000);
          59        paint.setColor(color);
          60        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
          61
          62        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
          63        canvas.drawBitmap(bitmap, rect, rect, paint);
          64
          65        return output;
          66    }

          67
          68    
          69    /**
          70     * 使圓角功能支持BitampDrawable
          71     * @param bitmapDrawable 
          72     * @param pixels 
          73     * @return
          74     */

          75    public static BitmapDrawable toRoundCorner(BitmapDrawable bitmapDrawable, int pixels) {
          76        Bitmap bitmap = bitmapDrawable.getBitmap();
          77        bitmapDrawable = new BitmapDrawable(toRoundCorner(bitmap, pixels));
          78        return bitmapDrawable;
          79    }

          80}
          posted on 2011-03-23 15:07 ApolloDeng 閱讀(6568) 評論(2)  編輯  收藏 所屬分類: 分享 、Android

          FeedBack:
          # re: 分享一個Android圖片轉換類 1. Bitmap去色,轉換為黑白的灰度圖, 2. Bitmap圖片加圓角效果 2011-11-02 16:23 kaima
          謝謝樓主分享。  回復  更多評論
            
          # re: 分享一個Android圖片轉換類 1. Bitmap去色,轉換為黑白的灰度圖, 2. Bitmap圖片加圓角效果 2011-12-10 21:38 tbw淘寶
          謝謝。幸苦了  回復  更多評論
            
          主站蜘蛛池模板: 江口县| 栖霞市| 策勒县| 宁都县| 黄大仙区| 永胜县| 桦甸市| 柳江县| 太保市| 西宁市| 井陉县| 莎车县| 涿鹿县| 岳普湖县| 社会| 区。| 都匀市| 东山县| 顺平县| 鹿邑县| 长宁区| 泾源县| 康平县| 竹溪县| 墨脱县| 依安县| 公安县| 阿坝县| 东乡| 颍上县| 吴川市| 大姚县| 定襄县| 黑龙江省| 阳城县| 将乐县| 焦作市| 江永县| 荔浦县| 建平县| 孙吴县|