歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android小應用幫美女更衣系列二(附源碼)

Android小應用幫美女更衣系列二(附源碼)

日期:2017/3/1 10:48:15   编辑:Linux編程

點擊ImageSwitcher顯示的圖片即可切換到為美女換衣全屏界面,手指在界面上滑動,滑動處的衣服就被褪掉了,很黃很暴力,大家要hold住呀!!

其實啊這個實現就是兩張圖片,一張底圖,一張上面的圖,上面的圖都被抹掉了,下面的圖就出來了,主要是PorterDuff和PorterDuffXfermode的利用,APIDEMO裡面也有相關的介紹。

免費下載地址在 http://linux.linuxidc.com/

用戶名與密碼都是www.linuxidc.com

具體下載目錄在 /pub/Android源碼集錦/2011年/12月/Android小應用幫美女更衣系列一(附源碼)/

好,貼出主要代碼:

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