RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
Android中几种图像特效处理

Android中有很多图片特效处理技巧,比如圆角、倒影,今天为大家分享一个实例,本例主要是先获取壁纸(getWallpaper()),然后对当前壁纸的一些特效处理。

班戈ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联建站的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:028-86922220(备注:SSL证书合作)期待与您的合作!

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

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

Java代码:

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

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

Java代码:

 
 
 
 
  1. view plaincopy to clipboardprint? 
  2.  
  3. android:orientation="vertical" 
  4. android:layout_width="fill_parent" 
  5. android:layout_height="fill_parent" 
  6. > 
  7. android:id="@+id/image01" 
  8. android:layout_width="wrap_content" 
  9. android:layout_height="wrap_content" 
  10. android:padding="10px" 
  11. /> 
  12. android:id="@+id/image02" 
  13. android:layout_width="wrap_content" 
  14. android:layout_height="wrap_content" 
  15. android:padding="10px" 
  16. /> 
  17.   

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

Java代码:

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

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


分享文章:Android中几种图像特效处理
标题网址:http://pengzhouwz.cn/article/cocjdeo.html