A person's life is like an article, only after several careful modification, can continue to improve.
the content of this lecture: Colorfilter Color Filter Class ( Follow up on a further in-depth understanding Custom view)
On the previous lecture we explained the Colormatrixcolorfilter class
First, Light ingcolorfilter Light Color Filter Class ( This class has only one constructor method:)
Lightingcolorfilter (int mul, int add)
mul full name is colormultiply meaning for color multiplication, add full name is Coloradd meaning to add color, both values are 16 binary color value 0xAARRGGBB
Example one: The picture is removed from the green (will find the original part of the green is red, which involves the knowledge of color )
Here are the custom Customtitleview.java files:
public class CustomView extends View {private Paint mpaint;//brush//Contextual reference is a messenger filled with information that Android needs to get the information it needs from the inside. Private Context mcontext;private Bitmap bitmap;//bitmap private int x,y;//bitmap drawing when the upper-left corner of the start coordinates public customview (context context) { Super (context, NULL);} Public CustomView (context context, AttributeSet Attrs) {Super (context, attrs); this.mcontext = Context;initpaint ();// Initialize the brush initres (context);//Initialize Resource}//initialize resource private void Initres (context context) {//Get bitmap bitmap= Bitmapfactory.decoderesource (Context.getresources (), R.DRAWABLE.P); /* * Calculates the coordinates of the upper-left corner of the bitmap drawing when it is in the center of the screen * screen coordinates x-axis shift to the left offset half of the width * screen coordinates y-axis upward shift figure half the height */x=measureutil.g Etscreensize (activity) mcontext) [0]/2-bitmap.getwidth ()/2;y = Measureutil.getscreensize ((activity) mcontext) [1]/2 -Bitmap.getheight ()/2; }//initialization Brush private void Initpaint () {//Instantiate brush and open anti-aliasing mpaint = new Paint (paint.anti_alias_flag);// Set Color filter mpaint.setcolorfilter (new Lightingcolorfilter (0XFFFF00FF, 0x00000000));} protected void OnDraw (Canvas Canvas) {Super.ondraw (canvas);//Draw a bitmap from a resource to get a bitmap drawn on the canvas canvas.drawbitmap (bitmap, X, Y,mpaint);}} Lightingcolorfilter (0xFFFFFFFF, 0x00000000) when the original image will not have any change, if we want to increase the value of red, then Lightingcolorfilter (0xFFFFFFFF, 0x00xx0000), where xx value is 00 to ff.
Example two: A gray rectangle that we clicked on to turn it into yellow
Here are the custom Customtitleview.java files:
public class CustomView extends View {private Paint mpaint;//brush Private Context mcontext;//Contextual environment reference private Bitmap Bitmap; Bitmap private int x,y;//bitmap is drawn with the start coordinate of the upper-left corner private Boolean isclick;//is used to identify whether the control was clicked over public CustomView (context context) {Super ( context, NULL);} Public CustomView (context context, AttributeSet Attrs) {Super (context, attrs); this.mcontext = Context;initpaint ();// Initializes the brush initres (context);//initializes the resource Setonclicklistener (new Onclicklistener () {public void OnClick (View arg0) {//Determines whether the control has been clicked if (Isclick) {//When we don't want the effect of a color filter, setcolorfilter (null) and redraw the view! If you have been clicked then set the color filter to NULL to restore the natural mpaint.setcolorfilter (null); isclick=false;} else{//if not clicked then set color filter to yellow mpaint.setcolorfilter (new Lightingcolorfilter (0xFFFFFFFF, 0x00ffff00)) Isclick=true;} Remember to redraw invalidate (); }});} Initialize resource private void Initres (context context) {//Get bitmap Bitmap=bitmapfactory.decoderesource (Context.getresources (), R.DRAWABLE.G); /* * Calculates the coordinates of the upper-left corner of the bitmap drawing when it is in the center of the screen * screen coordinates x-axis shift to the left offset half of the width of the screen coordinate y-axis upward shift figure halfHeight */x=measureutil.getscreensize (Activity) mcontext) [0]/2-bitmap.getwidth ()/2;y = Measureutil.getscreensize (Ac tivity) mcontext) [1]/2-bitmap.getheight ()/2; }//initialization Brush private void Initpaint () {//Instantiate brush and open anti-aliasing mpaint = new Paint (Paint.anti_alias_flag);} protected void OnDraw (canvas canvas) {super.ondraw (canvas);//Draw a bitmap from a resource to get a bitmap drawn on the canvas canvas.drawbitmap (bitmap, x, Y, Mpaint);}}
TwoPorterduffcolorfilter Class ( only one construction method:)
Porterduffcolorfilter (int color, porterduff.mode Mode)
one is the color value of the 16 binary representation, and the other is a constant value in the Porterduff inner class mode, which represents the blending mode ( mixed mix must be two things mixed ). The first is the color value we set and the second is the element on our canvas!
Example three: set the value of color to blue, and the mode to PorterDuff.Mode.DARKEN dimmed:
Darken also become Blue (similar to two pictures in PS, the bottom is a blue picture, above is our picture) ... This is the effect of the PorterDuff.Mode.DARKEN mode (there are other modes) Patterns in Porterduff.mode not only apply to image color blending, but also to graphics blending, such as PorterDuff.Mode.DST_OUT, which means cropping a mixed graph
Here are the custom Customtitleview.java files:
public class CustomView extends View {private Paint mpaint;//brush Private Context mcontext;//Contextual environment reference private Bitmap Bitmap; Bitmap private int x,y;//bitmap is drawn when the upper-left corner of the start coordinates public customview (context context) {Super (context, NULL);} Public CustomView (context context, AttributeSet Attrs) {Super (context, attrs); this.mcontext = Context;initpaint ();// Initialize the brush initres (context);//Initialize Resource}//initialize resource private void Initres (context context) {//Get bitmap bitmap= Bitmapfactory.decoderesource (Context.getresources (), R.DRAWABLE.P); /* * Calculates the coordinates of the upper-left corner of the bitmap drawing when it is in the center of the screen * screen coordinates x-axis shift to the left offset half of the width * screen coordinates y-axis upward shift figure half the height */x=measureutil.g Etscreensize (activity) mcontext) [0]/2-bitmap.getwidth ()/2;y = Measureutil.getscreensize ((activity) mcontext) [1]/2 -Bitmap.getheight ()/2; }//initialization Brush private void Initpaint () {//Instantiate brush and open anti-aliasing mpaint = new Paint (paint.anti_alias_flag);//Set Color filter Mpaint.setcolorfilter (New Porterduffcolorfilter (Color.Blue, PorterDuff.Mode.DARKEN));} protected void OnDraw (canvas canvas) {Super.ondraw (canvas);//Draw a bitmap from a resource to get a bitmap drawn on the canvas canvas.drawbitmap (bitmap, X, Y,mpaint);}}
Take your time and enjoy it passed by, learned to leave a message, the top of the chant ~ ~
Android Custom View (iii)