Android implementation scraping and scraping music sample analysis _android

Source: Internet
Author: User
Tags abs gety transparent color

Micro-letter public number has a lot to do scraping music activities, this article has achieved the effect of scraping the music, the specific code is as follows:

The first thing to do is something like eraser, then you can erase the handwriting on the paper.

/** * FileName:SplashActivity.java * * @desc eraser function, similar to scraping effect * @author HTP * @Date 20140311 * @ver Sion 1.00/public class Text_rubbler extends TextView {private float touch_tolerance;//fill distance, make line more natural and softer 
   
    , the smaller the value, the softer. 
    private final int bgcolor; 
    Bitmap private Bitmap Mbitmap; 
    Canvas private Canvas Mcanvas; 
    Brush private Paint Mpaint; 
    Private Path MPath; 
   
    Private float MX, my; 
   
    Private Boolean isdraw = false; 
   
    Public Text_rubbler {/** * @param contexts/super (context); Text_rubbler (context, AttributeSet attrs, int defstyle) {Super (context, Attrs, Defstyle 
      ); bgcolor =//Attrs.getattributeintvalue ("http://schemas.android.com/apk/res/android",//"TextColor", 0xF 
      FFFFF); 
    System.out.println ("Color:" +bgcolor); Public Text_rubbler, AttRibuteset attrs) {Super (context, attrs); bgcolor =//Attrs.getattributeintvalue ("http://schemas.android.com/apk/res/android",//"TextColor", 0xF 
      FFFFF); 
      System.out.println (bgcolor); 
    System.out.println (Attrs.getattributevalue ("http://schemas.android.com/apk/res/android",//"layout_width")); 
      } @Override protected void OnDraw (Canvas Canvas) {Super.ondraw (Canvas); 
        if (Isdraw) {Mcanvas.drawpath (MPath, mpaint); 
        Mcanvas.drawpoint (MX, My, mpaint); 
      Canvas.drawbitmap (mbitmap, 0, 0, NULL); 
     /** * Open Erase function * * @param bgcolor * cover background color * @param paintstrokewidth 
     * Contact (RUBBER) Width * @param touchtolerance * padding distance, the smaller the value, the softer. */public void Beginrubbler (final int bgcolor, final int paintstrokewidth, float touchtolerance) {to 
      Uch_tolerance = touchtolerance; 
   Set Brush   Mpaint = new Paint (); 
      Mpaint.setalpha (0); Brush across the traces will become transparent color mpaint.setcolor (color.black); 
      This cannot be a transparent color mpaint.setxfermode (new Porterduffxfermode (PorterDuff.Mode.DST_OUT)); 
      or//Mpaint.setalpha (0); 
   
      Mpaint.setxfermode (New Porterduffxfermode (PorterDuff.Mode.DST_IN)); 
      Mpaint.setantialias (TRUE); 
      Mpaint.setdither (TRUE); 
      Mpaint.setstyle (Paint.Style.STROKE); Mpaint.setstrokejoin (Paint.Join.ROUND); Front rounded corner mpaint.setstrokecap (Paint.Cap.ROUND); Posterior rounded corner mpaint.setstrokewidth (paintstrokewidth); 
      Pen wide//Mark MPath = new Path (); 
      ; Overlay//if (Getlayoutparams (). width = layoutparams.fill_parent) {////} Mbitmap = Bitmap.c 
      Reatebitmap (Getlayoutparams (). Width, getlayoutparams (). height, config.argb_8888); 
   
      Mcanvas = new Canvas (MBITMAP); 
      Mcanvas.drawcolor (bgcolor); 
    Isdraw = true; 
} @Override    public boolean ontouchevent (Motionevent event) {if (!isdraw) {return true; Switch (event.getaction ()) {case Motionevent.action_down://Contact Press//Touchdown (EVENT.GETRAWX (), E 
        Vent.getrawy ()); 
        Touchdown (Event.getx (), event.gety ()); 
        Invalidate (); 
      Break 
        Case Motionevent.action_move://Contact Mobile Touchmove (Event.getx (), event.gety ()); 
        Invalidate (); 
      Break 
        Case MOTIONEVENT.ACTION_UP://Contact Bounce TouchUp (Event.getx (), event.gety ()); 
        Invalidate (); 
      Break 
      Default:break; 
    return true; 
      private void touchdown (float x, float y) {mpath.reset (); 
      Mpath.moveto (x, y); 
      MX = x; 
    my = y; 
      } private void Touchmove (float x, float y) {float dx = math.abs (X-MX); 
      float dy = math.abs (y-my); if (dx >= touch_tolerance | | dy >= touch_tolerance) {MpatH.quadto (MX, my, (x + MX)/2, (y + Me)/2); 
        MX = x; 
      my = y; 
      } private void TouchUp (float x, float y) {mpath.lineto (x, y); 
      Mcanvas.drawpath (MPath, mpaint); 
    Mpath.reset ();  } 
   
  }

The next step is to use the eraser erase
 

  /** * FileName:RubblerAct.java * @Desc This class will display a scraping area on the activity by calling Text_rubbler, which can start the touch event * @author HTP 
    * @Date 20140312 * @version 1.00/public class Rubbleract extends activity {//scratch after text display 
    Private TextView Tv_rubbler; 
    Get a scrape of the content private sentence msentence; 
   
    The next one private TextView tv_next; 
   
      @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
   
      Setcontentview (This, "Thank you for your patronage", new Rect (rubble,//300,250), 2,1f,14)); 
      Setcontentview (R.layout.rubbler); 
      The color you set must be transparent. ((Text_rubbler) Findviewbyid (R.id.rubbler)). Beginrubbler (0xFFFFFFFF, 1f);//Set the width of the eraser, etc. msentence = NE 
      W sentence (); 
      Random initialization of text Tv_rubbler = (TextView) Findviewbyid (R.id.rubbler); 
      String str = msentence.getsentence (); Tv_rubbler.settext (StR); 
   
      Tv_next = (TextView) Findviewbyid (R.id.tv_next); Click Next Tv_next.setonclicklistener (New Onclicklistener () {@Override public void OnClick (Vie 
          W v) {//TODO auto-generated method stub String str = Msentence.getsentence (); 
          Tv_rubbler.settext (str); 
   
        ((Text_rubbler) Findviewbyid (R.id.rubbler))/initialization state. Beginrubbler (0xFFFFFFFF, 1f); 
   
    } 
      }); 
      Class Rubble extends View {private final int paint_stroke_width; Private final float touch_tolerance; 
      Fill the distance, make the line more natural, soft, the smaller the value, the softer. 
   
      private final int text_size; 
      Private Bitmap Mbitmap; 
      Canvas private Canvas Mcanvas; 
      Brush private Paint Mpaint; 
      Private Path MPath; 
      Private float MX, my; 
   
      Private final int X, Y, W, H; 
   
      Private final Rect Touchrect; Public rubble (context context, String Bgtext, Rect Rect, 
          int paintstrokewidth, float touchtolerance, int textsize) {super (context); 
        Setfocusable (TRUE); 
        Touchrect = rect; 
        W = Rect.right-rect.left; 
        H = Rect.bottom-rect.top; 
        X = Rect.left; 
        Y = Rect.top; 
        Text_size = Textsize; 
        Paint_stroke_width = Paintstrokewidth; 
        Touch_tolerance = touchtolerance; 
        SetBackground (Touchrect, Bgtext); 
   
      Initdrowtools (); 
        } private void SetBackground (Rect Rect, String bgtext) {displaymetrics dm = new Displaymetrics (); 
   
        DM = This.getresources (). Getdisplaymetrics (); 
        Bitmap Bitmap = Bitmap.createbitmap (Dm.widthpixels, Dm.heightpixels, config.argb_8888); 
   
        Canvas Canvas = new Canvas (bitmap); 
        Paint Paint = new Paint (); 
        Paint.setcolor (0x88000000); 
        Paint.setstyle (Style.stroke); 
        Paint.settextalign (Align.center); Paint.settextsize(text_size); 
        Paint.settextscalex (1.5f); 
        Canvas.drawcolor (Color.White); 
            Reprint coordinates are not good control int x = Rect.left + (rect.right-rect.left-bgtext.length () * text_size) 
        /2; 
        int y = rect.top + (rect.bottom-rect.top-text_size)/2; 
        int y = 218+25; 
        Canvas.drawtext (Bgtext, x, y, paint); 
        drawable drawable = new bitmapdrawable (bitmap); 
      Setbackgrounddrawable (drawable); 
        private void Initdrowtools () {//set brush Mpaint = new Paint (); 
        Mpaint.setalpha (0); Brush across the traces will become transparent color mpaint.setcolor (color.black); 
        This cannot be a transparent color mpaint.setxfermode (new Porterduffxfermode (PorterDuff.Mode.DST_OUT)); 
        or//Mpaint.setalpha (0); 
   
        Mpaint.setxfermode (New//Porterduffxfermode (PorterDuff.Mode.DST_IN)); 
        Mpaint.setantialias (TRUE); 
        Mpaint.setdither (TRUE); Mpaint.setstyle(Paint.Style.STROKE); Mpaint.setstrokejoin (Paint.Join.ROUND); Front rounded corner mpaint.setstrokecap (Paint.Cap.ROUND); Posterior rounded corner mpaint.setstrokewidth (paint_stroke_width); 
        Pen wide//Mark MPath = new Path (); 
   
        ; 
        Cover Mbitmap = Bitmap.createbitmap (W, H, config.argb_8888); 
        Mcanvas = new Canvas (MBITMAP); 
   
      Mcanvas.drawcolor (0x88000000); 
        } @Override protected void OnDraw (Canvas Canvas) {Super.ondraw (Canvas); 
        Mcanvas.drawpath (MPath, mpaint); 
        Mcanvas.drawpoint (MX, My, mpaint); 
      Canvas.drawbitmap (Mbitmap, X, Y, NULL); @Override public boolean ontouchevent (Motionevent event) {System.out.print ("x--" + event.get 
        X ()); 
        System.out.println ("y--" + event.gety ()); 
        if (!touchrect.contains (int) event.getx (), (int) event.gety ()) {return false; Switch (event.getaction ()) { 
        Contact Press Case Motionevent.action_down: {touchdown (EVENT.GETRAWX (), Event.getrawy ()); 
          Touchdown (Event.getx ()-Touchrect.left, Event.gety ()-touchrect.top); 
          Invalidate (); 
        Break Case Motionevent.action_move://Contact Mobile Touchmove (Event.getx ()-Touchrect.left, Event.get 
   
          Y ()-touchrect.top); 
          Invalidate (); 
        Break Case MOTIONEVENT.ACTION_UP://Contact Bounce TouchUp (Event.getx ()-Touchrect.left, Event.gety ()-TOUCHR 
          Ect.top); 
          Invalidate (); 
        Break 
        Default:break; 
      return true; 
        private void touchdown (float x, float y) {mpath.reset (); 
        Mpath.moveto (x, y); 
        MX = x; 
      my = y; 
        } private void Touchmove (float x, float y) {float dx = math.abs (X-MX); float dy = Math.ABS (Y-MY); 
          if (dx >= touch_tolerance | | dy >= touch_tolerance) {mpath.quadto (MX, my, (x + MX)/2, (y + Me)/2); 
          MX = x; 
        my = y; 
        } private void TouchUp (float x, float y) {mpath.lineto (x, y); 
        Mcanvas.drawpath (MPath, mpaint); 
      Mpath.reset (); 
    }/** * Keyboard event, when the back button is pressed to ask if you want to press the exit program again///exit time private long exittime = 0; 
          @Override public boolean onKeyDown (int keycode, keyevent event) {if (keycode = = Keyevent.keycode_back && event.getaction () = = Keyevent.action_down) {if (System.currenttimemillis ()-exittime) > 200 
          0) {Toast.maketext (Getapplicationcontext (), "Press the exit procedure again", Toast.length_short). Show (); 
        Exittime = System.currenttimemillis (); 
          else {finish (); 
   
        System.exit (0); 
      return true; 
  }    Return Super.onkeydown (KeyCode, event); 
 } 
   
  }

The implementation effect is as follows:

Thank you for reading, I hope to help you, thank you for your support for this site!

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.