大家好,这一节给大家分享的是Android图像特效处理的小技巧,比如圆角、倒影、还有就是图片缩放、Drawable转化为Bitmap、Bitmap转化为Drawable等等。
废话少说,直接讲解今天的实例,本例主要是先获取壁纸(getWallpaper()),然后对当前壁纸的一些特效处理。大家按步骤一步一步来:
第一步:新建一个Android工程命名为ImageDemo,工程结构如下:
![ImageDemo](http://www.devdiv.com/data/attachment/blog/201208/22/1420365sh43thkt2nzsbkh.jpg)
第二步:新建一个.java文件,命名为ImageUtil.java,在里面定义一些图片处理方法,代码如下:
[代码]java代码:
001 | package com.android.tutor; |
003 | import android.graphics.Bitmap; |
004 | import android.graphics.Canvas; |
005 | import android.graphics.LinearGradient; |
006 | import android.graphics.Matrix; |
007 | import android.graphics.Paint; |
008 | import android.graphics.PixelFormat; |
009 | import android.graphics.PorterDuffXfermode; |
010 | import android.graphics.Rect; |
011 | import android.graphics.RectF; |
012 | import android.graphics.Bitmap.Config; |
013 | import android.graphics.PorterDuff.Mode; |
014 | import android.graphics.Shader.TileMode; |
015 | import android.graphics.drawable.Drawable; |
017 | public class ImageUtil { |
020 | public static Bitmap zoomBitmap(Bitmap bitmap, int w, int h) { |
021 | int width = bitmap.getWidth(); |
022 | int height = bitmap.getHeight(); |
023 | Matrix matrix = new Matrix(); |
024 | float scaleWidht = (( float ) w / width); |
025 | float scaleHeight = (( float ) h / height); |
026 | matrix.postScale(scaleWidht, scaleHeight); |
027 | Bitmap newbmp = Bitmap.createBitmap(bitmap, 0 , 0 , width, height, |
033 | public static Bitmap drawableToBitmap(Drawable drawable) { |
034 | int width = drawable.getIntrinsicWidth(); |
035 | int height = drawable.getIntrinsicHeight(); |
036 | Bitmap bitmap = Bitmap.createBitmap(width, height, drawable |
037 | .getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 |
038 | : Bitmap.Config.RGB_565); |
039 | Canvas canvas = new Canvas(bitmap); |
040 | drawable.setBounds( 0 , 0 , width, height); |
041 | drawable.draw(canvas); |
047 | public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) { |
049 | Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), |
050 | bitmap.getHeight(), Config.ARGB_8888); |
051 | Canvas canvas = new Canvas(output); |
053 | final int color = 0xff424242 ; |
054 | final Paint paint = new Paint(); |
055 | final Rect rect = new Rect( 0 , 0 , bitmap.getWidth(), bitmap.getHeight()); |
056 | final RectF rectF = new RectF(rect); |
058 | paint.setAntiAlias( true ); |
059 | canvas.drawARGB( 0 , 0 , 0 , 0 ); |
060 | paint.setColor(color); |
061 | canvas.drawRoundRect(rectF, roundPx, roundPx, paint); |
063 | paint.setXfermode( new PorterDuffXfermode(Mode.SRC_IN)); |
064 | canvas.drawBitmap(bitmap, rect, rect, paint); |
070 | public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap) { |
071 | final int reflectionGap = 4 ; |
072 | int width = bitmap.getWidth(); |
073 | int height = bitmap.getHeight(); |
075 | Matrix matrix = new Matrix(); |
076 | matrix.preScale( 1 , - 1 ); |
078 | Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0 , height / 2 , |
079 | width, height / 2 , matrix, false ); |
081 | Bitmap bitmapWithReflection = Bitmap.createBitmap(width, |
082 | (height + height / 2 ), Config.ARGB_8888); |
084 | Canvas canvas = new Canvas(bitmapWithReflection); |
085 | canvas.drawBitmap(bitmap, 0 , 0 , null ); |
086 | Paint deafalutPaint = new Paint(); |
087 | canvas.drawRect( 0 , height, width, height + reflectionGap, deafalutPaint); |
089 | canvas.drawBitmap(reflectionImage, 0 , height + reflectionGap, null ); |
091 | Paint paint = new Paint(); |
092 | LinearGradient shader = new LinearGradient( 0 , bitmap.getHeight(), 0 , |
093 | bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff , |
094 | 0x00ffffff , TileMode.CLAMP); |
095 | paint.setShader(shader); |
097 | paint.setXfermode( new PorterDuffXfermode(Mode.DST_IN)); |
099 | canvas.drawRect( 0 , height, width, bitmapWithReflection.getHeight() |
100 | + reflectionGap, paint); |
102 | return bitmapWithReflection; |
第三步:修改main.xml布局文件,主要放了两个ImageView控件,代码如下:
[代码]xml代码:
01 | <? xml version = "1.0" encoding = "utf-8" ?> |
02 | < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" |
03 | android:orientation = "vertical" |
04 | android:layout_width = "fill_parent" |
05 | android:layout_height = "fill_parent" |
08 | android:id = "@+id/image01" |
09 | android:layout_width = "wrap_content" |
10 | android:layout_height = "wrap_content" |
11 | android:padding = "10px" |
14 | android:id = "@+id/image02" |
15 | android:layout_width = "wrap_content" |
16 | android:layout_height = "wrap_content" |
17 | android:padding = "10px" |
第四步:修改主核心程序,ImageDemo.java,代码如下:
[代码]java代码:
01 | package com.android.tutor; |
03 | import android.app.Activity; |
04 | import android.graphics.Bitmap; |
05 | import android.graphics.drawable.Drawable; |
06 | import android.os.Bundle; |
07 | import android.widget.ImageView; |
09 | public class Imagedemo extends Activity { |
11 | private ImageView mImageView01, mImageView02; |
13 | public void onCreate(Bundle savedInstanceState) { |
14 | super .onCreate(savedInstanceState); |
15 | setContentView(R.layout.main); |
19 | private void setupViews() { |
21 | mImageView01 = (ImageView) findViewById(R.id.image01); |
22 | mImageView02 = (ImageView) findViewById(R.id.image02); |
25 | Drawable drawable = getWallpaper(); |
27 | Bitmap bitmap = ImageUtil.drawableToBitmap(drawable); |
29 | Bitmap zoomBitmap = ImageUtil.zoomBitmap(bitmap, 100 , 100 ); |
31 | Bitmap roundBitmap = ImageUtil |
32 | .getRoundedCornerBitmap(zoomBitmap, 10 .0f); |
34 | Bitmap reflectBitmap = ImageUtil |
35 | .createReflectionImageWithOrigin(zoomBitmap); |
42 | mImageView01.setImageBitmap(roundBitmap); |
43 | mImageView02.setImageBitmap(reflectBitmap); |
第五步:运行上述工程,查看效果如下:
![](http://122.70.156.176/attachment_remote/blog/201208/22/142058ms15mlm222554sj4.jpg)
OK大功告成了!!