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

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

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

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

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

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

  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 BitmapzoomBitmap(Bitmapbitmap, int w, int h){
  18. int width=bitmap.getWidth();
  19. int height=bitmap.getHeight();
  20. Matrixmatrix=new Matrix();
  21. float scaleWidht=(( float )w/width);
  22. float scaleHeight=(( float )h/height);
  23. matrix.postScale(scaleWidht,scaleHeight);
  24. Bitmapnewbmp=Bitmap.createBitmap(bitmap,0 , 0 ,width,height,matrix, true );
  25. return newbmp;
  26. }
  27. //将Drawable转化为Bitmap
  28. public static BitmapdrawableToBitmap(Drawabledrawable){
  29. int width=drawable.getIntrinsicWidth();
  30. int height=drawable.getIntrinsicHeight();
  31. Bitmapbitmap=Bitmap.createBitmap(width,height,
  32. drawable.getOpacity()!=PixelFormat.OPAQUE?Bitmap.Config.ARGB_8888
  33. :Bitmap.Config.RGB_565);
  34. Canvascanvas=new Canvas(bitmap);
  35. drawable.setBounds(0 , 0 ,width,height);
  36. drawable.draw(canvas);
  37. return bitmap;
  38. }
  39. //获得圆角图片的方法
  40. public static BitmapgetRoundedCornerBitmap(Bitmapbitmap, float roundPx){
  41. Bitmapoutput=Bitmap.createBitmap(bitmap.getWidth(),bitmap
  42. .getHeight(),Config.ARGB_8888);
  43. Canvascanvas=new Canvas(output);
  44. final int color= 0xff424242 ;
  45. final Paintpaint= new Paint();
  46. final Rectrect= new Rect( 0 , 0 ,bitmap.getWidth(),bitmap.getHeight());
  47. final RectFrectF= 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 BitmapcreateReflectionImageWithOrigin(Bitmapbitmap){
  58. final int reflectionGap= 4 ;
  59. int width=bitmap.getWidth();
  60. int height=bitmap.getHeight();
  61. Matrixmatrix=new Matrix();
  62. matrix.preScale(1 ,- 1 );
  63. BitmapreflectionImage=Bitmap.createBitmap(bitmap,
  64. 0 ,height/ 2 ,width,height/ 2 ,matrix, false );
  65. BitmapbitmapWithReflection=Bitmap.createBitmap(width,(height+height/2 ),Config.ARGB_8888);
  66. Canvascanvas=new Canvas(bitmapWithReflection);
  67. canvas.drawBitmap(bitmap,0 , 0 , null );
  68. PaintdeafalutPaint=new Paint();
  69. canvas.drawRect(0 ,height,width,height+reflectionGap,
  70. deafalutPaint);
  71. canvas.drawBitmap(reflectionImage,0 ,height+reflectionGap, null );
  72. Paintpaint=new Paint();
  73. LinearGradientshader=new LinearGradient( 0 ,
  74. bitmap.getHeight(),0 ,bitmapWithReflection.getHeight()
  75. +reflectionGap,0x70ffffff , 0x00ffffff ,TileMode.CLAMP);
  76. paint.setShader(shader);
  77. //SettheTransfermodetobeporterduffanddestinationin
  78. paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
  79. //Drawarectangleusingthepaintwithourlineargradient
  80. canvas.drawRect(0 ,height,width,bitmapWithReflection.getHeight()
  81. +reflectionGap,paint);
  82. return bitmapWithReflection;
  83. }
  84. }

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

  1. <?xmlversion= "1.0" encoding= "utf-8" ?>
  2. <LinearLayoutxmlns: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,代码如下:

  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 ImageViewmImageView01,mImageView02;
  9. public void onCreate(BundlesavedInstanceState){
  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. Drawabledrawable=getWallpaper();
  19. //将Drawable转化为Bitmap
  20. Bitmapbitmap=ImageUtil.drawableToBitmap(drawable);
  21. //缩放图片
  22. BitmapzoomBitmap=ImageUtil.zoomBitmap(bitmap,100 , 100 );
  23. //获取圆角图片
  24. BitmaproundBitmap=ImageUtil.getRoundedCornerBitmap(zoomBitmap,10 .0f);
  25. //获取倒影图片
  26. BitmapreflectBitmap=ImageUtil.createReflectionImageWithOrigin(zoomBitmap);
  27. //这里可以让Bitmap再转化为Drawable
  28. //DrawableroundDrawable=newBitmapDrawable(roundBitmap);
  29. //DrawablereflectDrawable=newBitmapDrawable(reflectBitmap);
  30. //mImageView01.setBackgroundDrawable(roundDrawable);
  31. //mImageView02.setBackgroundDrawable(reflectDrawable);
  32. mImageView01.setImageBitmap(roundBitmap);
  33. mImageView02.setImageBitmap(reflectBitmap);
  34. }
  35. }

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

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

OK大功告成了!!

本文参考文献:http://wiki.impjq.net/doku.php?id=code:android-code:image-convert&rev=1275640889&mddo=print

你可能感兴趣的:(android)