• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            life02

              C++博客 :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
              197 隨筆 :: 3 文章 :: 37 評(píng)論 :: 0 Trackbacks

            http://06peng.com/read.php/52.htm

            整理了一下目前Android開(kāi)發(fā)中圖片的各種處理方法:

            Java代碼
                  
            1.    /** 
            2.     
            3.      * 使頭像變灰 
            4.     
            5.      * @param drawable 
            6.     
            7.      */  
            8.     
            9.     public static void porBecomeGrey(ImageView imageView, Drawable drawable) {  
            10.     
            11.         drawable.mutate();      
            12.     
            13.         ColorMatrix cm = new ColorMatrix();      
            14.     
            15.         cm.setSaturation(0);      
            16.     
            17.         ColorMatrixColorFilter cf = new ColorMatrixColorFilter(cm);      
            18.     
            19.         drawable.setColorFilter(cf);   
            20.     
            21.         imageView.setImageDrawable(drawable);  
            22.     
            23.     }  

             

            Java 代碼復(fù)制內(nèi)容到剪貼板
                  
            1. Drawable drawable = new FastBitmapDrawable(bitmap);    
            Java 代碼復(fù)制內(nèi)容到剪貼板
                  
            1. public byte[] getBitmapByte(Bitmap bitmap){    
            2.     
            3.     ByteArrayOutputStream out = new ByteArrayOutputStream();    
            4.     
            5.     bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);    
            6.     
            7.     try {    
            8.     
            9.         out.flush();    
            10.     
            11.         out.close();    
            12.     
            13.     } catch (IOException e) {    
            14.     
            15.         e.printStackTrace();    
            16.     
            17.     }    
            18.     
            19.     return out.toByteArray();    
            20.     
            21. }    
            22.     
            23.     
            24.     
            25.     
            26.     
            27. public Bitmap getBitmapFromByte(byte[] temp){    
            28.     
            29.     if(temp != null){    
            30.     
            31.         Bitmap bitmap = BitmapFactory.decodeByteArray(temp, 0, temp.length);    
            32.     
            33.         return bitmap;    
            34.     
            35.     }else{    
            36.     
            37.         return null;    
            38.     
            39.     }    
            40.     
            41. }    
            Java 代碼復(fù)制內(nèi)容到剪貼板
                  
            1. /**
            2.     
            3.      * 將Drawable轉(zhuǎn)化為Bitmap
            4.     
            5.      * @param drawable
            6.     
            7.      * @return
            8.     
            9.      */  
            10.     
            11.     public static Bitmap drawableToBitmap(Drawable drawable) {  
            12.     
            13.         int width = drawable.getIntrinsicWidth();  
            14.     
            15.         int height = drawable.getIntrinsicHeight();  
            16.     
            17.         Bitmap bitmap = Bitmap.createBitmap(width, height, drawable  
            18.     
            19.                 .getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888  
            20.     
            21.                 : Bitmap.Config.RGB_565);  
            22.     
            23.         Canvas canvas = new Canvas(bitmap);  
            24.     
            25.         drawable.setBounds(0, 0, width, height);  
            26.     
            27.         drawable.draw(canvas);  
            28.     
            29.         return bitmap;  
            30.     
            31.     }  
            Java 代碼復(fù)制內(nèi)容到剪貼板
                  
            1. /**
            2.     
            3.     * 獲取圖片的倒影
            4.     
            5.     * @param bitmap
            6.     
            7.     * @return
            8.     
            9.     */  
            10.     
            11.     public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap) {  
            12.     
            13.         final int reflectionGap = 4;  
            14.     
            15.         int width = bitmap.getWidth();  
            16.     
            17.         int height = bitmap.getHeight();  
            18.     
            19.   
            20.     
            21.         Matrix matrix = new Matrix();  
            22.     
            23.         matrix.preScale(1, -1);  
            24.     
            25.   
            26.     
            27.         Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height / 2,  
            28.     
            29.                 width, height / 2, matrix, false);  
            30.     
            31.   
            32.     
            33.         Bitmap bitmapWithReflection = Bitmap.createBitmap(width,  
            34.     
            35.                 (height + height / 2), Config.ARGB_8888);  
            36.     
            37.   
            38.     
            39.         Canvas canvas = new Canvas(bitmapWithReflection);  
            40.     
            41.         canvas.drawBitmap(bitmap, 0, 0, null);  
            42.     
            43.         Paint deafalutPaint = new Paint();  
            44.     
            45.         canvas.drawRect(0, height, width, height + reflectionGap, deafalutPaint);  
            46.     
            47.   
            48.     
            49.         canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);  
            50.     
            51.   
            52.     
            53.         Paint paint = new Paint();  
            54.     
            55.         LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,  
            56.     
            57.                 bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff,  
            58.     
            59.                 0x00ffffff, TileMode.CLAMP);  
            60.     
            61.         paint.setShader(shader);  
            62.     
            63.         paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));  
            64.     
            65.         canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()  
            66.     
            67.                 + reflectionGap, paint);  
            68.     
            69.         return bitmapWithReflection;  
            70.     
            71.     }  
            Java 代碼復(fù)制內(nèi)容到剪貼板
                  
            1. /**
            2.     
            3.     * 把圖片變成圓角  
            4.     
            5.     * @param bitmap 需要修改的圖片  
            6.     
            7.     * @param pixels 圓角的弧度  
            8.     
            9.     * @return 圓角圖片  
            10.     
            11.     */      
            12.     
            13.    public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) {      
            14.     
            15.       
            16.     
            17.        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);      
            18.     
            19.        Canvas canvas = new Canvas(output);      
            20.     
            21.       
            22.     
            23.        final int color = 0xff424242;      
            24.     
            25.        final Paint paint = new Paint();      
            26.     
            27.        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());      
            28.     
            29.        final RectF rectF = new RectF(rect);      
            30.     
            31.        final float roundPx = pixels;      
            32.     
            33.       
            34.     
            35.        paint.setAntiAlias(true);      
            36.     
            37.        canvas.drawARGB(0, 0, 0, 0);      
            38.     
            39.        paint.setColor(color);      
            40.     
            41.        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);      
            42.     
            43.       
            44.     
            45.        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));      
            46.     
            47.        canvas.drawBitmap(bitmap, rect, rect, paint);      
            48.     
            49.       
            50.     
            51.        return output;      
            52.     
            53.    }    
            Java 代碼復(fù)制內(nèi)容到剪貼板
                  
            1. /**
            2.     
            3.      * 縮放圖片
            4.     
            5.      * @param bmp
            6.     
            7.      * @param width
            8.     
            9.      * @param height
            10.     
            11.      * @return
            12.     
            13.      */  
            14.     
            15.     public static Bitmap PicZoom(Bitmap bmp, int width, int height) {  
            16.     
            17.         int bmpWidth = bmp.getWidth();  
            18.     
            19.         int bmpHeght = bmp.getHeight();  
            20.     
            21.         Matrix matrix = new Matrix();  
            22.     
            23.         matrix.postScale((float) width / bmpWidth, (float) height / bmpHeght);  
            24.     
            25.   
            26.     
            27.         return Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeght, matrix, true);  
            28.     
            29.     }  
            Java 代碼復(fù)制內(nèi)容到剪貼板
                  
            1. /**
            2.     
            3.      * @param photoPath --原圖路經(jīng)
            4.     
            5.      * @param aFile     --保存縮圖
            6.     
            7.      * @param newWidth  --縮圖寬度
            8.     
            9.      * @param newHeight --縮圖高度
            10.     
            11.      */  
            12.     
            13.     public static boolean bitmapToFile(String photoPath, File aFile, int newWidth, int newHeight) {  
            14.     
            15.         BitmapFactory.Options options = new BitmapFactory.Options();  
            16.     
            17.         options.inJustDecodeBounds = true;  
            18.     
            19.         // 獲取這個(gè)圖片的寬和高  
            20.     
            21.         Bitmap bitmap = BitmapFactory.decodeFile(photoPath, options);  
            22.     
            23.         options.inJustDecodeBounds = false;  
            24.     
            25.   
            26.     
            27.         //計(jì)算縮放比  
            28.     
            29.         options.inSampleSize = reckonThumbnail(options.outWidth, options.outHeight, newWidth, newHeight);  
            30.     
            31.   
            32.     
            33.         bitmap = BitmapFactory.decodeFile(photoPath, options);  
            34.     
            35.   
            36.     
            37.         try {  
            38.     
            39.             ByteArrayOutputStream baos = new ByteArrayOutputStream();  
            40.     
            41.             bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);  
            42.     
            43.             byte[] photoBytes = baos.toByteArray();  
            44.     
            45.   
            46.     
            47.             if (aFile.exists()) {  
            48.     
            49.                 aFile.delete();  
            50.     
            51.             }  
            52.     
            53.             aFile.createNewFile();  
            54.     
            55.   
            56.     
            57.             FileOutputStream fos = new FileOutputStream(aFile);  
            58.     
            59.             fos.write(photoBytes);  
            60.     
            61.             fos.flush();  
            62.     
            63.             fos.close();  
            64.     
            65.   
            66.     
            67.             return true;  
            68.     
            69.         } catch (Exception e1) {  
            70.     
            71.             e1.printStackTrace();  
            72.     
            73.             if (aFile.exists()) {  
            74.     
            75.                 aFile.delete();  
            76.     
            77.             }  
            78.     
            79.             Log.e("Bitmap To File Fail", e1.toString());  
            80.     
            81.             return false;  
            82.     
            83.         }  
            84.     
            85.     }  
            Java 代碼復(fù)制內(nèi)容到剪貼板
                  
            1. /**
            2.     
            3.      * 計(jì)算縮放比
            4.     
            5.      * @param oldWidth
            6.     
            7.      * @param oldHeight
            8.     
            9.      * @param newWidth
            10.     
            11.      * @param newHeight
            12.     
            13.      * @return
            14.     
            15.      */  
            16.     
            17.     public static int reckonThumbnail(int oldWidth, int oldHeight, int newWidth, int newHeight) {  
            18.     
            19.         if ((oldHeight > newHeight && oldWidth > newWidth)  
            20.     
            21.                 || (oldHeight <= newHeight && oldWidth > newWidth)) {  
            22.     
            23.             int be = (int) (oldWidth / (float) newWidth);  
            24.     
            25.             if (be <= 1)  
            26.     
            27.                 be = 1;  
            28.     
            29.             return be;  
            30.     
            31.         } else if (oldHeight > newHeight && oldWidth <= newWidth) {  
            32.     
            33.             int be = (int) (oldHeight / (float) newHeight);  
            34.     
            35.             if (be <= 1)  
            36.     
            37.                 be = 1;  
            38.     
            39.             return be;  
            40.     
            41.         }  
            42.     
            43.   
            44.     
            45.         return 1;  
            46.     
            47.     }  

            Android邊框圓角

            XML/HTML 代碼復(fù)制內(nèi)容到剪貼板
                  
            1. <?xml version="1.0" encoding="utf-8"?>      
            2.     
            3. <shape xmlns:android="http://schema...android">        
            4.     
            5.     <solid android:color="#000000" />        
            6.     
            7.     <corners android:topLeftRadius="10dp"      
            8.     
            9.                     android:topRightRadius="10dp"        
            10.     
            11.                 android:bottomRightRadius="10dp"      
            12.     
            13.                 android:bottomLeftRadius="10dp"/>        
            14.     
            15. </shape>    

            解釋:solid的表示填充顏色,為了簡(jiǎn)單,這里用的是黑色。

            而corners則是表示圓角,注意的是這里bottomRightRadius是左下角而不是右下角,bottomLeftRadius右下角。
            當(dāng)然上面的效果也可以像下面一樣設(shè)置,如下: <corners android:radius="5dp" />  

             

            源碼下載:

             

            如果有更多的功能和方法,請(qǐng)大家評(píng)論指出。

            posted on 2012-03-15 13:50 life02 閱讀(544) 評(píng)論(0)  編輯 收藏 引用 所屬分類: Android開(kāi)發(fā)
            久久99精品久久久久子伦| 免费精品久久久久久中文字幕| 久久综合亚洲色HEZYO社区| 亚洲国产成人久久精品99| 久久久精品国产免大香伊| 性欧美大战久久久久久久久| 久久伊人精品青青草原高清| 久久播电影网| 亚洲av日韩精品久久久久久a | 亚洲∧v久久久无码精品| 精品久久久久久成人AV| 久久久久18| 国产精品一区二区久久国产| 久久久久久青草大香综合精品| 久久水蜜桃亚洲av无码精品麻豆 | 久久99久久无码毛片一区二区| 日韩美女18网站久久精品| 欧美一区二区三区久久综 | 久久狠狠爱亚洲综合影院| 国产一区二区精品久久| 亚洲综合伊人久久大杳蕉| 国产精品免费久久久久电影网| 午夜人妻久久久久久久久| 日日狠狠久久偷偷色综合96蜜桃| 97久久精品人妻人人搡人人玩| 久久久久久久久久久精品尤物 | 99久久精品国产毛片| 久久精品水蜜桃av综合天堂| 少妇无套内谢久久久久| 欧美午夜A∨大片久久| 久久激情亚洲精品无码?V| 好久久免费视频高清| 久久精品无码专区免费东京热 | 国产亚洲欧美精品久久久| 亚洲天堂久久久| 噜噜噜色噜噜噜久久| 欧美国产精品久久高清| 国产精品免费看久久久香蕉| 狠狠久久综合| 性欧美大战久久久久久久| 亚洲国产日韩欧美久久|