2010年10月19日火曜日

Android 背景画像を repeat させる

背景画像を repeat させたい場合、ImageView とかの属性では実現できません。 BitmapDrawable で repeat した画像を作って、背景に指定するば OK です。

BitmapDrawable はコードからも生成できますが、xml で作ったほうが楽です。 Bitmap Resource の XML Bitmap のところを参照してね。

XML ファイルは
 res/drawable/filename.xml

XML ファイルの中身の SYNTAX
  1. <!--xml version="1.0" encoding="utf-8"?-->  
  2. <bitmap xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:src="@[package:]drawable/drawable_resource"  
  4.     android:antialias=["true" | "false"]  
  5.     android:dither=["true" | "false"]  
  6.     android:filter=["true" | "false"]  
  7.     android:gravity=["top" | "bottom" | "left" |  
  8.            "right" | "center_vertical" |   
  9.            "fill_vertical" | "center_horizontal" |   
  10.            "fill_horizontal" | "center" | "fill" |   
  11.            "clip_vertical" | "clip_horizontal"]  
  12.     android:tileMode=["disabled" | "clamp" |   
  13.                       "repeat" | "mirror"] />  



ここの android:tileMode に repeat を指定すると、android:src で指定した
画像がタイル状になった Bitmap ができます。


この Bitmap をコードから指定するときは
  R.drawable.filename

レイアウトXMLの ImageView の android:src や android:backgroundに
指定するときは
  @[package:]drawable/filename

とすればOK

こんな感じ



res/drawable/tilebg.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <bitmap xmlns:android="http://schemas.android.com/apk/res/android"  
  3.  android:src="@drawable/icon"  
  4.         android:antialias="true"  
  5.         android:dither="false"  
  6.         android:filter="false"  
  7.         android:gravity="fill"  
  8.         android:tileMode="repeat" />  


res/layout/main.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:background="@drawable/tilebg"  
  7.     >  
  8. </LinearLayout>  


 

0 件のコメント:

コメントを投稿