android - Layout Tiled background programmatically

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"     >     <Button         android:id="@+id/btn"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="Apply Tiled Background"         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.Shader; import android.graphics.drawable.BitmapDrawable; 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;   public class MainActivity extends AppCompatActivity {     private Context mContext;     private RelativeLayout mRelativeLayout;     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         mRelativeLayout = (RelativeLayout) findViewById(R.id.rl);         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.wood);                  /*                     BitmapDrawable                         A Drawable that wraps a bitmap and can be tiled, stretched, or aligned. You                         can create a BitmapDrawable from a file path, an input stream, through XML                         inflation, or from a Bitmap object.                          It can be defined in an XML file with the <bitmap> element.                 */                 /*                     public BitmapDrawable (Resources res, Bitmap bitmap)                         Create drawable from a bitmap, setting initial target density based on the                         display metrics of the resources.                 */                 BitmapDrawable drawable = new BitmapDrawable(getResources(),bitmap);                  /*                     public void setTileModeXY (Shader.TileMode xmode, Shader.TileMode ymode)                         Sets the repeat behavior of this drawable on both axis. By default, the                         drawable does not repeat its bitmap. Using REPEAT or MIRROR the bitmap can                         be repeated (or tiled) if the bitmap is smaller than this drawable.                      Parameters                         xmode : The X repeat mode for this drawable.                         ymode : The Y repeat mode for this drawable.                  */                 /*                     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                 */                 drawable.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);                  /*                     public void setBackground (Drawable background)                         Set the background to a given Drawable, or remove the background. If the                         background has padding, this View's padding is set to the background's padding.                         However, when a background is removed, this View's padding isn't touched.                         If setting the padding is desired, please use setPadding(int, int, int, int).                      Parameters                         background : The Drawable to use as the background, or null to remove the background                  */                 mRelativeLayout.setBackground(drawable);             }         });     } } 
More android examples

Komentar