Tear clothes for Android multimedia development and learning
Today we are learning to tear clothes. Let me first talk about the principle of tearing clothes: There are two pictures of tearing clothes, one is a picture of a beautiful woman wearing a coat, and the other is a picture of a beautiful woman wearing a lingerie. When our fingers slide, we set the pictures of beautiful women in the coat to transparent color, so that the following picture is displayed.
Directly run the Code:
Public class MainActivity extends Activity {private Bitmap BMP copy; private Paint paint; private Canvas canvas; private ImageView imageView; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // 1: Because I modify the coat image, set the copy Bitmap BMP = BitmapFactory for the coat. decodeResource (getResources (), R. drawable. awaiyi); BMP copy = Bitmap. createBitmap (bmp src. getWidth (), bmp src. getHeight (), bmp src. getConfig (); paint = new Paint (); canvas = new Canvas (BMP copy); canvas. drawBitmap (bmp src, new Matrix (), paint); imageView = (ImageView) findViewById (R. id. lv2); imageView. setImageBitmap (BMP copy); // 2: listens to the imageView of the touch event. setOnTouchListener (new OnTouchListener () {@ Overridepublic boolean onTouch (View v, MotionEvent event) {// We only care about the sliding event switch (event. getAction () {case MotionEvent. ACTION_MOVE: // the trajectory of finger sliding is set to transparent int x = (int) event. getX (); int y = (int) event. getY (); cleanImage (x, y); break; default: break;} return true ;}}) ;}// because only one pixel is set at a time, public void cleanImage (int x, int y) {for (int I =-10; I <10; I ++) {for (int j =-10; j <10; j ++) {// draw if (Math. sqrt (I * I + j * j) <= 10) {// determines whether the finger has crossed the border. if the cross border is not processed, if (x + I <BMP copy. getWidth () & y + j <BMP copy. getHeight () & x + I> = 0 & y + j> = 0) {// you can specify a transparent BMP copy. setPixel (x + I, y + j, Color. TRANSPARENT); // sets the image imageView. setImageBitmap (BMP copy );}}}}}}
: