android小应用帮美女更衣系列二(附源码)

点击ImageSwitcher显示的图片即可切换到为美女换衣全屏界面,手指在界面上滑动,滑动处的衣服就被褪掉了,很黄很暴力,大家要hold住呀!! 
其实啊这个实现就是两张图片,一张底图,一张上面的图,上面的图都被抹掉了,下面的图就出来了,主要是PorterDuff和PorterDuffXfermode的利用,APIDEMO里面也有相关的介绍。好,贴出主要代码: 

Java代码  收藏代码
  1. package com.picture;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Context;  
  5. import android.content.Intent;  
  6. import android.graphics.Bitmap;  
  7. import android.graphics.Bitmap.Config;  
  8. import android.graphics.BitmapFactory;  
  9. import android.graphics.Canvas;  
  10. import android.graphics.Paint;  
  11. import android.graphics.Path;  
  12. import android.graphics.PorterDuff;  
  13. import android.graphics.PorterDuffXfermode;  
  14. import android.media.MediaPlayer;  
  15. import android.os.Bundle;  
  16. import android.util.DisplayMetrics;  
  17. import android.view.MotionEvent;  
  18. import android.view.View;  
  19. import android.view.WindowManager;  
  20.   
  21. public class RemoveClothActivity extends Activity {  
  22.   
  23.     private int SCREEN_W;  
  24.   
  25.     private int SCREEN_H;  
  26.   
  27.     private int imagePosition;  
  28.       
  29.     private MediaPlayer mp3Player;  
  30.   
  31.     @Override  
  32.     protected void onCreate(Bundle savedInstanceState) {  
  33.         super.onCreate(savedInstanceState);  
  34.         getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//设置全屏  
  35.         Intent intent = getIntent();  
  36.         imagePosition = intent.getIntExtra("imagePosition"0);  
  37.         initMP3Player();  
  38.         setContentView(new MyView(this));  
  39.     }  
  40.       
  41.     private void initMP3Player(){  
  42.         mp3Player = MediaPlayer.create(RemoveClothActivity.this, R.raw.higirl);  
  43.         mp3Player.setLooping(false);  
  44.     }  
  45.       
  46.     private void playMusic(){  
  47.         mp3Player.start();  
  48.     }  
  49.   
  50.     class MyView extends View {  
  51.         private Bitmap mBitmap;  
  52.         private Canvas mCanvas;  
  53.         private Paint mPaint;  
  54.         private Path mPath;  
  55.         private float mX, mY;  
  56.         private static final float TOUCH_TOLERANCE = 4;  
  57.           
  58.         public MyView(Context context) {  
  59.             super(context);  
  60.             setFocusable(true);  
  61.             setScreenWH();  
  62.             setBackGround();  
  63.   
  64.             // 1.if icon1 is a image,you can open MENU_ITEM_COMMENT bellow  
  65.             // Bitmap bm = createBitmapFromSRC();  
  66.             // if you want to set icon1 image's alpha,you can open  
  67.             // MENU_ITEM_COMMENT bellow  
  68.             // bm = setBitmapAlpha(bm, 100);  
  69.             // if you want to scale icon1 image,you can open MENU_ITEM_COMMENT  
  70.             // bellow  
  71.             // bm = scaleBitmapFillScreen(bm);  
  72.   
  73.             // 2.if icon1 is color  
  74.             // Bitmap bm = createBitmapFromARGB(0x8800ff00, SCREEN_W, SCREEN_H);  
  75.             int drawableId = 0;  
  76.             try {  
  77.                 drawableId = R.drawable.class.getDeclaredField(  
  78.                         "pre" + imagePosition).getInt(this);  
  79.             } catch (IllegalArgumentException e) {  
  80.                 e.printStackTrace();  
  81.             } catch (SecurityException e) {  
  82.                 e.printStackTrace();  
  83.             } catch (IllegalAccessException e) {  
  84.                 e.printStackTrace();  
  85.             } catch (NoSuchFieldException e) {  
  86.                 e.printStackTrace();  
  87.             }  
  88.             Bitmap bm = scaleBitmapFillScreen(BitmapFactory.decodeResource(getResources(), drawableId));  
  89.             seticon1Bitmap(bm);  
  90.   
  91.         }  
  92.   
  93.         private void setScreenWH() {  
  94.             // get screen info  
  95.             DisplayMetrics dm = new DisplayMetrics();  
  96.             dm = this.getResources().getDisplayMetrics();  
  97.             // get screen width  
  98.             int screenWidth = dm.widthPixels;  
  99.             // get screen height  
  100.             int screenHeight = dm.heightPixels;  
  101.   
  102.             SCREEN_W = screenWidth;  
  103.             SCREEN_H = screenHeight;  
  104.         }  
  105.   
  106.         private Bitmap createBitmapFromSRC() {  
  107.             return BitmapFactory.decodeResource(getResources(),  
  108.                     R.drawable.icon1);  
  109.         }  
  110.   
  111.         /** 
  112.          *  
  113.          * @param colorARGB 
  114.          *            should like 0x8800ff00 
  115.          * @param width 
  116.          * @param height 
  117.          * @return 
  118.          */  
  119.         private Bitmap createBitmapFromARGB(int colorARGB, int width, int height) {  
  120.             int[] argb = new int[width * height];  
  121.   
  122.             for (int i = 0; i < argb.length; i++) {  
  123.   
  124.                 argb[i] = colorARGB;  
  125.   
  126.             }  
  127.             return Bitmap.createBitmap(argb, width, height, Config.ARGB_8888);  
  128.         }  
  129.   
  130.         /** 
  131.          *  
  132.          * @param bm 
  133.          * @param alpha 
  134.          *            ,and alpha should be like ox00000000-oxff000000 
  135.          * @note set bitmap's alpha 
  136.          * @return 
  137.          */  
  138.         /* 
  139.          * private Bitmap setBitmapAlpha(Bitmap bm, int alpha) { int[] argb = 
  140.          * new int[bm.getWidth() * bm.getHeight()]; bm.getPixels(argb, 0, 
  141.          * bm.getWidth(), 0, 0, bm.getWidth(), bm .getHeight()); 
  142.          *  
  143.          *  
  144.          * for (int i = 0; i < argb.length; i++) { 
  145.          *  
  146.          * argb[i] = ((alpha) | (argb[i] & 0x00FFFFFF)); } return 
  147.          * Bitmap.createBitmap(argb, bm.getWidth(), bm.getHeight(), 
  148.          * Config.ARGB_8888); } 
  149.          */  
  150.   
  151.         /** 
  152.          *  
  153.          * @param bm 
  154.          * @param alpha 
  155.          *            ,alpha should be between 0 and 255 
  156.          * @note set bitmap's alpha 
  157.          * @return 
  158.          */  
  159.         private Bitmap setBitmapAlpha(Bitmap bm, int alpha) {  
  160.             int[] argb = new int[bm.getWidth() * bm.getHeight()];  
  161.             bm.getPixels(argb, 0, bm.getWidth(), 00, bm.getWidth(),  
  162.                     bm.getHeight());  
  163.   
  164.             for (int i = 0; i < argb.length; i++) {  
  165.   
  166.                 argb[i] = ((alpha << 24) | (argb[i] & 0x00FFFFFF));  
  167.             }  
  168.             return Bitmap.createBitmap(argb, bm.getWidth(), bm.getHeight(),  
  169.                     Config.ARGB_8888);  
  170.         }  
  171.   
  172.         /** 
  173.          *  
  174.          * @param bm 
  175.          * @note if bitmap is smaller than screen, you can scale it fill the 
  176.          *       screen. 
  177.          * @return 
  178.          */  
  179.         private Bitmap scaleBitmapFillScreen(Bitmap bm) {  
  180.             return Bitmap.createScaledBitmap(bm, SCREEN_W, SCREEN_H, true);  
  181.         }  
  182.   
  183.         private void setBackGround(){  
  184.             int drawableId = 0;  
  185.             try {  
  186.                 drawableId = R.drawable.class.getDeclaredField(  
  187.                         "after" + imagePosition).getInt(this);  
  188.             } catch (IllegalArgumentException e) {  
  189.                 e.printStackTrace();  
  190.             } catch (SecurityException e) {  
  191.                 e.printStackTrace();  
  192.             } catch (IllegalAccessException e) {  
  193.                 e.printStackTrace();  
  194.             } catch (NoSuchFieldException e) {  
  195.                 e.printStackTrace();  
  196.             }  
  197.             setBackgroundResource(drawableId);  
  198.         }  
  199.   
  200.         /** 
  201.          *  
  202.          * @param bm 
  203.          * @note set icon1 bitmap , which overlay on background. 
  204.          */  
  205.         private void seticon1Bitmap(Bitmap bm) {  
  206.             // setting paint  
  207.             mPaint = new Paint();  
  208.             mPaint.setAlpha(0);  
  209.             mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));  
  210.             mPaint.setAntiAlias(true);  
  211.   
  212.             mPaint.setDither(true);  
  213.             mPaint.setStyle(Paint.Style.STROKE);  
  214.             mPaint.setStrokeJoin(Paint.Join.ROUND);  
  215.             mPaint.setStrokeCap(Paint.Cap.ROUND);  
  216.             mPaint.setStrokeWidth(20);  
  217.   
  218.             // set path  
  219.             mPath = new Path();  
  220.             ;  
  221.   
  222.             // converting bitmap into mutable bitmap  
  223.             mBitmap = Bitmap.createBitmap(SCREEN_W, SCREEN_H, Config.ARGB_8888);  
  224.             mCanvas = new Canvas();  
  225.             mCanvas.setBitmap(mBitmap);  
  226.             // drawXY will result on that Bitmap  
  227.             // be sure parameter is bm, not mBitmap  
  228.             mCanvas.drawBitmap(bm, 00null);  
  229.         }  
  230.   
  231.         @Override  
  232.         protected void onDraw(Canvas canvas) {  
  233.             canvas.drawBitmap(mBitmap, 00null);  
  234.             mCanvas.drawPath(mPath, mPaint);  
  235.             super.onDraw(canvas);  
  236.         }  
  237.   
  238.         private void touch_start(float x, float y) {  
  239.             mPath.reset();  
  240.             mPath.moveTo(x, y);  
  241.             mX = x;  
  242.             mY = y;  
  243.         }  
  244.   
  245.         private void touch_move(float x, float y) {  
  246.             float dx = Math.abs(x - mX);  
  247.             float dy = Math.abs(y - mY);  
  248.             if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {  
  249.                 mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);  
  250.                 mX = x;  
  251.                 mY = y;  
  252.             }  
  253.         }  
  254.   
  255.         private void touch_up() {  
  256.             mPath.lineTo(mX, mY);  
  257.             // commit the path to our offscreen  
  258.             mCanvas.drawPath(mPath, mPaint);  
  259.             // kill this so we don't double draw  
  260.             mPath.reset();  
  261.         }  
  262.   
  263.         @Override  
  264.         public boolean onTouchEvent(MotionEvent event) {  
  265.             float x = event.getX();  
  266.             float y = event.getY();  
  267.   
  268.             switch (event.getAction()) {  
  269.             case MotionEvent.ACTION_DOWN:  
  270.                 touch_start(x, y);  
  271.                 invalidate();  
  272.                 break;  
  273.             case MotionEvent.ACTION_MOVE:  
  274.                 touch_move(x, y);  
  275.                 invalidate();  
  276.                 break;  
  277.             case MotionEvent.ACTION_UP:  
  278.                 touch_up();  
  279.                 invalidate();  
  280.                 playMusic();  
  281.                 break;  
  282.             }  
  283.             return true;  
  284.         }  
  285.     }  
  286. }  
  • 大小: 378.6 KB
  • 大小: 413.2 KB
  • 大小: 248.5 KB
  • 大小: 307.5 KB
原文地址:https://www.cnblogs.com/afly/p/2360264.html