How to achieve scraping effect in Android

Source: Internet
Author: User
Tags abs final gety transparent color reset

A few months ago, when I was in touch with Android, I did a small project that used a scratch-like effect, and now I'm going to post the code

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

/** * * * @desc eraser function, similar to scraping effect * @author HTP * @Date 20140311 * @version 1.00  
    * * public class Text_rubbler extends TextView {private float touch_tolerance;//fill distance, make the line more natural, soft, 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); Public Text_rubbler (context context, AttributeSet attrs, int defstyle) {Super (Contex  
        T, Attrs, Defstyle); bgcolor =//Attrs.getattributeintvalue ("",///"TextColor  
        ", 0xFFFFFF);  
    System.out.println ("Color:" +bgcolor);  
}    Public Text_rubbler (context, AttributeSet attrs) {Super (context, attrs); bgcolor =//Attrs.getattributeintvalue ("",///"TextColor  
        ", 0xFFFFFF);  
        System.out.println (bgcolor);   
    System.out.println (Attrs.getattributevalue ("",//"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 paints 
     Trokewidth * Contact (RUBBER) Width * @param touchtolerance * padding distance, the smaller the value, the softer. */public void Beginrubbler (final int bgcolor, FInal int paintstrokewidth, float touchtolerance) {touch_tolerance = touchtolerance;  
        Set Brush Mpaint = new Paint ();  
        Mpaint.setalpha (0); Brush across the traces will become transparent color mpaint.setcolor (;  
        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) {////} MBITMA p = Bitmap.createbitmap (GetlaYoutparams (). Width, getlayoutparams (). height, config.argb_8888);  
        Mcanvas = new Canvas (MBITMAP);  
        Mcanvas.drawcolor (bgcolor);  
    Isdraw = true; @Override public boolean ontouchevent (Motionevent event) {if (!isdraw) {retur  
        n true; Switch (event.getaction ()) {case Motionevent.action_down://Contact Press//touchdown (event.  
            GETRAWX (), Event.getrawy ());  
            Touchdown (Event.getx (), event.gety ());  
            Invalidate ();  
            Case Motionevent.action_move://Contact Mobile Touchmove (Event.getx (), event.gety ());  
            Invalidate ();  
            Case MOTIONEVENT.ACTION_UP://Contact Bounce TouchUp (Event.getx (), event.gety ());  
            Invalidate ();  
    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 (); }  

Related Article

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: 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.