本文实例总结了android编程中图片特效处理方法。分享给大家供大家参考,具体如下:
这里介绍的android图片处理方法包括:
转换 - drawable to bitmap
缩放 - zoom
圆角 - round corner
倒影 - reflected
bitmapprcess code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
package com.learn.games; import android.graphics.bitmap; import android.graphics.canvas; import android.graphics.lineargradient; import android.graphics.matrix; import android.graphics.paint; import android.graphics.pixelformat; import android.graphics.porterduffxfermode; import android.graphics.rect; import android.graphics.rectf; import android.graphics.bitmap.config; import android.graphics.porterduff.mode; import android.graphics.shader.tilemode; import android.graphics.drawable.drawable; public class bitmapprocess { // zoom public static bitmap zoombitmap(bitmap bitmap, int w, int h){ int width = bitmap.getwidth(); int height = bitmap.getheight(); matrix matrix = new matrix(); float scalewidth = w/( float )width; float scaleheight = h/( float )height; matrix.postscale(scalewidth, scaleheight); bitmap bitmap2 = bitmap.createbitmap(bitmap, 0 , 0 , width, height, matrix, true ); return bitmap2; } // drawable to bitmap public static bitmap drawable2bitmap(drawable drawable){ int width = drawable.getintrinsicheight(); int height = drawable.getintrinsicheight(); bitmap bitmap = bitmap.createbitmap(width, height, drawable.getopacity() != pixelformat.opaque ? bitmap.config.argb_8888 : bitmap.config.rgb_565); canvas canvas = new canvas(bitmap); drawable.setbounds( 0 , 0 , width, height); drawable.draw(canvas); return bitmap; } // round corner bitmap public static bitmap getroundcornerbitmap(bitmap bitmap, float roundpx){ int width = bitmap.getwidth(); int height = bitmap.getheight(); bitmap bitmap2 = bitmap.createbitmap(width, height, config.argb_8888); canvas canvas = new canvas(bitmap2); final int color = 0xff424242 ; final paint paint = new paint(); final rect rect = new rect( 0 , 0 , width, height); final rectf rectf = new rectf(rect); paint.setcolor(color); paint.setantialias( true ); canvas.drawargb( 0 , 0 , 0 , 0 ); canvas.drawroundrect(rectf, roundpx, roundpx, paint); paint.setxfermode( new porterduffxfermode(mode.src_in)); canvas.drawbitmap(bitmap, rect, rect, paint); return bitmap2; } // reflect bitmap public static bitmap createreflectedbitmap(bitmap bitmap){ final int reflectedgap = 4 ; int width = bitmap.getwidth(); int height = bitmap.getheight(); matrix matrix = new matrix(); matrix.prescale( 1 , - 1 ); bitmap reflectedimage = bitmap.createbitmap(bitmap, 0 , height/ 2 , width, height/ 2 , matrix, false ); bitmap reflectedbitmap = bitmap.createbitmap(width, (height + height/ 2 ), config.argb_8888); canvas canvas = new canvas(reflectedbitmap); canvas.drawbitmap(bitmap, 0 , 0 , null ); paint defaultpaint = new paint(); canvas.drawrect( 0 , height, width, height + reflectedgap, defaultpaint); canvas.drawbitmap(reflectedimage, 0 , height + reflectedgap, null ); paint paint = new paint(); lineargradient shader = new lineargradient( 0 , bitmap.getheight(), 0 , reflectedbitmap.getheight() + reflectedgap, 0x70ffffff , 0x00ffffff , tilemode.clamp); paint.setshader(shader); paint.setxfermode( new porterduffxfermode(mode.dst_in)); canvas.drawrect( 0 , height, width, reflectedbitmap.getheight() + reflectedgap, paint); return reflectedbitmap; } } |
java code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
package com.learn.games; import android.app.activity; import android.graphics.bitmap; import android.graphics.drawable.bitmapdrawable; import android.graphics.drawable.drawable; import android.os.bundle; import android.widget.imageview; public class mybitmapprocessactivity extends activity { private imageview imgview1; private imageview imgview2; private imageview imgview3; private imageview imgview4; /** called when the activity is first created. */ @override public void oncreate(bundle savedinstancestate) { super .oncreate(savedinstancestate); setcontentview(r.layout.main); drawbitmap(); } private void drawbitmap(){ imgview1 = (imageview)findviewbyid(r.id.imgview1); imgview2 = (imageview)findviewbyid(r.id.imgview2); imgview3 = (imageview)findviewbyid(r.id.imgview3); imgview4 = (imageview)findviewbyid(r.id.imgview4); drawable drawable = this .getwallpaper(); bitmap bitmap = bitmapprocess.drawable2bitmap(drawable); // drawable to bitmap bitmap zoombitmap = bitmapprocess.zoombitmap(bitmap, 100 , 100 ); // zoom bitmap roundbitmap = bitmapprocess.getroundcornerbitmap(zoombitmap, 10 .0f); // round corner bitmap reflectedbitmap = bitmapprocess.createreflectedbitmap(zoombitmap); // reflect bitmap // drawable to bitmap imgview1.setimagebitmap(bitmap); imgview2.setimagebitmap(zoombitmap); imgview3.setimagebitmap(roundbitmap); imgview4.setimagebitmap(reflectedbitmap); // bitmap to drawable drawable rounddrawable = new bitmapdrawable(roundbitmap); drawable reflecteddrawable = new bitmapdrawable(reflectedbitmap); imgview1.setbackgrounddrawable(rounddrawable); imgview2.setbackgrounddrawable(reflecteddrawable); } } |
xml:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<?xml version= "1.0" encoding= "utf-8" ?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:orientation= "vertical" android:layout_width= "fill_parent" android:layout_height= "fill_parent" > <imageview android:id= "@+id/imgview1" android:layout_width= "wrap_content" android:layout_height= "wrap_content" /> <imageview android:id= "@+id/imgview2" android:layout_width= "wrap_content" android:layout_height= "wrap_content" /> <imageview android:id= "@+id/imgview3" android:layout_width= "wrap_content" android:layout_height= "wrap_content" /> <imageview android:id= "@+id/imgview4" android:layout_width= "wrap_content" android:layout_height= "wrap_content" /> </linearlayout> |
效果图:
drawable2bitmap
zoom
round corner
reflected bitmap
希望本文所述对大家android程序设计有所帮助。