Android Rip clothes (scratch games)

Source: Internet
Author: User
Tags transparent color

Project Introduction:

This item is for tearing clothes, similar to scraping music games

Detailed Description:

After the user launches the project, loading a picture, when the user clicks on the picture, the area of the click disappears, thus showing the picture below this picture
This small game is similar to the Scratch award, the scratch coating will display text.
Here is to rip off the clothes of beauty, leaking out the picture inside.

The knowledge involved in this application is:
    • 1. How to implement the drawing function
    • 2. How to turn the pixel point into a transparent color
    • 3. How to monitor the phone's operation on the screen
      There are three kinds of situations, such as striking, sliding and leaving the main
Note:
    • 1. Be aware that when you set a pixel, the range cannot exceed the range of the current control
    • 2. Set ImageView best set to Wrap_content, if set to Match_parent, may cause the picture to remain white, affect the positioning
      For example, if the image wide distance ImageView component is 20DP (that is, the picture two times the left White is 20DP), when the finger moves to the (36,0) position, according to the problem code, you can calculate:
      Instead of (36,0), the pixel color of a circular area with a radius of 8 is changed to a transparent color (16,0).
steps:1. Create an Android project and write the code for the Activity_main.xml file:
<relativelayout xmlns:android="Http://schemas.android.com/apk/res/android"xmlns:tools="Http://schemas.android.com/tools"Android:layout_width="Match_parent"android:layout_height="Match_parent"Android:paddingbottom="@dimen/activity_vertical_margin"android:paddingleft="@dimen/activity_horizontal_margin"android:paddingright="@dimen/activity_horizontal_margin"android:paddingtop="@dimen/activity_vertical_margin"tools:context="Hhh.exercise.smultimedia_d.MainActivity"> <imageview android:layout_width="Wrap_content"android:layout_height="Wrap_content"Android:src="@drawable/aneiyi"/> <imageview android:id="@+id/iv"Android:layout_width="Wrap_content"android:layout_height="Wrap_content"/></relativelayout>
This is essentially creating two ImageView controls, and two controls overlap, with the interface as follows:

2. Write the Mainactivity code:
Importandroid.app.Activity;ImportAndroid.graphics.Bitmap;ImportAndroid.graphics.BitmapFactory;ImportAndroid.graphics.Canvas;ImportAndroid.graphics.Color;ImportAndroid.graphics.Matrix;ImportAndroid.graphics.Paint;ImportAndroid.os.Bundle;ImportAndroid.view.MotionEvent;ImportAndroid.view.View;ImportAndroid.view.View.OnTouchListener;ImportAndroid.widget.ImageView; Public  class mainactivity extends Activity {    PrivateImageView IV;PrivateBitmap bmcopy;@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_main); IV = (ImageView) Findviewbyid (R.ID.IV);//Create a read-only bitmap objectBitmap bmsrc = Bitmapfactory.decoderesource (Getresources (), R.drawable.awaiyi); Bmcopy = Bitmap.createbitmap (Bmsrc.getwidth (), Bmsrc.getheight (), Bmsrc.getconfig ());//Create Paint ObjectPaint paint =NewPaint ();//Create a Canvas objectCanvas Canvas =NewCanvas (bmcopy);//start paintingCanvas.drawbitmap (BMSRC,NewMatrix (), paint);//Load the outermost pictureIv.setimagebitmap (bmcopy);//Set Touch listeningIv.setontouchlistener (NewOntouchlistener () {@Override             Public Boolean OnTouch(View V, motionevent event) {Switch(Event.getaction ()) { CaseMotionevent.action_move://Get the coordinates of your current finger                    intx = (int) Event.getx ();inty = (int) event.gety ();//Take an area of the current coordinate (here is the point of the money coordinates as the origin, radius of 5 to make a circular area)                    intR =8; for(inti =-r; I <= R; i++) { for(intj =-R; J <= R; J + +) {if(Math.sqrt (i*i+ j*j) <= R) {//Determine if the point of the current region is within the scope of the ImageView control, do the following, and do nothing                                if(x + i < Bmcopy.getwidth () && x + i >0&& y + J < bmcopy.getheight () && y + j >0) {//Set the pixel of the coordinate point that the user has crossed to a transparent colorBmcopy.setpixel (x + i, y + j, color.transparent);//Display the picture on the interfaceIv.setimagebitmap (bmcopy); }                            }                        }                    }                }return true;    }        }); }}

Finally, after deployment, just a few clicks from the screen, the effect is this:

Android Rip clothes (scratch games)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.