Android Basics Getting Started tutorial--8.3.7 Paint API--xfermode and Porterduff detailed (iv)
tags (space delimited): Android Basics Getting Started Tutorial
Introduction to this section:
We wrote the first example of the use of Xfermode and Porterduff in the previous section: Fillet & round picture ImageView implementation,
We appreciate the benefits that PorterDuff.Mode.DST_IN brings us, and we continue to write examples to practice practiced hand in this section,
Remember Android Basics Getting Started Tutorial--8.3.2 Drawing class example to bring everyone to pull out the implementation of beautiful clothes?
At the time, our implementation was to set the 20*20 pixels near the touch area of the finger to be transparent, such as:
Do not know if you have found a problem, we wipe beautiful clothes, when the wipe is a block, but we draw the drawing board
When drawing, the dash is very smooth, there is no way to combine the two, we clean clothes is also smooth?
The answer must be some, that is, using Xfermode! In this section we use another mode,dst_out mode!
Draw a target graph where it doesn't intersect
If you have forgotten a pattern or have not seen any of the 18 modes, then please go:
Android Basics Getting Started tutorial--8.3.5 Paint API--xfermode and Porterduff details (ii)
In addition, the Porterduff.mode should be affixed:
Well, don't talk much, start this section ~
1. To achieve and implement the process analysis:
to achieve :
Well, I don't know how many times you've seen that gif? Do not know whether the picture is suitable for everyone's taste, piglet
Is from other people's app to take down, do not ask me the designation or leave the mailbox and so on, I do not know anything ~ to find a pan or something,
Ask the old driver in the group-Kishing bar, OK, let's analyze the implementation process.
- We say that the principle, in fact, is two bitmap, one after the front is wearing clothes, the back is not wearing clothes,
Then, using a path to record the user-drawn graphics, and then set the dst_out pattern for our brushes, then
The DST (target graph) that overlaps with path, is the picture that wears clothes, becomes transparent! Good, very simple!
We'll refine it again!
- First we need two bitmap, to store two pictures before and after, here we let two bitmap are full screen!
- Then set the brush, rounded corners, pen width, anti-aliasing and so on!
- Then define a drawing path, that is, the user to draw the area of the method, set xfermode after the painting area!
- Then rewrite the Ontouchevent method, which is the same as the previous custom paint board!
- Finally rewrite the OnDraw () method, first draw the background image, call the user to draw the area of the method, and then draw the foreground picture!
May seem a little complicated, but the code is very simple to say ~
2. Code implementation:
Directly on a custom view--Stripmeizi.java
/** * Created by Jay on 2015/10/25 0025. * * Public class stripmeizi extends View{ PrivatePaint Mpaint =NewPaint ();PrivatePath MPath =NewPath ();PrivateCanvas Mcanvas;PrivateBitmap Mbeforebitmap;PrivateBitmap Mbackbitmap;Private intMlastx,mlasty;Private intScreenw, Screenh;//Screen width high PrivateXfermode Mxfermode =NewPorterduffxfermode (PorterDuff.Mode.DST_OUT); Public Stripmeizi(Context context) { This(Context,NULL); } Public Stripmeizi(context context, AttributeSet attrs) {Super(context, attrs); Screenw = SCREENUTIL.GETSCREENW (context); Screenh = screenutil.getscreenh (context); Init (); } Public Stripmeizi(context context, AttributeSet attrs,intDEFSTYLEATTR) {Super(Context, attrs, defstyleattr); }Private void Init() {//Picture behind, here let it fullscreenMbackbitmap = Bitmapfactory.decoderesource (Getresources (), r.mipmap.meizi_back); Mbackbitmap = Bitmap.createscaledbitmap (Mbackbitmap, Screenw, Screenh,false);//Front of the picture and draw it onto the canvasMbeforebitmap = Bitmap.createbitmap (Screenw, Screenh, Bitmap.Config.ARGB_8888); Mcanvas =NewCanvas (MBEFOREBITMAP); Mcanvas.drawbitmap (Bitmapfactory.decoderesource (Getresources (), R.mipmap.meizi_before),NULL,NewRECTF (0,0, Screenw, Screenh),NULL);//Brush-related SettingsMpaint.setantialias (true); Mpaint.setdither (true); Mpaint.setstyle (Paint.Style.STROKE); Mpaint.setstrokejoin (Paint.Join.ROUND);//FilletMpaint.setstrokecap (Paint.Cap.ROUND);//FilletMpaint.setstrokewidth ( the);//Set Brush width}Private void DrawPath() {Mpaint.setxfermode (Mxfermode); Mcanvas.drawpath (MPath, mpaint); }@Override protected void OnDraw(Canvas canvas) {Canvas.drawbitmap (Mbackbitmap,0,0,NULL); DrawPath (); Canvas.drawbitmap (Mbeforebitmap,0,0,NULL); }@Override Public Boolean ontouchevent(Motionevent event) {intAction = Event.getaction ();intx = (int) Event.getx ();inty = (int) event.gety ();Switch(action) { CaseMotionEvent.ACTION_DOWN:mLastX = x; Mlasty = y; Mpath.moveto (MLASTX, mlasty); Break; CaseMotionevent.action_move:intDX = Math.Abs (X-MLASTX);intDY = Math.Abs (y-mlasty);if(DX >3|| Dy >3) Mpath.lineto (x, y); MLASTX = x; Mlasty = y; Break; } invalidate ();return true; }}
Layout code activity_main.xml:
<RelativeLayout 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" tools:context=".MainActivity"> <com.jay.xfermodedemo2.StripMeiZi android:layout_width="match_parent" android:layout_height="match_parent"/></RelativeLayout>
3. Code Sample Download:
Xfermodedemo2.zip
This section summarizes:
Well, in this section we wrote another practical example of Xfermode and Porterduff--the demo of the hand-torn beauty dress, compared to our previous
That kind of tear beautiful clothes (let touch point near 20*20 pixel point to become transparent) way more gentle ~ code is also a lot of simple, right, there is no
Experience the benefits of hybrid Xfermode for Android images, or the importance of custom controls! Well, what are you waiting for?
Open your IDE, and repeat the code, and taste the thrill of the hand-torn beauty dress ~
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android Basics Getting Started tutorial--8.3.7 Paint API--xfermode and Porterduff detailed (iv)