android - How to use BitmapShader

activity_main.xml
 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout     xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:id="@+id/rl"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:padding="16dp"     tools:context=".MainActivity"     android:background="#ee8af5"     >     <TextView         android:id="@+id/tv"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="BitmapShader"         android:textStyle="bold"         android:textSize="90dp"         android:fontFamily="sans-serif-condensed"         android:textColor="#000"         />     <Button         android:id="@+id/btn"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="Apply BitmapShader"         android:layout_alignParentBottom="true"         android:layout_alignParentRight="true"         /> </RelativeLayout> 
MainActivity.java
 package com.cfsuman.me.androidcode;  import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.BitmapShader; import android.graphics.Shader; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.view.Window; import android.widget.Button; import android.widget.RelativeLayout; import android.widget.TextView;   public class MainActivity extends AppCompatActivity {     private Context mContext;     private RelativeLayout mRL;     private TextView mTV;     private Button mBTN;      @Override     protected void onCreate(Bundle savedInstanceState) {         requestWindowFeature(Window.FEATURE_ACTION_BAR);         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          // Get the application context         mContext = getApplicationContext();          // Get the widgets reference from XML layout         mRL = (RelativeLayout) findViewById(R.id.rl);         mTV = (TextView) findViewById(R.id.tv);         mBTN = (Button) findViewById(R.id.btn);          // Set a click listener for Button widget         mBTN.setOnClickListener(new View.OnClickListener() {             @Override             public void onClick(View view) {                 /*                     BitmapFactory                         Creates Bitmap objects from various sources, including files, streams,                         and byte-arrays.                      public static Bitmap decodeResource (Resources res, int id)                         Synonym for decodeResource(Resources, int, android.graphics.BitmapFactory.Options)                         with null Options.                          Returns                             The decoded bitmap, or null if the image could not be decoded.                 */                 Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.pattern);                  /*                     Shader                         Known Direct Subclasses                         BitmapShader, ComposeShader, LinearGradient, RadialGradient, SweepGradient                          Shader is the based class for objects that return horizontal spans of colors                         during drawing. A subclass of Shader is installed in a Paint calling                         paint.setShader(shader). After that any object (other than a bitmap) that is                         drawn with that paint will get its color(s) from the shader.                 */                 /*                     BitmapShader                         Shader used to draw a bitmap as a texture. The bitmap can be repeated or                         mirrored by setting the tiling mode.                      public BitmapShader (Bitmap bitmap, Shader.TileMode tileX, Shader.TileMode tileY)                         Call this to create a new shader that will draw with a bitmap.                      Parameters                         bitmap : The bitmap to use inside the shader                         tileX : The tiling mode for x to draw the bitmap in.                         tileY : The tiling mode for y to draw the bitmap in.                 */                 /*                     Shader.TileMode                         CLAMP : replicate the edge color if the shader draws outside of its original bounds                         MIRROR : repeat the shader's image horizontally and vertically, alternating                             mirror images so that adjacent images always seam                         REPEAT : repeat the shader's image horizontally and vertically                 */                 Shader shader = new BitmapShader(bitmap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);                  /*                     public void setLayerType (int layerType, Paint paint)                         Specifies the type of layer backing this view. The layer can be LAYER_TYPE_NONE,                         LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE.                          A layer is associated with an optional Paint instance that controls how the                         layer is composed on screen.                      Parameters                         layerType : The type of layer to use with this view, must be one of                             LAYER_TYPE_NONE, LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE                         paint : The paint used to compose the layer. This argument is optional and                             can be null. It is ignored when the layer type is LAYER_TYPE_NONE                 */                 /*                     public static selesai int LAYER_TYPE_SOFTWARE                         Indicates that the view has a software layer. A software layer is backed by                         a bitmap and causes the view to be rendered using Android's software rendering                         pipeline, even if hardware acceleration is enabled.                 */                 mTV.setLayerType(View.LAYER_TYPE_SOFTWARE, null);                  /*                     Paint                         The Paint class holds the style and color information about how to draw                         geometries, text and bitmaps.                 */                 mTV.getPaint().setShader(shader);             }         });     } } 
More android examples

Komentar