现在的位置: 首页 > 综合 > 正文

Android — 图片的特效处理

2017年11月19日 ⁄ 综合 ⁄ 共 6933字 ⁄ 字号 评论关闭

IT程序员开发必备-各类资源下载清单,史上最全IT资源,个人收藏总结!

Android --- 图片处理的方法

转换 -  drawable To  bitmap

缩放 -  Zoom

圆角 -  Round Corner

倒影 -  Reflected


bitmapPrcess  code:

  1. package com.learn.games;  
  2.   
  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.   
  17. public class bitmapProcess {  
  18.   
  19.     // zoom  
  20.     public static Bitmap zoomBitmap(Bitmap bitmap, int w, int h){  
  21.         int width = bitmap.getWidth();  
  22.         int height = bitmap.getHeight();  
  23.         Matrix matrix = new Matrix();  
  24.           
  25.         float scaleWidth = w/(float)width;  
  26.         float scaleHeight = h/(float)height;  
  27.         matrix.postScale(scaleWidth, scaleHeight);  
  28.           
  29.         Bitmap bitmap2 = Bitmap.createBitmap(bitmap, 00, width, height, matrix, true);  
  30.         return bitmap2;       
  31.     }  
  32.       
  33.       
  34.     // drawable to bitmap  
  35.     public static Bitmap drawable2Bitmap(Drawable drawable){  
  36.         int width = drawable.getIntrinsicHeight();  
  37.         int height = drawable.getIntrinsicHeight();  
  38.           
  39.         Bitmap bitmap = Bitmap.createBitmap(width, height, drawable.getOpacity()   
  40.                 != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);  
  41.           
  42.         Canvas canvas = new Canvas(bitmap);  
  43.         drawable.setBounds(00, width, height);  
  44.         drawable.draw(canvas);  
  45.           
  46.         return bitmap;  
  47.     }  
  48.       
  49.       
  50.     // Round Corner Bitmap  
  51.     public static Bitmap getRoundCornerBitmap(Bitmap bitmap, float roundPX){  
  52.         int width = bitmap.getWidth();  
  53.         int height = bitmap.getHeight();  
  54.           
  55.         Bitmap bitmap2 = Bitmap.createBitmap(width, height, Config.ARGB_8888);  
  56.         Canvas canvas = new Canvas(bitmap2);  
  57.           
  58.         final int color = 0xff424242;  
  59.         final Paint paint = new Paint();  
  60.         final Rect rect = new Rect(00, width, height);  
  61.         final RectF rectF = new RectF(rect);  
  62.   
  63.         paint.setColor(color);  
  64.         paint.setAntiAlias(true);  
  65.         canvas.drawARGB(0000);  
  66.         canvas.drawRoundRect(rectF, roundPX, roundPX, paint);  
  67.           
  68.         paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));  
  69.         canvas.drawBitmap(bitmap, rect, rect, paint);  
  70.           
  71.         return bitmap2;  
  72.     }  
  73.       
  74.     // Reflect Bitmap  
  75.     public static Bitmap createReflectedBitmap(Bitmap bitmap){  
  76.         final int reflectedGap = 4;  
  77.         int width = bitmap.getWidth();  
  78.         int height = bitmap.getHeight();  
  79.           
  80.         Matrix matrix = new Matrix();  
  81.         matrix.preScale(1, -1);  
  82.           
  83.         Bitmap reflectedImage = Bitmap.createBitmap(bitmap, 0, height/2, width, height/2, matrix, false);  
  84.         Bitmap reflectedBitmap = Bitmap.createBitmap(width, (height + height/2), Config.ARGB_8888);  
  85.           
  86.         Canvas canvas = new Canvas(reflectedBitmap);  
  87.         canvas.drawBitmap(bitmap, 00null);  
  88.         Paint defaultPaint = new Paint();  
  89.         canvas.drawRect(0, height, width, height + reflectedGap, defaultPaint);  
  90.         canvas.drawBitmap(reflectedImage, 0, height + reflectedGap, null);  
  91.           
  92.         Paint paint = new Paint();  
  93.         LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,  
  94.                                 reflectedBitmap.getHeight() + reflectedGap, 0x70ffffff0x00ffffff, TileMode.CLAMP);  
  95.         paint.setShader(shader);  
  96.         paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));  
  97.         canvas.drawRect(0, height, width, reflectedBitmap.getHeight() + reflectedGap, paint);  
  98.           
  99.         return reflectedBitmap;  
  100.     }  
  101.       
  102. }  


Java Code:

  1. package com.learn.games;  
  2.   
  3. import android.app.Activity;  
  4. import android.graphics.Bitmap;  
  5. import android.graphics.drawable.BitmapDrawable;  
  6. import android.graphics.drawable.Drawable;  
  7. import android.os.Bundle;  
  8. import android.widget.ImageView;  
  9.   
  10. public class MyBitmapProcessActivity extends Activity {  
  11.     private ImageView imgView1;  
  12.     private ImageView imgView2;  
  13.     private ImageView imgView3;  
  14.     private ImageView imgView4;  
  15.       
  16.     /** Called when the activity is first created. */  
  17.     @Override  
  18.     public void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.main);  
  21.           
  22.         drawBitmap();  
  23.     }  
  24.       
  25.       
  26.     private void drawBitmap(){  
  27.         imgView1 = (ImageView)findViewById(R.id.imgView1);  
  28.         imgView2 = (ImageView)findViewById(R.id.imgView2);  
  29.         imgView3 = (ImageView)findViewById(R.id.imgView3);  
  30.         imgView4 = (ImageView)findViewById(R.id.imgView4);  
  31.           
  32.         Drawable drawable = this.getWallpaper();  
  33.           
  34.         Bitmap bitmap = bitmapProcess.drawable2Bitmap(drawable);                        // drawable to bitmap  
  35.         Bitmap zoomBitmap = bitmapProcess.zoomBitmap(bitmap, 100100);                 // zoom   
  36.         Bitmap roundBitmap = bitmapProcess.getRoundCornerBitmap(zoomBitmap, 10.0f);     // round corner   
  37.         Bitmap reflectedBitmap = bitmapProcess.createReflectedBitmap(zoomBitmap);       // reflect bitmap  
  38.           
  39.         // drawable to bitmap  
  40.         imgView1.setImageBitmap(bitmap);  
  41.         imgView2.setImageBitmap(zoomBitmap);  
  42.         imgView3.setImageBitmap(roundBitmap);  
  43.         imgView4.setImageBitmap(reflectedBitmap);  
  44.           
  45.         // bitmap to drawable  
  46.         Drawable roundDrawable = new BitmapDrawable(roundBitmap);  
  47.         Drawable reflectedDrawable = new BitmapDrawable(reflectedBitmap);  
  48.         imgView1.setBackgroundDrawable(roundDrawable);  
  49.         imgView2.setBackgroundDrawable(reflectedDrawable);  
  50.     }  
  51. }  


XML:

  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.    
  8.      <ImageView android:id="@+id/imgView1"  
  9.         android:layout_width="wrap_content"   
  10.         android:layout_height="wrap_content"/>  
  11.           
  12.           
  13.     <ImageView android:id="@+id/imgView2"  
  14.         android:layout_width="wrap_content"   
  15.         android:layout_height="wrap_content"/>  
  16.       
  17.     <ImageView android:id="@+id/imgView3"  
  18.         android:layout_width="wrap_content"   
  19.         android:layout_height="wrap_content"/>  
  20.           
  21.           
  22.     <ImageView android:id="@+id/imgView4"  
  23.         android:layout_width="wrap_content"   
  24.         android:layout_height="wrap_content"/>  
  25.           
  26. </LinearLayout>  


效果图:

drawable2bitmap

zoom

round corner

reflected bitmap

抱歉!评论已关闭.