Android中几种图像特效处理

大家好,这一节给大家分享的是Android中几种图像特效处理的小技巧,比如圆角,倒影,还有就是图片缩放,Drawable转化为Bitmap,Bitmap转化为Drawable等等.

废话少说了,直接讲解今天的实例,本例主要是先获取壁纸(getWallpaper()),然后对当前壁纸的一些特效处理.大家按步骤一步一步来:

第一步:新建一个Android工程命名为ImageDemo,工程结构如下:

第二步:新建一个.java文件,命名为ImageUtil.java,在里面定义一些图片处理方法,代码如下:

view plaincopy to clipboardprint?

  1. package com.android.tutor; 
  2. import android.graphics.Bitmap; 
  3. import android.graphics.Canvas; 
  4. import android.graphics.LinearGradient; 
  5. import android.graphics.Matrix; 
  6. import android.graphics.Paint; 
  7. import android.graphics.PixelFormat; 
  8. import android.graphics.PorterDuffXfermode; 
  9. import android.graphics.Rect; 
  10. import android.graphics.RectF; 
  11. import android.graphics.Bitmap.Config; 
  12. import android.graphics.PorterDuff.Mode; 
  13. import android.graphics.Shader.TileMode; 
  14. import android.graphics.drawable.Drawable; 
  15. public class ImageUtil { 
  16. //放大缩小图片
  17. public static Bitmap zoomBitmap(Bitmap bitmap,int w,int h){ 
  18. int width = bitmap.getWidth(); 
  19. int height = bitmap.getHeight(); 
  20.         Matrix matrix = new Matrix(); 
  21. float scaleWidht = ((float)w / width); 
  22. float scaleHeight = ((float)h / height); 
  23.         matrix.postScale(scaleWidht, scaleHeight); 
  24.         Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true); 
  25. return newbmp; 
  26.     } 
  27. //将Drawable转化为Bitmap
  28. public static Bitmap drawableToBitmap(Drawable drawable){ 
  29. int width = drawable.getIntrinsicWidth(); 
  30. int height = drawable.getIntrinsicHeight(); 
  31.             Bitmap bitmap = Bitmap.createBitmap(width, height, 
  32.                     drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 
  33.                             : Bitmap.Config.RGB_565); 
  34.             Canvas canvas = new Canvas(bitmap); 
  35.             drawable.setBounds(0,0,width,height); 
  36.             drawable.draw(canvas); 
  37. return bitmap; 
  38.         } 
  39. //获得圆角图片的方法
  40. public static Bitmap getRoundedCornerBitmap(Bitmap bitmap,float roundPx){ 
  41.         Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap 
  42.                 .getHeight(), Config.ARGB_8888); 
  43.         Canvas canvas = new Canvas(output); 
  44. final int color = 0xff424242; 
  45. final Paint paint = new Paint(); 
  46. final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); 
  47. final RectF rectF = new RectF(rect); 
  48.         paint.setAntiAlias(true); 
  49.         canvas.drawARGB(0, 0, 0, 0); 
  50.         paint.setColor(color); 
  51.         canvas.drawRoundRect(rectF, roundPx, roundPx, paint); 
  52.         paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); 
  53.         canvas.drawBitmap(bitmap, rect, rect, paint); 
  54. return output; 
  55.     } 
  56. //获得带倒影的图片方法
  57. public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap){ 
  58. final int reflectionGap = 4; 
  59. int width = bitmap.getWidth(); 
  60. int height = bitmap.getHeight(); 
  61.         Matrix matrix = new Matrix(); 
  62.         matrix.preScale(1, -1); 
  63.         Bitmap reflectionImage = Bitmap.createBitmap(bitmap,  
  64. 0, height/2, width, height/2, matrix, false); 
  65.         Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height/2), Config.ARGB_8888); 
  66.         Canvas canvas = new Canvas(bitmapWithReflection); 
  67.         canvas.drawBitmap(bitmap, 0, 0, null); 
  68.         Paint deafalutPaint = new Paint(); 
  69.         canvas.drawRect(0, height,width,height + reflectionGap, 
  70.                 deafalutPaint); 
  71.         canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null); 
  72.         Paint paint = new Paint(); 
  73.         LinearGradient shader = new LinearGradient(0, 
  74.                 bitmap.getHeight(), 0, bitmapWithReflection.getHeight() 
  75.                 + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP); 
  76.         paint.setShader(shader); 
  77. // Set the Transfer mode to be porter duff and destination in
  78.         paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN)); 
  79. // Draw a rectangle using the paint with our linear gradient
  80.         canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() 
  81.                 + reflectionGap, paint); 
  82. return bitmapWithReflection; 
  83.     } 

第三步:修改main.xml布局文件,主要放了两个ImageView控件,代码如下:

view plaincopy to clipboardprint?

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:orientation="vertical"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="fill_parent"
  6.     > 
  7.     <ImageView  
  8.         android:id="@+id/image01"
  9.         android:layout_width="wrap_content"
  10.         android:layout_height="wrap_content"
  11.         android:padding="10px"
  12.         /> 
  13.     <ImageView 
  14.         android:id="@+id/image02"
  15.         android:layout_width="wrap_content"
  16.         android:layout_height="wrap_content"
  17.         android:padding="10px"
  18.     /> 
  19. </LinearLayout> 

第四步:修改主核心程序,ImageDemo.java,代码如下:

view plaincopy to clipboardprint?

  1. package com.android.tutor; 
  2. import android.app.Activity; 
  3. import android.graphics.Bitmap; 
  4. import android.graphics.drawable.Drawable; 
  5. import android.os.Bundle; 
  6. import android.widget.ImageView; 
  7. public class Imagedemo extends Activity { 
  8. private ImageView mImageView01,mImageView02; 
  9. public void onCreate(Bundle savedInstanceState) { 
  10. super.onCreate(savedInstanceState); 
  11.         setContentView(R.layout.main); 
  12.         setupViews(); 
  13.     } 
  14. private void setupViews(){ 
  15.         mImageView01 = (ImageView)findViewById(R.id.image01); 
  16.         mImageView02 = (ImageView)findViewById(R.id.image02); 
  17. //获取壁纸返回值是Drawable
  18.         Drawable drawable = getWallpaper(); 
  19. //将Drawable转化为Bitmap
  20.         Bitmap bitmap = ImageUtil.drawableToBitmap(drawable); 
  21. //缩放图片
  22.         Bitmap zoomBitmap = ImageUtil.zoomBitmap(bitmap, 100, 100); 
  23. //获取圆角图片
  24.         Bitmap roundBitmap = ImageUtil.getRoundedCornerBitmap(zoomBitmap, 10.0f); 
  25. //获取倒影图片
  26.         Bitmap reflectBitmap = ImageUtil.createReflectionImageWithOrigin(zoomBitmap); 
  27. //这里可以让Bitmap再转化为Drawable
  28. //      Drawable roundDrawable = new BitmapDrawable(roundBitmap);      
  29. //      Drawable reflectDrawable = new BitmapDrawable(reflectBitmap);      
  30. //      mImageView01.setBackgroundDrawable(roundDrawable);
  31. //      mImageView02.setBackgroundDrawable(reflectDrawable);
  32.         mImageView01.setImageBitmap(roundBitmap); 
  33.         mImageView02.setImageBitmap(reflectBitmap); 
  34.     } 

第五步:运行上述工程,查看效果如下:

OK大功告成了!!

 

 

作者:Android_Tutor

出处:http://blog.csdn.net/Android_Tutor/archive/2010/11/02/5981753.aspx

posted @ 2011-06-24 14:40  Gang.Wang  阅读(1781)  评论(0编辑  收藏  举报