그림자 효과 이미지 비트맵의 함수

4343 단어
그림자 효과를 넣은 비트맵의 함수입니다.코드는 다음과 같습니다.
 
view sourceprint?
01 package com.util;  

02    

03 import android.graphics.Bitmap;  

04 import android.graphics.Bitmap.Config;  

05 import android.graphics.Canvas;  

06 import android.graphics.Color;  

07 import android.graphics.LinearGradient;  

08 import android.graphics.Matrix;  

09 import android.graphics.Paint;  

10 import android.graphics.PorterDuff.Mode;  

11 import android.graphics.PorterDuffXfermode;  

12 import android.graphics.Shader.TileMode;  

13    

14 public class BitmapUtil {  

15    

16     public static Bitmap createTxtImage(String txt, int txtSize) {  

17         Bitmap mbmpTest = Bitmap.createBitmap(txt.length() * txtSize + 4,  

18                 txtSize + 4, Config.ARGB_8888);  

19         Canvas canvasTemp = new Canvas(mbmpTest);  

20         Paint p = new Paint();  

21         p.setAntiAlias(true);  

22         p.setColor(Color.WHITE);  

23         p.setTextSize(txtSize);  

24         canvasTemp.drawText(txt, 2, txtSize - 2, p);  

25         return mbmpTest;  

26     }  

27        

28     public static Bitmap createReflectedImage(Bitmap originalImage) {  

29         // The gap we want between the reflection and the original image  

30         final int reflectionGap = 0;  

31    

32         int width = originalImage.getWidth();  

33         int height = originalImage.getHeight();  

34    

35         // This will not scale but will flip on the Y axis  

36         Matrix matrix = new Matrix();  

37         matrix.preScale(1, -1);  

38    

39         // Create a Bitmap with the flip matrix applied to it.  

40         // We only want the bottom half of the image  

41         Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0,  

42                 height / 2, width, height / 2, matrix, false);  

43    

44         // Create a new bitmap with same width but taller to fit reflection  

45         Bitmap bitmapWithReflection = Bitmap.createBitmap(width,  

46                 (height + height / 2), Config.ARGB_8888);  

47    

48         // Create a new Canvas with the bitmap that's big enough for  

49         // the image plus gap plus reflection  

50         Canvas canvas = new Canvas(bitmapWithReflection);  

51         // Draw in the original image  

52         canvas.drawBitmap(originalImage, 0, 0, null);  

53         // Draw in the gap  

54         Paint defaultPaint = new Paint();  

55         canvas.drawRect(0, height, width, height + reflectionGap, defaultPaint);  

56         // Draw in the reflection  

57         canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);  

58    

59         // Create a shader that is a linear gradient that covers the reflection  

60         Paint paint = new Paint();  

61         LinearGradient shader = new LinearGradient(0,  

62                 originalImage.getHeight(), 0, bitmapWithReflection.getHeight()  

63                         + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);  

64         // Set the paint to use this shader (linear gradient)  

65         paint.setShader(shader);  

66         // Set the Transfer mode to be porter duff and destination in  

67         paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));  

68         // Draw a rectangle using the paint with our linear gradient  

69         canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()  

70                 + reflectionGap, paint);  

71    

72         return bitmapWithReflection;  

73     }  

74 } 



좋은 웹페이지 즐겨찾기