Android Scraping card effect implementation code _android

Source: Internet
Author: User
Tags gety transparent color

The example of this article for everyone to share the impact of the Android scraping card, for your reference, the specific content as follows

Android to achieve the bottom of a picture, the upper layer of a mask, touch slip by the finger sliding path to achieve the removal of the mask effect, similar to the lottery scraping card, not to mention the first effect of the picture:

Directly on the code:

Xfermodeview.java

/** * Created by 2016-4-21. 
 * * public class Xfermodeview extends view{private Bitmap mbgbitmap,mfgbitmap; 
 Private Paint Mpaint; 
 Private Canvas Mcanvas; 
 
 Private Path MPath; 
 Public Xfermodeview {Super (context); 
 Init (); 
 Public Xfermodeview (context, AttributeSet attrs) {Super (context, attrs); 
 Init (); 
 Public Xfermodeview (context, AttributeSet attrs, int defstyleattr) {Super (context, attrs, defstyleattr); 
 Init (); 
 } @Override protected void OnDraw (Canvas Canvas) {canvas.drawbitmap (mbgbitmap,0,0,null); 
 Canvas.drawbitmap (Mfgbitmap,0,0,null); 
 private void Init () {mpaint = new Paint (); 
 Mpaint.setalpha (0); 
 Mpaint.setxfermode (New Porterduffxfermode (PorterDuff.Mode.DST_IN)); 
 Mpaint.setstyle (Paint.Style.STROKE); 
 Mpaint.setstrokejoin (Paint.Join.ROUND); 
 Mpaint.setstrokewidth (50); 
 Mpaint.setstrokecap (Paint.Cap.ROUND); 
 MPath = new Path (); Mbgbitmap = Bitmapfactory.decoderEsource (Getresources (), R.DRAWABLE.TEST5); 
 Mfgbitmap = Bitmap.createbitmap (Mbgbitmap.getwidth (), Mbgbitmap.getheight (), Bitmap.Config.ARGB_8888); 
 Mcanvas = new Canvas (MFGBITMAP); 
 
 Mcanvas.drawcolor (Color.gray); @Override public boolean ontouchevent (Motionevent event) {switch (event.getaction ()) {case Motionevent.action 
  _down:mpath.reset (); 
  Mpath.moveto (Event.getx (), event.gety ()); 
  Break 
  Case MotionEvent.ACTION_MOVE:mPath.lineTo (Event.getx (), event.gety ()); 
 Break 
 } mcanvas.drawpath (Mpath,mpaint); 
 Invalidate (); 
 return true; 

 } 
}

Xfermode has three subclasses:
avoidxfermode Specifies a color and tolerance that forces the paint to avoid drawing on it (or drawing only on it).
Pixelxorxfermode when overwriting an existing color, apply a simple pixel xor or operation.
Porterduffxfermode This is a very powerful conversion mode that can be used to control any one of the 16 porter-duff rules of the image synthesis paint

How to interact with an existing canvas image.

From the above we can see that Porterduff.mode is an enumeration class, with a total of 16 enumerated values:
1.porterduff.mode.clear
The drawing is not submitted to the canvas.
2.porterduff.mode.src
Show Upper Drawing Pictures
3.porterduff.mode.dst
Show lower drawing picture
4.porterduff.mode.src_over
The normal drawing display, the upper and lower layers draw the overlay.
5.porterduff.mode.dst_over
The upper and lower layers are displayed. The lower ranks show.
6.porterduff.mode.src_in
Draws the intersection of two layers. Displays the upper layer.
7.porterduff.mode.dst_in
Draws the intersection of two layers. Shows the lower level.
8.porterduff.mode.src_out
Draws a non-intersection portion of the upper layer.
9.porterduff.mode.dst_out
Remove the layer to draw the non intersection part.
10.porterduff.mode.src_atop
To remove the disjoint portion of a layer from the upper part
11.porterduff.mode.dst_atop
Take the upper part of the non-intersection and the lower intersection
12.porterduff.mode.xor
XOR: Removing the two Layer intersection section
13.porterduff.mode.darken
Take two layer all area, the intersection part color deepens
14.porterduff.mode.lighten
Take two layers all, light the intersection part of the color
15.porterduff.mode.multiply
Take two layer intersection part overlay color
16.porterduff.mode.screen
Take two layer all area, the intersection part becomes transparent color

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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