android背景圖片平鋪

Android背景平鋪: java

 在drawable文件夾下創建以下文件bg.xml: android

<?xml version ="1.0" encoding ="utf-8" ?>  
<bitmap xmlns:android ="http://schemas.android.com/apk/res/android"  
   android:src ="@drawable/pattern"  
   android:tileMode ="repeat" />
在佈局中以下引用:


<LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/repeat_bg"></LinearLayout>
=======================釋意以下=====================


tileMode :屬性就是用於定義背景的顯示模式: disabled 默認值,表示不使用平鋪  canvas

clamp : 複製邊緣色彩  api

repeat  :X、Y 軸進行重複圖片顯示,也就是咱們說要說的平鋪  佈局

mirror : 在水平和垂直方向上使用交替鏡像的方式重複圖片的繪製 spa

圖片平鋪的三種方式:
  1)第一種利用系統提供的api實現
        
code

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pic);
   //bitmap = Bitmap.createBitmap(100, 20, Config.ARGB_8888);
   BitmapDrawable drawable = new BitmapDrawable(bitmap);
   drawable.setTileModeXY(TileMode.REPEAT , TileMode.REPEAT );
   drawable.setDither(true);
   view.setBackgroundDrawable(drawable);

  

  2)第二種咱們使用xml來輕鬆實現 xml


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

      3)第三種本身畫出來 圖片

public static Bitmap createRepeater(int width, Bitmap src){
 int count = (width + src.getWidth() - 1) / src.getWidth();
 Bitmap bitmap = Bitmap.createBitmap(width, src.getHeight(), Config.ARGB_8888);
 Canvas canvas = new Canvas(bitmap);
 for(int idx = 0; idx < count; ++ idx){
 canvas.drawBitmap(src, idx * src.getWidth(), 0, null);
 }
 return bitmap;
}
     
相關文章
相關標籤/搜索