Simple use of Android shader

Source: Internet
Author: User

Shader is a kind of light and shadow effect, not commonly used in general Android development, but sometimes it is a very important technical means

  Android provides shader classes specifically for rendering images and some geometries, shader below include several direct subclasses, Bitmapshader, Composeshader, LinearGradient, Radialgradient, Sweepgradient. Bitmapshader is primarily used to render images, lineargradient for gradient rendering, radialgradient for ring rendering, sweepgradient for gradient rendering, and composeshader for mixed rendering , which can be combined with several other subclasses.  
    the use of the shader class, you need to build the shader object first, then set the Render object through the Setshader method of the paint, then set the Render object, and then use the Paint object when drawing. Of course, different objects need to be built with different rendering.   The following is a simple example, in fact, it is easier to use than the method parameters are more. But it's easier to understand. We can go over the API here is not in-depth discussion, later use to say it.  

Package Com.yarin.android.examples_05_11;import Android.app.activity;import Android.os.bundle;import Android.view.keyevent;public class Activity01 extends Activity {private Gameview mgameview = null; @Overridepublic void on Create (Bundle savedinstancestate) {super.oncreate (savedinstancestate); mgameview = new Gameview (this); Setcontentview (Mgameview);} public boolean onKeyUp (int keycode, keyevent event) {Super.onkeyup (keycode, event); return true;} public boolean onKeyDown (int keycode, keyevent event) {if (Mgameview = = null) {return false;} if (keycode = = Keyevent.keycode_back) {this.finish (); return true;} Return Mgameview.onkeydown (KeyCode, event);}}

View class

package com.yarin.android.examples_05_11;import android.content.context;import  Android.graphics.bitmap;import android.graphics.bitmapshader;import android.graphics.canvas;import  android.graphics.Color;import android.graphics.ComposeShader;import  android.graphics.lineargradient;import android.graphics.paint;import android.graphics.porterduff; import android.graphics.radialgradient;import android.graphics.shader;import  android.graphics.sweepgradient;import android.graphics.drawable.bitmapdrawable;import  android.graphics.drawable.shapedrawable;import android.graphics.drawable.shapes.ovalshape;import  Android.view.keyevent;import android.view.motionevent;import android.view.view;public class  GameView extends View implements Runnable{/*  Declaration Bitmap Object  */bitmapmbitqq=  null;intBitQQwidth= 0;intBitQQheight= 0; Paint   mpaint = nUll;     /* bitmap Rendering  */Shader mBitmapShader = null;/*  Linear gradient rendering  */Shader mLinearGradient = null;/*  mixed rendering  */shader mcomposeshader  = null;   /*  wake-up gradient rendering  */shader mradialgradient = null;/*   Gradient Rendering  */shader msweepgradient = null;  shapedrawable mshapedrawableqq  = null;  public gameview (Context context) {super (Context);/*  load Resource  */ mbitqq =  ((bitmapdrawable)  getresources (). getdrawable (R.DRAWABLE.QQ)). GetBitmap ();/*  Get the width and height of the picture  */bitqqwidth = mbitqq.getwidth (); Bitqqheight = mbitqq.getheight ();/*  creating Bitmapshader objects  */mBitmapShader = new  Bitmapshader (mbitqq,shader.tilemode.repeat,shader.tilemode.mirror);/*  create lineargradient and set the gradient's color array   Explain these days. Parameters   *  First   start X coordinate  *  second   starting y-coordinate                  *  a third   End x-coordinate                  *   Fourth   end y-coordinate  *  fifth   color array  *  Sixth   This is also an array to specify the relative position of the color array   if null  is evenly distributed along the slope line  *  seventh   render mode  * */mlineargradient = new lineargradient ( 0,0,100,100, new int[]{color.red,color.green,color.blue,color.white}, null, Shader.TileMode.REPEAT);/*  is understood here as mixed rendering */mcomposeshader = new composeshader (Mbitmapshader, Mlineargradient,porterduff.mode.darken);       /*  build Radialgradient objects, Set the properties of the radius  *///here Use the Bitmapshader and lineargradient to mix//Of course can also use other combinations//mixed rendering mode many, can choose according to their own needs mradialgradient  = new radialgradient (50,200,50, new int[]{color.green,color.red,color.blue, Color.white}, null,shader.tilemode.repeat);/*  Build SweePgradient Object  */msweepgradient = new sweepgradient (30,30,new int[]{color.green, Color.red,color.blue,color.white},null); Mpaint = new paint ();/*  Open thread  */new  Thread (This). Start ();} Public void ondraw (Canvas canvas) {Super.ondraw (Canvas);//crop the picture to an oval/*  Constructs a Shapedrawable object and defines the shape as an ellipse  */mshapedrawableqq = new shapedrawable (New ovalshape ());/*   Set the ellipse to draw something for shapedrawable picture  */mshapedrawableqq.getpaint (). Setshader (Mbitmapshader);/*  Setting the display area  */mshapedrawableqq.setbounds (0,0, bitqqwidth, bitqqheight);/*  drawing shapedrawableqq */ Mshapedrawableqq.draw (canvas);  //draws a gradient rectangle mpaint.setshader (mlineargradient); Canvas.drawrect (BitQQwidth,  0, 320, 156, mpaint);         // Displays the blend render effect Mpaint.setshader (Mcomposeshader); Canvas.drawrect (0, 300, bitqqwidth, 300+bitqqheight,  mpaint);//Draw a circular gradient mpaint.setshader (Mradialgradient); canvas.drawcircle (50, 200, 50, mpaint);//Draw gradient gradient mpaint.setshader (msweepgradient); Canvas.drawrect (150, 160, 300, 300, mpaint);}   Stylus Event Public boolean ontouchevent (motionevent event) {return true;}   Press event Public boolean onkeydown (int keycode, keyevent event) {return true;}   Button Bounce Event Public boolean onkeyup (int keycode, keyevent event) {return false;} Public boolean onkeymultiple (int keycode, int repeatcount, keyevent event) { Return true;} /** *  thread Processing  */public void run () {while  (! Thread.CurrentThread (). isinterrupted ()) {try{thread.sleep (100);} catch  (interruptedexception e) {Thread.CurrentThread (). interrupt (); Use Postinvalidate to update the interface directly in the thread Postinvalidate ();}}}


For animation development, shader is sometimes very important, a special kind of progress bar animation, the text appears on the left and right sliding shader effect


Reprinted from: http://byandby.iteye.com/blog/831011

Simple use of Android shader

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.