Three methods to achieve Android tile Effect

Source: Internet
Author: User

There are several methods to achieve tile effect.

First, use the api provided by the system:

 
 
  1. Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pic); 
  2.  
  3. //bitmap = Bitmap.createBitmap(200, 30, Config.ARGB_8888); 
  4. BitmapDrawable drawable = new BitmapDrawable(bitmap); 
  5. drawable.setTileModeXY(TileMode.REPEAT , TileMode.REPEAT ); 
  6. drawable.setDither(true); 
  7. view.setBackgroundDrawable(drawable);  

Second, it is easy to implement using xml, which may appear after 4.0:

 
 
  1. <bitmap xmlns:Android="http://schemas.android.com/apk/res/android"    
  2. android:src="../../@drawable/img" 
  3. Android:tileMode="repeat" />  

Third, draw it by yourself:

 
 
  1. public static Bitmap createRepeater(int width, Bitmap src){ 
  2. int count = (width + src.getWidth() - 1) / src.getWidth(); 
  3.  
  4. Bitmap bitmap = Bitmap.createBitmap(width, src.getHeight(), Config.ARGB_8888); 
  5. Canvas canvas = new Canvas(bitmap); 
  6.  
  7.     for(int idx = 0; idx < count; ++ idx){ 
  8.         canvas.drawBitmap(src, idx * src.getWidth(), 0, null); 
  9.      } 
  10.  
  11.      return bitmap; 
  12. }  

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.