Android 几种图像特效处理的集锦

第一步:新建一个Android工程命名为ImageDemo,工程结构如下:
Android 几种图像特效处理的集锦_第1张图片 

第二步.java文件ImageUtil.java,在里面定义一些图片处理方法<span color:#333333;"="" style="font-size: 10.5pt; ">,代码如下:

001 package com.android.tutor; 
002 import android.graphics.Bitmap; 
003 import android.graphics.Canvas; 
004 import android.graphics.LinearGradient; 
005 import android.graphics.Matrix; 
006 import android.graphics.Paint; 
007 import android.graphics.PixelFormat; 
008 import android.graphics.PorterDuffXfermode; 
009 import android.graphics.Rect; 
010 import android.graphics.RectF; 
011 import android.graphics.Bitmap.Config; 
012 import android.graphics.PorterDuff.Mode; 
013 import android.graphics.Shader.TileMode; 
014 import android.graphics.drawable.Drawable; 
015 public class ImageUtil { 
016        
017     //放大缩小图片  
018     public static Bitmap zoomBitmap(Bitmap bitmap,int w,int h){ 
019         int width = bitmap.getWidth(); 
020         int height = bitmap.getHeight(); 
021         Matrix matrix = new Matrix(); 
022         float scaleWidht = ((float)w / width); 
023         float scaleHeight = ((float)h / height); 
024         matrix.postScale(scaleWidht, scaleHeight); 
025         Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true); 
026         return newbmp; 
027     
028     //将Drawable转化为Bitmap  
029      public static Bitmap drawableToBitmap(Drawable drawable){ 
030             int width = drawable.getIntrinsicWidth(); 
031             int height = drawable.getIntrinsicHeight(); 
032             Bitmap bitmap = Bitmap.createBitmap(width, height, 
033                     drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 
034                             : Bitmap.Config.RGB_565); 
035             Canvas canvas = new Canvas(bitmap); 
036             drawable.setBounds(0,0,width,height); 
037             drawable.draw(canvas); 
038             return bitmap; 
039                
040         
041         
042      //获得圆角图片的方法  
043     public static Bitmap getRoundedCornerBitmap(Bitmap bitmap,float roundPx){ 
044            
045         Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap 
046                 .getHeight(), Config.ARGB_8888); 
047         Canvas canvas = new Canvas(output); 
048     
049         final int color = 0xff424242
050         final Paint paint = new Paint(); 
051         final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); 
052         final RectF rectF = new RectF(rect); 
053     
054         paint.setAntiAlias(true); 
055         canvas.drawARGB(0, 0, 0, 0); 
056         paint.setColor(color); 
057         canvas.drawRoundRect(rectF, roundPx, roundPx, paint); 
058     
059         paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); 
060         canvas.drawBitmap(bitmap, rect, rect, paint); 
061     
062         return output; 
063     
064     //获得带倒影的图片方法  
065     public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap){ 
066         final int reflectionGap = 4
067         int width = bitmap.getWidth(); 
068         int height = bitmap.getHeight(); 
069            
070         Matrix matrix = new Matrix(); 
071         matrix.preScale(1, -1); 
072            
073         Bitmap reflectionImage = Bitmap.createBitmap(bitmap,  
074                 0, height/2, width, height/2, matrix, false); 
075            
076         Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height/2), Config.ARGB_8888); 
077            
078         Canvas canvas = new Canvas(bitmapWithReflection); 
079         canvas.drawBitmap(bitmap, 0, 0, null); 
080         Paint deafalutPaint = new Paint(); 
081         canvas.drawRect(0, height,width,height + reflectionGap, 
082                 deafalutPaint); 
083            
084         canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null); 
085            
086         Paint paint = new Paint(); 
087         LinearGradient shader = new LinearGradient(0
088                 bitmap.getHeight(), 0, bitmapWithReflection.getHeight() 
089                 + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP); 
090         paint.setShader(shader); 
091         // Set the Transfer mode to be porter duff and destination in  
092         paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN)); 
093         // Draw a rectangle using the paint with our linear gradient  
094         canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() 
095                 + reflectionGap, paint); 
096     
097         return bitmapWithReflection; 
098     
099        
100 }
第三步:修改main.xml布局文件,主要放了两个ImageView控件,代码如下:
1 <!--?xml version="1.0" encoding="utf-8"?-->   

你可能感兴趣的:(Android 几种图像特效处理的集锦)