About android's renderer Shader

Because the company is more demanding on the custom drawing (various requirements = =), and finally changed it again and again. Anyway, Shader plays a big role, hereby record, on the basis of achartengine can not meet them = =

android provides the Shader class for rendering images and some geometric shapes.

Write your own class to inherit Vew

1.bitmap renderer effect


2. Linear renderer effect


3. Ring Renderer Effect


4. Hybrid renderer effects


5. Gradient renderer effect


Well, let's go down and put the code directly. The comments are very clear, if you want to adjust various effects, you can adjust X, Y and type by yourself.

public class MyShaderView  extends View {

// Shader renderer The following are inherited from Shader for the Shader class to render images and some geometric shapes.

BitmapShader bitmapShader; //bitmap renderer
ComposeShader composeShader; //Mixed renderer

LinearGradient linearGradient; //Linear rendering
RadialGradient radialGradient; //The circular rendering of the circle
SweepGradient sweepGradient; //Gradient renderer
int    width;
int    height;

public MyShaderView(Context context) {
	super(context);
	
   //Use bitmap to render 1 image first = =
   //First get the picture drawable - bitmapDrawable
    BitmapDrawable bitmap_deawable = (BitmapDrawable) getResources().getDrawable(R.drawable.yingkou);
   //get bitmap again
    Bitmap bitmap = bitmap_deawable.getBitmap();
    width = bitmap.getWidth();
    height = bitmap.getHeight();
    //********************************Start rendering bitmap image
    //  new BitmapShader(bitmap, tileX, tileY)
    // tileX The tiling mode for x to draw the bitmap in.
    // tileY The tiling mode for y to draw the bitmap in. Y direction tile mode on the bitmap
	// CLAMP : If the renderer goes beyond the original bounds, the edge tint within the bounds will be copied.
    // REPEAT : Repeating renderer images for landscape and portrait, tiled.
	// MIRROR: Horizontal and vertical repeated renderer images, this is different from the REPEAT repetition method, it is tiled in a mirrored manner.
	    bitmapShader = new BitmapShader(bitmap, TileMode.MIRROR, TileMode.MIRROR);
		    
 
    //******************************** LinearGradient Linear rendering  
    // LinearGradient(float x0, float y0, float x1, float y1, int[] colors, float[] positions, Shader.TileMode);
    // X0: the coordinate x position of the starting point of the gradient
    // y0: The y position of the starting point of the gradient
    // x1: gradient end point coordinate x position
    // y1: The y position of the end point of the gradient
    // colors: array of gradient colors
    // positions: This is also an array used to specify the relative position of the color array. If it is null, it will be evenly distributed along the slope line
    // tile: rendering mode (tiling)
    linearGradient = new LinearGradient(50, 50,1000,1000, new int[]{Color.RED, Color.GREEN, Color.BLUE, Color.WHITE},
    		                    null,Shader.TileMode.REPEAT);
 
    
   //******************************** radialGradient ring rendering
    // xy radius gradient color array position tiling method
    //multicolor
    radialGradient = new RadialGradient( 50,200,50,new int[]{Color.WHITE,Color.YELLOW,Color.GREEN,Color.RED,Color.BLUE,},  
                                 null,Shader.TileMode.REPEAT);  
    //Single color + border - this is really in response to demand. . It must be solid - in order not to reveal the things behind (the hollow set drawn by the paint itself will leak)
    //radialGradient = new RadialGradient( 50,200, 50,  new int[]
    //{Color.WHITE,Color.WHITE,Color.WHITE,Color.WHITE,Color.WHITE,Color.WHITE,Color.WHITE,
    //Color.WHITE,Color.RED},  
    //null,Shader.TileMode.REPEAT);  
		    
 
	    //******************************** composeShader mixed rendering  
    composeShader = new ComposeShader(bitmapShader,linearGradient,PorterDuff.Mode.DARKEN);  
    //The final mixed rendering mode has PorterDuff.Mode has 16 parameters to choose from, and the effect picture is below
    //分别为:CLEAR、SRC、DST、SRC_OVER、DST_OVER、SRC_IN、DST_IN、SRC_OUT、DST_OUT、
    // SRC_ATOP、DST_ATOP、    XOR、DARKEN、LIGHTEN、MULTIPLY、SCREEN。
    

    //******************************** sweepGradient gradient renderer
    sweepGradient = new SweepGradient(30,30,new int[]{Color.GREEN,Color.RED,Color.BLUE,Color.WHITE},null);  
}

    @Override
    protected void onDraw(Canvas canvas) {
    	super.onDraw(canvas);

      // Build a ShapeDrawable object and define the shape as an ellipse    
      // RectShape
      // ArcShape    
      // RectShape
      //1. Can be written like this
      //ShapeDrawable shapeDrawable = new ShapeDrawable(new OvalShape());  
      // get the brush and set the renderer  
      // shapeDrawable.getPaint().setShader(bitmapShader);  
      //set display area
      // shapeDrawable.setBounds(20, 20, width-10, height-10);  
      //Draw shapeDrawable  
      // shapeDrawable.draw(canvas);  

     // 2. You can also write like this
       Paint paint = new Paint();
       canvas.drawColor(Color.WHITE); //The background is set to gray
      
     //Draw the ellipse rendered by Bitmap
       paint.setShader(bitmapShader);
       canvas.drawOval(new RectF(90, 20, 90+ width,  20+ height), paint);
	
       //Draw a rectangle with linear gradient
       paint.setShader(linearGradient);
       canvas.drawRect(10, 200, 200, 400, paint);
      
     //Draw a circle with a circular gradient
       paint.setShader(radialGradient);
       canvas.drawCircle(50,200,50, paint);
      
     //Draw a rectangle that blends gradients (linear and circular)
       paint.setShader (composeShader);
       canvas.drawRect(0, 0, 1000, 1000, paint);
             
      //Draw a rectangle with a trapezoidal gradient
       paint.setShader (sweepGradient);
       canvas.drawRect(50, 50, 300, 300, paint);  
        
    }
}

The following picture is the hybrid renderer mentioned above, 16 styles of each type



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325921248&siteId=291194637