Android 图片处理特效

来源:互联网 发布:新手开淘宝店流程 编辑:程序博客网 时间:2024/05/07 19:36
 

Android --- 图片处理的方法

转换 -  drawable To  bitmap

缩放 -  Zoom

圆角 -  Round Corner

倒影 -  Reflected


bitmapPrcess  code:

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

Java Code:

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

XML:

<?xml version="1.0" encoding="utf-8"?>  
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:orientation="vertical"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     >  
  6.    
  7.      <ImageView android:id="@+id/imgView1"  
  8.         android:layout_width="wrap_content"   
  9.         android:layout_height="wrap_content"/>  
  10.           
  11.           
  12.     <ImageView android:id="@+id/imgView2"  
  13.         android:layout_width="wrap_content"   
  14.         android:layout_height="wrap_content"/>  
  15.       
  16.     <ImageView android:id="@+id/imgView3"  
  17.         android:layout_width="wrap_content"   
  18.         android:layout_height="wrap_content"/>  
  19.           
  20.           
  21.     <ImageView android:id="@+id/imgView4"  
  22.         android:layout_width="wrap_content"   
  23.         android:layout_height="wrap_content"/>  
  24.           
  25. </LinearLayout>  

效果图:

drawable2bitmap


zoom


round corner


reflected bitmap