Achieve results
Main code
<framelayout 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 " > <imageview android:id= "@+id/img_info" android:layout_width= "Wrap_content" android: layout_height= "Wrap_content" android:src= "@drawable/b"/> <imageview android:id= "@+id/img_ Surface " android:layout_width=" wrap_content " android:layout_height=" wrap_content " android:src=" @drawable/A "/> </FrameLayout>
Note: Do not set the picture position, the surface image is larger than the underlying image
Mainactivity.java
Package Com.example.guaguale;import Android.app.activity;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.graphics.canvas;import Android.graphics.color;import Android.graphics.matrix;import Android.graphics.paint;import Android.os.bundle;import android.view.MotionEvent; Import Android.view.view;import android.view.view.ontouchlistener;import android.widget.imageview;/** * Scratch Music * * @ Author Zhaoyazhi * * 2014-6-18 */public class Mainactivity extends Activity implements Ontouchlistener {private C Anvas canvas;private Paint paint;private Bitmap bitmap;private ImageView imginfo;private ImageView imgsurface;private Bitmap bsurface;//No scratch on the surface of the image private Bitmap binfo;//after scraping the display content @overrideprotected void OnCreate (Bundle savedinstancestate {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); imgsurface = (ImageView) findViewById (r.id.img_surface); imginfo = (ImageView) Findviewbyid (r.id.img_info);//Get Picture//Parameter 1:res is a reference to the resource, parameter 2:id is the Idbsur of the pictureface = Bitmapfactory.decoderesource (Getresources (), r.drawable.a); binfo = Bitmapfactory.decoderesource (getResources (), r.drawable.b);//Set Picture Imgsurface.setimagebitmap (Bsurface); Imginfo.setimagebitmap (binfo);// Create a blank bitmapbitmap that can be modified = Bitmap.createbitmap (Bsurface.getwidth (), Bsurface.getheight (), Bsurface.getconfig ());// Set screen Touch event Imgsurface.setontouchlistener (this);//Create brush paint = new paint ();//create canvas canvases = new canvas (bitmap); Canvas.drawbitmap (Bsurface, New Matrix (), paint);} /** * User Touch Screen Event */@Overridepublic Boolean OnTouch (view view, Motionevent event) {switch (event.getaction ()) {case Motion Event.ACTION_MOVE:int newx = (int) event.getx (); int newy = (int) event.gety ();//I, j size of each transparent unit for (int i = -10; I < 10 ; i++) {for (int j = -10; J <; J + +) {//area outside the picture if ((i + newx) >= bsurface.getwidth () | | j + newy >= bsurface.gethe ight () | | i + newx < 0| | J + Newy < 0) {return false;} Turn the sliding place into transparent bitmap.setpixel (i + newx, j + newy, color.transparent);}} Setting transparent areas Imgsurface.setimagEbitmap (bitmap); break;} return true;}}
Involving knowledge points
getresources ()
http://blog.csdn.net/zhaoyazhi2129/article/details/32077513
Setimagebitmap
http://blog.csdn.net/zhaoyazhi2129/article/details/32080827
Paint brushes
http://blog.csdn.net/zhaoyazhi2129/article/details/32090187
Canvas Canvases
http://blog.csdn.net/zhaoyazhi2129/article/details/32098477
Bitmap
http://blog.csdn.net/zhaoyazhi2129/article/details/32136179