Zhao Yazhi _android Case _ scraping Music

Source: Internet
Author: User

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

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.