0
点赞
收藏
分享

微信扫一扫

Android 图片操作



 
1. packagecom.lp.imageUI;    
2.    
3. importandroid.graphics.Bitmap;    
4. importandroid.graphics.Canvas;    
5. importandroid.graphics.LinearGradient;    
6. importandroid.graphics.Matrix;    
7. importandroid.graphics.Paint;    
8. importandroid.graphics.PixelFormat;    
9. importandroid.graphics.PorterDuffXfermode;    
10. importandroid.graphics.Rect;    
11. importandroid.graphics.RectF;    
12. importandroid.graphics.Bitmap.Config;    
13. importandroid.graphics.PorterDuff.Mode;    
14. importandroid.graphics.Shader.TileMode;    
15. importandroid.graphics.drawable.Drawable;    
16. publicclassImageUtil {    
17.        
18.    //放大缩小图片   
19.    publicstaticBitmap zoomBitmap(Bitmap bitmap,intw,inth){    
20.        intwidth = bitmap.getWidth();    
21.        intheight = bitmap.getHeight();    
22.        Matrix matrix = newMatrix();    
23.        floatscaleWidht = ((float)w / width);    
24.        floatscaleHeight = ((float)h / height);    
25.        matrix.postScale(scaleWidht, scaleHeight);    
26.        Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);    
27.        returnnewbmp;    
28.    }    
29.    //将Drawable转化为Bitmap   
30.     publicstaticBitmap drawableToBitmap(Drawable drawable){    
31.            intwidth = drawable.getIntrinsicWidth();    
32.            intheight = drawable.getIntrinsicHeight();    
33.            Bitmap bitmap = Bitmap.createBitmap(width, height,    
34.                    drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888    
35.                            : Bitmap.Config.RGB_565);    
36.            Canvas canvas = newCanvas(bitmap);    
37.            drawable.setBounds(0,0,width,height);    
38.            drawable.draw(canvas);    
39.            returnbitmap;    
40.                
41.        }    
42.         
43.     //获得圆角图片的方法   
44.    publicstaticBitmap getRoundedCornerBitmap(Bitmap bitmap,floatroundPx){    
45.            
46.        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap    
47.                .getHeight(), Config.ARGB_8888);    
48.        Canvas canvas = newCanvas(output);    
49.     
50.        finalintcolor = 0xff424242;    
51.        finalPaint paint = newPaint();    
52.        finalRect rect = newRect(0, 0, bitmap.getWidth(), bitmap.getHeight());    
53.        finalRectF rectF = newRectF(rect);    
54.     
55.        paint.setAntiAlias(true);    
56.        canvas.drawARGB(0, 0, 0, 0);    
57.        paint.setColor(color);    
58.           
59.        canvas.drawRoundRect(rectF, roundPx, roundPx+10, paint);    
60.        //设置两张图片相交时的模式   
61.        //setXfermode前的是 dst 之后的是src   
62.        //在正常的情况下,在已有的图像上绘图将会在其上面添加一层新的形状。    
63.        //如果新的Paint是完全不透明的,那么它将完全遮挡住下面的Paint;   
64.        //PorterDuffXfermode就可以来解决这个问题   
65.        //canvas原有的图片 可以理解为背景 就是dst   
66.        //新画上去的图片 可以理解为前景 就是src   
67. //      paint.setXfermode(new PorterDuffXfermode(Mode.SRC_OUT));   
68.        paint.setXfermode(newPorterDuffXfermode(Mode.SRC_IN));    
69.        canvas.drawBitmap(bitmap, rect, rect, paint);    
70.     
71.        returnoutput;    
72.    }    
73.    //获得带倒影的图片方法   
74.    publicstaticBitmap createReflectionImageWithOrigin(Bitmap bitmap){    
75.        // 图片与倒影间隔距离     
76.        finalintreflectionGap = 4;    
77.        // 图片的宽度     
78.        intwidth = bitmap.getWidth();    
79.         // 图片的高度     
80.        intheight = bitmap.getHeight();    
81.            
82.        Matrix matrix = newMatrix();    
83.        // 图片缩放,x轴变为原来的1倍,y轴为-1倍,实现图片的反转   
84.        matrix.preScale(1, -1);    
85.        // 创建反转后的图片Bitmap对象,图片高是原图的一半。     
86.        Bitmap reflectionImage = Bitmap.createBitmap(bitmap,     
87.                0, height/2, width, height/2, matrix, false);    
88.        // 创建标准的Bitmap对象,宽和原图一致,高是原图的1.5倍。 可以理解为这张图将会在屏幕上显示 是原图和倒影的合体     
89.        Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height/2), Config.ARGB_8888);    
90.         // 构造函数传入Bitmap对象,为了在图片上画图     
91.        Canvas canvas = newCanvas(bitmapWithReflection);    
92.        // 画原始图片     
93.        canvas.drawBitmap(bitmap, 0, 0, null);    
94.        // 画间隔矩形     
95.        Paint deafalutPaint = newPaint();    
96.        canvas.drawRect(0, height,width,height + reflectionGap,    
97.                deafalutPaint);    
98.        // 画倒影图片     
99.        canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);    
100.        // 实现倒影渐变效果     
101.        Paint paint = newPaint();    
102.        LinearGradient shader = newLinearGradient(0,    
103.                bitmap.getHeight(), 0, bitmapWithReflection.getHeight()    
104.                + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);    
105.        paint.setShader(shader);    
106.            
107.        // Set the Transfer mode to be porter duff and destination in   
108.        // 覆盖效果     
109.        paint.setXfermode(newPorterDuffXfermode(Mode.DST_IN));    
110.        // Draw a rectangle using the paint with our linear gradient   
111.        canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()    
112.                + reflectionGap, paint);    
113.     
114.        returnbitmapWithReflection;    
115.    }    
116.        
117. }

举报

相关推荐

0 条评论