日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

android 圖片處理

 herozhou1314 2012-05-03

android 圖片處理

Posted on 2011-09-16 15:09 蔡清華 閱讀(930) 評(píng)論(0) 編輯 收藏

1、圖標(biāo)加灰色過(guò)濾;
2、android的圖片資源默認(rèn)是靜態(tài)的,單實(shí)例;如果兩個(gè)IM好友的頭像一樣,最簡(jiǎn)單的都是用的軟件自帶頭像,有一個(gè)在線,一個(gè)離線,直接改變頭像的灰度,則兩個(gè)用戶的頭像都會(huì)變灰或者在線,答案是:Drawable.mutate()。
Java代碼 復(fù)制代碼 收藏代碼
  1. Drawable mDrawable = context.getResources().getDrawable(R.drawable.face_icon);     
  2. //Make this drawable mutable.     
  3. //A mutable drawable is guaranteed to not share its state with any other drawable.     
  4. mDrawable.mutate();     
  5. ColorMatrix cm = new ColorMatrix();     
  6. cm.setSaturation(0);     
  7. ColorMatrixColorFilter cf = new ColorMatrixColorFilter(cm);     
  8. mDrawable.setColorFilter(cf);  


Java代碼 復(fù)制代碼 收藏代碼
  1. //Android Matrix類實(shí)現(xiàn)鏡像方法   
  2. public void drawRegion(Image image_src,   
  3.   
  4. int x_src, int y_src,   
  5.   
  6. int width, int height,   
  7.   
  8. int transform,   
  9.   
  10. int x_dest, int y_dest,   
  11.   
  12. int anchor){   
  13.   
  14. if((anchor&VCENTER) != 0){   
  15.   
  16. y_dest -= height/2;   
  17.   
  18. }else if((anchor&BOTTOM) != 0){   
  19.   
  20. y_dest -= height;   
  21.   
  22. }   
  23.   
  24. if((anchor&RIGHT) != 0){   
  25.   
  26. x_dest -= width;   
  27.   
  28. }else if((anchor&HCENTER) != 0){   
  29.   
  30. x_dest -= width/2;   
  31.   
  32. }   
  33.   
  34. Bitmap newMap = Bitmap.createBitmap(image_src.getBitmap(), x_src, y_src, width, height);   
  35.   
  36. Matrix mMatrix = new Matrix();   
  37.   
  38. Matrix temp = new Matrix();   
  39.   
  40. Matrix temp2 = new Matrix();   
  41.   
  42. float[] mirrorY = {   
  43.   
  44. -100,   
  45. 010,   
  46. 001  
  47.   
  48. };   
  49.   
  50. temp.setValues(mirrorY);   
  51.   
  52. switch(transform){   
  53.   
  54. case Sprite.TRANS_NONE:   
  55.   
  56. break;   
  57.   
  58. case Sprite.TRANS_ROT90:   
  59.   
  60. mMatrix.setRotate(90,width/2, height/2);   
  61.   
  62. break;   
  63.   
  64. case Sprite.TRANS_ROT180:   
  65.   
  66. mMatrix.setRotate(180,width/2, height/2);   
  67.   
  68. break;   
  69.   
  70. case Sprite.TRANS_ROT270:   
  71.   
  72. mMatrix.setRotate(270,width/2, height/2);   
  73.   
  74. break;   
  75.   
  76. case Sprite.TRANS_MIRROR:   
  77.   
  78. mMatrix.postConcat(temp);   
  79.   
  80. break;   
  81.   
  82. case Sprite.TRANS_MIRROR_ROT90:   
  83.   
  84. mMatrix.postConcat(temp);   
  85.   
  86. mMatrix.setRotate(90,width/2, height/2);   
  87.   
  88. break;   
  89.   
  90. case Sprite.TRANS_MIRROR_ROT180:   
  91.   
  92. mMatrix.postConcat(temp);   
  93.   
  94. mMatrix.setRotate(180,width/2, height/2);   
  95.   
  96. break;   
  97.   
  98. case Sprite.TRANS_MIRROR_ROT270:   
  99.   
  100. mMatrix.postConcat(temp);   
  101.   
  102. mMatrix.setRotate(270,width/2, height/2);   
  103.   
  104. break;   
  105.   
  106. }   
  107.   
  108. mMatrix.setTranslate(x_dest, y_dest);   
  109.   
  110. canvas.drawBitmap(newMap, mMatrix, mPaint);   
  111.   
  112. }  


Java代碼 復(fù)制代碼 收藏代碼
  1. //圖片Url保存為位圖并進(jìn)行縮放操作   
  2. //通過(guò)傳入圖片url獲取位圖方法   
  3. public Bitmap returnBitMap(String url) {   
  4.         URL myFileUrl = null;   
  5.         Bitmap bitmap = null;   
  6.         try {   
  7.             myFileUrl = new URL(url);   
  8.         } catch (MalformedURLException e) {   
  9.             e.printStackTrace();   
  10.         }   
  11.         try {   
  12.             HttpURLConnection conn = (HttpURLConnection) myFileUrl   
  13.                     .openConnection();   
  14.             conn.setDoInput(true);   
  15.             conn.connect();   
  16.             InputStream is = conn.getInputStream();   
  17.             bitmap = BitmapFactory.decodeStream(is);   
  18.             is.close();   
  19.         } catch (IOException e) {   
  20.             e.printStackTrace();   
  21.         }   
  22.         Log.v(tag, bitmap.toString());   
  23.   
  24.         return bitmap;   
  25.     }   
  26. //通過(guò)傳入位圖,新的寬.高比進(jìn)行位圖的縮放操作   
  27. public static Drawable resizeImage(Bitmap bitmap, int w, int h) {   
  28.   
  29.         // load the origial Bitmap   
  30.         Bitmap BitmapOrg = bitmap;   
  31.   
  32.         int width = BitmapOrg.getWidth();   
  33.         int height = BitmapOrg.getHeight();   
  34.         int newWidth = w;   
  35.         int newHeight = h;   
  36.   
  37.         Log.v(tag, String.valueOf(width));   
  38.         Log.v(tag, String.valueOf(height));   
  39.   
  40.         Log.v(tag, String.valueOf(newWidth));   
  41.         Log.v(tag, String.valueOf(newHeight));   
  42.   
  43.         // calculate the scale   
  44.         float scaleWidth = ((float) newWidth) / width;   
  45.         float scaleHeight = ((float) newHeight) / height;   
  46.   
  47.         // create a matrix for the manipulation   
  48.         Matrix matrix = new Matrix();   
  49.         // resize the Bitmap   
  50.         matrix.postScale(scaleWidth, scaleHeight);   
  51.         // if you want to rotate the Bitmap   
  52.         // matrix.postRotate(45);   
  53.   
  54.         // recreate the new Bitmap   
  55.         Bitmap resizedBitmap = Bitmap.createBitmap(BitmapOrg, 00, width,   
  56.                 height, matrix, true);   
  57.   
  58.         // make a Drawable from Bitmap to allow to set the Bitmap   
  59.         // to the ImageView, ImageButton or what ever   
  60.         return new BitmapDrawable(resizedBitmap);   
  61.   
  62.     }  


Java代碼 復(fù)制代碼 收藏代碼
  1. 1.圖片加載方法,方便用戶加載圖片   
  2. /***  
  3. * 加載本地圖片  
  4. * @param context:主運(yùn)行函數(shù)實(shí)例  
  5. * @param bitAdress:圖片地址,一般指向R下的drawable目錄  
  6. * @return  
  7. */  
  8. public final Bitmap CreatImage(Context context, int bitAdress) {   
  9. Bitmap bitmaptemp = null;   
  10. bitmaptemp = BitmapFactory.decodeResource(context.getResources(),   
  11. bitAdress);   
  12. return bitmaptemp;   
  13. }   
  14. 2.圖片平均分割方法,將大圖平均分割為N行N列,方便用戶使用   
  15. /***  
  16. * 圖片分割  
  17. *  
  18. * @param g  
  19. * :畫布  
  20. * @param paint  
  21. * :畫筆  
  22. * @param imgBit  
  23. * :圖片  
  24. * @param x  
  25. * :X軸起點(diǎn)坐標(biāo)  
  26. * @param y  
  27. * :Y軸起點(diǎn)坐標(biāo)  
  28. * @param w  
  29. * :?jiǎn)我粓D片的寬度  
  30. * @param h  
  31. * :?jiǎn)我粓D片的高度  
  32. * @param line  
  33. * :第幾列  
  34. * @param row  
  35. * :第幾行  
  36. */  
  37. public final void cuteImage(Canvas g, Paint paint, Bitmap imgBit, int x,   
  38. int y, int w, int h, int line, int row) {   
  39. g.clipRect(x, y, x + w, h + y);   
  40. g.drawBitmap(imgBit, x – line * w, y – row * h, paint);   
  41. g.restore();   
  42. }   
  43. 3.圖片縮放,對(duì)當(dāng)前圖片進(jìn)行縮放處理   
  44. /***  
  45. * 圖片的縮放方法  
  46. *  
  47. * @param bgimage  
  48. * :源圖片資源  
  49. * @param newWidth  
  50. * :縮放后寬度  
  51. * @param newHeight  
  52. * :縮放后高度  
  53. * @return  
  54. */  
  55. public Bitmap zoomImage(Bitmap bgimage, int newWidth, int newHeight) {   
  56. // 獲取這個(gè)圖片的寬和高   
  57. int width = bgimage.getWidth();   
  58. int height = bgimage.getHeight();   
  59. // 創(chuàng)建操作圖片用的matrix對(duì)象   
  60. Matrix matrix = new Matrix();   
  61. // 計(jì)算縮放率,新尺寸除原始尺寸   
  62. float scaleWidth = ((float) newWidth) / width;   
  63. float scaleHeight = ((float) newHeight) / height;   
  64. // 縮放圖片動(dòng)作   
  65. matrix.postScale(scaleWidth, scaleHeight);   
  66. Bitmap bitmap = Bitmap.createBitmap(bgimage, 00, width, height,   
  67. matrix, true);   
  68. return bitmap;   
  69. }   
  70. 4.繪制帶有邊框的文字,一般在游戲中起文字的美化作用   
  71. /***  
  72. * 繪制帶有邊框的文字  
  73. *  
  74. * @param strMsg  
  75. * :繪制內(nèi)容  
  76. * @param g  
  77. * :畫布  
  78. * @param paint  
  79. * :畫筆  
  80. * @param setx  
  81. * ::X軸起始坐標(biāo)  
  82. * @param sety  
  83. * :Y軸的起始坐標(biāo)  
  84. * @param fg  
  85. * :前景色  
  86. * @param bg  
  87. * :背景色  
  88. */  
  89. public void drawText(String strMsg, Canvas g, Paint paint, int setx,   
  90. int sety, int fg, int bg) {   
  91. paint.setColor(bg);   
  92. g.drawText(strMsg, setx + 1, sety, paint);   
  93. g.drawText(strMsg, setx, sety – 1, paint);   
  94. g.drawText(strMsg, setx, sety + 1, paint);   
  95. g.drawText(strMsg, setx – 1, sety, paint);   
  96. paint.setColor(fg);   
  97. g.drawText(strMsg, setx, sety, paint);   
  98. g.restore();   
  99. }   
  100. 5.Android 圖片透明度處理代碼   
  101. /**  
  102. * 圖片透明度處理  
  103. *  
  104. * @param sourceImg  
  105. *            原始圖片  
  106. * @param number  
  107. *            透明度  
  108. * @return  
  109. */  
  110. public static Bitmap setAlpha(Bitmap sourceImg, int number) {   
  111. int[] argb = new int[sourceImg.getWidth() * sourceImg.getHeight()];   
  112. sourceImg.getPixels(argb, 0, sourceImg.getWidth(), 00,sourceImg.getWidth(), sourceImg.getHeight());// 獲得圖片的ARGB值   
  113. number = number * 255 / 100;   
  114. for (int i = 0; i < argb.length; i++) {   
  115. argb = (number << 24) | (argb & 0×00FFFFFF);// 修改最高2位的值   
  116. }   
  117. sourceImg = Bitmap.createBitmap(argb, sourceImg.getWidth(), sourceImg.getHeight(), Config.ARGB_8888);   
  118. return sourceImg;   
  119. }   
  120. 6.圖片翻轉(zhuǎn)   
  121. Resources res = this.getContext().getResources();   
  122. img = BitmapFactory.decodeResource(res, R.drawable.slogo);   
  123. Matrix matrix = new Matrix();   
  124. matrix.postRotate(90);        /*翻轉(zhuǎn)90度*/  
  125. int width = img.getWidth();   
  126. int height = img.getHeight();   
  127. r_img = Bitmap.createBitmap(img, 00, width, height, matrix, true);  

Java代碼 復(fù)制代碼 收藏代碼
  1. import android.graphics.Bitmap;   
  2. import android.graphics.Canvas;   
  3. import android.graphics.LinearGradient;   
  4. import android.graphics.Matrix;   
  5. import android.graphics.Paint;   
  6. import android.graphics.PixelFormat;   
  7. import android.graphics.PorterDuffXfermode;   
  8. import android.graphics.Rect;   
  9. import android.graphics.RectF;   
  10. import android.graphics.Bitmap.Config;   
  11. import android.graphics.PorterDuff.Mode;   
  12. import android.graphics.Shader.TileMode;   
  13. import android.graphics.drawable.Drawable;   
  14. /**  
  15.  
  16. * @author superdev  
  17. * @version 1.0  
  18. *  
  19. */  
  20. public class ImageUtil {   
  21.   
  22. /**  
  23. * 放大縮小圖片  
  24. */  
  25. public static Bitmap zoomBitmap(Bitmap bitmap, int w, int h) {   
  26.    int width = bitmap.getWidth();   
  27.    int height = bitmap.getHeight();   
  28.    Matrix matrix = new Matrix();   
  29.    float scaleWidht = ((float) w / width);   
  30.    float scaleHeight = ((float) h / height);   
  31.    matrix.postScale(scaleWidht, scaleHeight);   
  32.    Bitmap newbmp = Bitmap.createBitmap(bitmap, 00, width, height, matrix, true);   
  33.    return newbmp;   
  34. }   
  35.   
  36. /**  
  37. * 將Drawable轉(zhuǎn)化為Bitmap  
  38. */  
  39. public static Bitmap drawableToBitmap(Drawable drawable) {   
  40.    int width = drawable.getIntrinsicWidth();   
  41.    int height = drawable.getIntrinsicHeight();   
  42.    Bitmap bitmap = Bitmap.createBitmap(width, height, drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);   
  43.    Canvas canvas = new Canvas(bitmap);   
  44.    drawable.setBounds(00, width, height);   
  45.    drawable.draw(canvas);   
  46.    return bitmap;   
  47.   
  48. }   
  49.   
  50. /**  
  51. * 獲得圓角圖片的方法  
  52. */  
  53. public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {   
  54.   
  55.    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);   
  56.    Canvas canvas = new Canvas(output);   
  57.   
  58.    final int color = 0xff424242;   
  59.    final Paint paint = new Paint();   
  60.    final Rect rect = new Rect(00, bitmap.getWidth(), bitmap.getHeight());   
  61.    final RectF rectF = new RectF(rect);   
  62.   
  63.    paint.setAntiAlias(true);   
  64.    canvas.drawARGB(0000);   
  65.    paint.setColor(color);   
  66.    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);   
  67.   
  68.    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));   
  69.    canvas.drawBitmap(bitmap, rect, rect, paint);   
  70.   
  71.    return output;   
  72. }   
  73.   
  74. /**  
  75. * 獲得帶倒影的圖片方法  
  76. */  
  77. public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap) {   
  78.    final int reflectionGap = 4;   
  79.    int width = bitmap.getWidth();   
  80.    int height = bitmap.getHeight();   
  81.   
  82.    Matrix matrix = new Matrix();   
  83.    matrix.preScale(1, -1);   
  84.   
  85.    Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height / 2, width, height / 2, matrix, false);   
  86.   
  87.    Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2), Config.ARGB_8888);   
  88.   
  89.    Canvas canvas = new Canvas(bitmapWithReflection);   
  90.    canvas.drawBitmap(bitmap, 00null);   
  91.    Paint deafalutPaint = new Paint();   
  92.    canvas.drawRect(0, height, width, height + reflectionGap, deafalutPaint);   
  93.   
  94.    canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);   
  95.   
  96.    Paint paint = new Paint();   
  97.    LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0, bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff0x00ffffff, TileMode.CLAMP);   
  98.    paint.setShader(shader);   
  99.    // Set the Transfer mode to be porter duff and destination in   
  100.    paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));   
  101.    // Draw a rectangle using the paint with our linear gradient   
  102.    canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint);   
  103.    return bitmapWithReflection;   
  104. }   
  105. }  

Java代碼 復(fù)制代碼 收藏代碼
  1. private byte[] Bitmap2Bytes(Bitmap bm){   
  2.    ByteArrayOutputStream baos = new ByteArrayOutputStream();   
  3.    bm.compress(Bitmap.CompressFormat.PNG, 100, baos);   
  4.    return baos.toByteArray();   
  5. }   
  6. private Bitmap Bytes2Bimap(byte[] b){   
  7.             if(b.length!=0){   
  8.                 return BitmapFactory.decodeByteArray(b, 0, b.length);   
  9.             }   
  10.             else {   
  11.                 return null;   
  12.             }   
  13.       }   
  14.   
  15.  /**  
  16.      * create the bitmap from a byte array  
  17.      *生成水印圖片  
  18.      * @param src the bitmap object you want proecss  
  19.      * @param watermark the water mark above the src  
  20.      * @return return a bitmap object ,if paramter's length is 0,return null  
  21.      */  
  22.     private Bitmap createBitmap( Bitmap src, Bitmap watermark )   
  23.     {   
  24.         String tag = "createBitmap";   
  25.         Log.d( tag, "create a new bitmap" );   
  26.         if( src == null )   
  27.         {   
  28.             return null;   
  29.         }   
  30.     
  31.         int w = src.getWidth();   
  32.         int h = src.getHeight();   
  33.         int ww = watermark.getWidth();   
  34.         int wh = watermark.getHeight();   
  35.         //create the new blank bitmap   
  36.         Bitmap newb = Bitmap.createBitmap( w, h, Config.ARGB_8888 );//創(chuàng)建一個(gè)新的和SRC長(zhǎng)度寬度一樣的位圖   
  37.         Canvas cv = new Canvas( newb );   
  38.         //draw src into   
  39.         cv.drawBitmap( src, 00null );//在 0,0坐標(biāo)開(kāi)始畫入src   
  40.         //draw watermark into   
  41.         cv.drawBitmap( watermark, w - ww + 5, h - wh + 5null );//在src的右下角畫入水印   
  42.         //save all clip   
  43.         cv.save( Canvas.ALL_SAVE_FLAG );//保存   
  44.         //store   
  45.         cv.restore();//存儲(chǔ)   
  46.         return newb;   
  47.     }   
  48.    /** 重新編碼Bitmap  
  49.    *   
  50.    * @param src  
  51.    *          需要重新編碼的Bitmap  
  52.    *  
  53.    * @param format  
  54.    *          編碼后的格式(目前只支持png和jpeg這兩種格式)  
  55.    *  
  56.    * @param quality  
  57.    *          重新生成后的bitmap的質(zhì)量  
  58.    *  
  59.    * @return  
  60.    *          返回重新生成后的bitmap  
  61.    */  
  62.  private static Bitmap codec(Bitmap src, Bitmap.CompressFormat format,   
  63.                                     int quality) {   
  64.             ByteArrayOutputStream os = new ByteArrayOutputStream();   
  65.             src.compress(format, quality, os);               
  66.   
  67.             byte[] array = os.toByteArray();   
  68.             return BitmapFactory.decodeByteArray(array, 0, array.length);   
  69.         }   
  70.   
  71. //Stream轉(zhuǎn)換成Byte   
  72. static byte[] streamToBytes(InputStream is) {   
  73.       ByteArrayOutputStream os = new ByteArrayOutputStream(1024);   
  74.       byte[] buffer = new byte[1024];   
  75.       int len;   
  76.       try {   
  77.              while ((len = is.read(buffer)) >= 0) {   
  78.              os.write(buffer, 0, len);   
  79.              }   
  80.           } catch (java.io.IOException e) {   
  81.   
  82.           }   
  83.           return os.toByteArray();   
  84. }   
  85. //把View轉(zhuǎn)換成Bitmap   
  86.   
  87.     /**  
  88.      * 把一個(gè)View的對(duì)象轉(zhuǎn)換成bitmap  
  89.      */  
  90.     static Bitmap getViewBitmap(View v) {   
  91.            
  92.         v.clearFocus();   
  93.         v.setPressed(false);   
  94.   
  95.         //能畫緩存就返回false   
  96.         boolean willNotCache = v.willNotCacheDrawing();   
  97.         v.setWillNotCacheDrawing(false);    
  98.         int color = v.getDrawingCacheBackgroundColor();   
  99.         v.setDrawingCacheBackgroundColor(0);   
  100.         if (color != 0) {   
  101.             v.destroyDrawingCache();   
  102.         }   
  103.         v.buildDrawingCache();   
  104.         Bitmap cacheBitmap = v.getDrawingCache();   
  105.         if (cacheBitmap == null) {   
  106.             Log.e(TAG, "failed getViewBitmap(" + v + ")"new RuntimeException());   
  107.             return null;   
  108.         }   
  109.         Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);   
  110.         // Restore the view   
  111.         v.destroyDrawingCache();   
  112.         v.setWillNotCacheDrawing(willNotCache);   
  113.         v.setDrawingCacheBackgroundColor(color);   
  114.         return bitmap;   
  115.     }  

Java代碼 復(fù)制代碼 收藏代碼
  1. 讀取raw資源文件中的mp3文件,然后通過(guò)音樂(lè)播放器播放:   
  2.   
  3.     /**  
  4.      * 把mp3文件寫入卡  
  5.      *   
  6.      * @param fileName  
  7.      *             輸出的文件名(全路徑)  
  8.      * @param context  
  9.          *             context對(duì)象  
  10.      */  
  11.     private void writeMP3ToSDcard(String fileName, Context context) {   
  12.         byte[] buffer = new byte[1024 * 8];   
  13.         int read;   
  14.         BufferedInputStream bin = new BufferedInputStream(context.getResources().openRawResource(R.raw.ring));   
  15.         try {   
  16.             BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(fileName));   
  17.             while ((read = bin.read(buffer)) > -1) {   
  18.                 bout.write(buffer, 0, read);   
  19.             }   
  20.             bout.flush();   
  21.             bout.close();   
  22.             bin.close();   
  23.         } catch (FileNotFoundException e) {   
  24.             e.printStackTrace();   
  25.         } catch (IOException e) {   
  26.             e.printStackTrace();   
  27.         }   
  28.     }   
  29.   
  30.   
  31. Intent intent = new Intent();   
  32. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);   
  33. intent.setAction(android.content.Intent.ACTION_VIEW);   
  34. intent.setDataAndType(Uri.fromFile(newFile("XXXXmp3的文件全路徑")),"audio/*");   
  35. startActivity(intent);  


繪制圖像倒影
Java代碼 復(fù)制代碼 收藏代碼
  1. private void     
  2. _Init()      
  3. {      
  4.   m_paint = new Paint(Paint.ANTI_ALIAS_FLAG);      
  5.   LinearGradient lg = new LinearGradient(      
  6.     000, m_nShadowH,       
  7.     0xB0FFFFFF0x00000000,      
  8.     Shader.TileMode.CLAMP);      
  9.   m_paint.setShader(lg);      
  10.   m_paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY));      
  11. }      
  12.      
  13. @Override protected void       
  14. onDraw(Canvas canvas)      
  15. {      
  16.   super.onDraw(canvas);      
  17.      
  18.   int nX = 0;      
  19.   int nY = 20;      
  20.      
  21.   _DrawNormalImg(canvas, nX, nY);      
  22.   _DrawMirror(canvas, nX, nY);      
  23. }       
  24.      
  25. private void     
  26. _DrawNormalImg(Canvas canvas, int nX, int nY)      
  27. {      
  28.   canvas.save(Canvas.MATRIX_SAVE_FLAG);      
  29.   canvas.translate(nX, nY);         
  30.   m_dw.draw(canvas);      
  31.   canvas.restore();      
  32. }      
  33.      
  34. private void     
  35. _DrawMirror(Canvas canvas, int nX, int nY)      
  36. {      
  37.   int nW = m_dw.getIntrinsicWidth();      
  38.   int nH = m_dw.getIntrinsicHeight();      
  39.      
  40.   ///////////////////////////////////      
  41.   //draw mirror image      
  42.   canvas.save(Canvas.MATRIX_SAVE_FLAG);      
  43.   canvas.scale(1.0f, -1.0f);      
  44.   canvas.translate(nX, -(nY + nH * 2));      
  45.   canvas.clipRect(0, nH, nW, nH - m_nShadowH);      
  46.   m_dw.draw(canvas);      
  47.   canvas.restore();      
  48.      
  49.   //////////////////////////////      
  50.   //draw mask      
  51.   canvas.save();      
  52.   canvas.translate(nX, nY + nH);      
  53.   canvas.drawRect(00, nW, m_nShadowH, m_paint);      
  54.   canvas.restore();      
  55. }    

Android 繪圖座標(biāo)體系預(yù)設(shè)的原點(diǎn)在左上角,X 軸往右是越來(lái)越大的正值,而 Y 軸往下,則是越來(lái)越大的正值。要畫出垂直翻轉(zhuǎn)的圖片,其實(shí)也就是要垂直翻轉(zhuǎn)整個(gè)繪圖座標(biāo)體系。在 Android 中,要如何做?答案就是 canvas.scale(1.0f, -1.0f)。很簡(jiǎn)單吧,沒(méi)想到給 scale() 函式一個(gè)負(fù)值,就可以翻轉(zhuǎn)相對(duì)應(yīng)的軸。
在 Photoshop 中,做鏡像特效的第二步是要對(duì)這翻轉(zhuǎn)的圖片,加個(gè)由灰到黑的漸層 mask。
在 Android 中,要畫漸層色,那就一定得用 LinearGradient 這個(gè)類別。至於要對(duì)背景圖加上個(gè) mask,就請(qǐng)參考一下 Paint 的 setXfermode() 函式。_Init() 這個(gè)函式,就是負(fù)責(zé)生成一個(gè)由灰到黑漸層 mask 的 m_paint 物件。

 

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多