Calvin's Tech Space

          成于堅(jiān)忍,毀于浮躁

             :: 首頁(yè) :: 聯(lián)系 :: 聚合  :: 管理
          Android中實(shí)現(xiàn)倒影效果

          實(shí)現(xiàn)原理
          實(shí)現(xiàn)倒影可以使用 OpenGL 等 3D 接口方法,也可以用 2D 的方法模擬。
          用 2D 方法實(shí)現(xiàn)倒影需要從兩個(gè)方面考慮:
          倒影是上、下翻轉(zhuǎn)的圖像;
          從上到下透明度越來(lái)越大。

          圖像翻轉(zhuǎn)
          原理上講,圖像的翻轉(zhuǎn)實(shí)際就是將圖像數(shù)據(jù)上下行互換。

          透明度漸變
          實(shí)現(xiàn)透明度漸變有兩種方法,一是使用蒙板;二是直接修改像素?cái)?shù)據(jù),修改每個(gè)像素的 alpha 值。
          對(duì)于蒙板法,事先做好一張透明度漸變的圖,這就是我們的蒙板,在繪制完圖像之后把蒙板圖片繪制上去,這樣就產(chǎn)生了透明度漸變的效果。
          對(duì)于第二種方法,我們需要首先讀出圖片的數(shù)據(jù),然后修改每個(gè)像素的 alpha 值。逐行增加 alpha 值,產(chǎn)生的效果是自上向下由暗變亮。

           1 
           2     /**
           3      * get reflection bitmap of the original bitmap.
           4      * @param srcBitmap
           5      * @return
           6      */
           7     private Bitmap makeReflectionBitmap(Bitmap srcBitmap){
           8         int bmpWidth = srcBitmap.getWidth();
           9         int bmpHeight = srcBitmap.getHeight();
          10         int[] pixels = new int[bmpWidth * bmpHeight * 4];
          11         srcBitmap.getPixels(pixels, 0, bmpWidth, 00, bmpWidth, bmpHeight);
          12 
          13         //get reversed bitmap
          14         Bitmap reverseBitmap = Bitmap.createBitmap(bmpWidth, bmpHeight,
          15                 Bitmap.Config.ARGB_8888);
          16         for (int y = 0; y < bmpHeight; y++) {
          17             reverseBitmap.setPixels(pixels, y * bmpWidth, bmpWidth, 0,
          18                     bmpHeight - y - 1, bmpWidth, 1);
          19         }
          20         
          21         //get reflection bitmap based on the reversed one
          22         reverseBitmap.getPixels(pixels, 0, bmpWidth, 00, bmpWidth, bmpHeight);
          23         Bitmap reflectionBitmap = Bitmap.createBitmap(bmpWidth, bmpHeight,
          24                 Bitmap.Config.ARGB_8888);
          25         int alpha = 0x00000000;
          26         for (int y = 0; y < bmpHeight; y++) {
          27             for (int x = 0; x < bmpWidth; x++) {
          28                 int index = y * bmpWidth + x;
          29                 int r = (pixels[index] >> 16& 0xff;
          30                 int g = (pixels[index] >> 8& 0xff;
          31                 int b = pixels[index] & 0xff;
          32 
          33                 pixels[index] = alpha | (r << 16| (g << 8| b;
          34                 
          35                 reflectionBitmap.setPixel(x, y, pixels[index]);
          36             }
          37             alpha = alpha + 0x01000000;
          38         }
          39         
          40         return reflectionBitmap;
          41     }

          參考 http://www.linuxgraphics.cn/android/reflection_effect_2d.html


          posted on 2010-04-13 15:37 calvin 閱讀(1330) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): Android
          主站蜘蛛池模板: 潞城市| 榆中县| 长宁区| 阿坝| 海兴县| 钟祥市| 江达县| 巴马| 唐河县| 江口县| 突泉县| 通化县| 诏安县| 敦化市| 武宣县| 定日县| 武功县| 石屏县| 澄城县| 济南市| 山西省| 河南省| 鹤岗市| 房产| 内黄县| 天气| 蒙阴县| 朔州市| 攀枝花市| 彭泽县| 同心县| 阳泉市| 山东省| 龙门县| 历史| 海安县| 九龙坡区| 藁城市| 大名县| 肃南| 枞阳县|