이미지 리소스를 조금이라도 줄여볼까 하는 생각으로 배경 이미지를 패턴으로 깔아주는 것에 대해서 좀 알아 보았다.


아이폰에서는 쉽게 아래와 같이 하면 패턴이 적용된다.


1:    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_pattern.png"]]];  


안드로이드에서는 어떻게 하는 지 좀 알아봤다.


1. xml을 이용한 배경 이미지 패턴 설정

xml을 이용하는 방법은 일반 레이아웃 사용할 때 활용하면 될 듯하다.

적용한는 방법은 간단하다.


1) Drawable 폴더에 비트맵 xml  파일을 만든다. (background_pattern.xml로 만든다)

타일 이미지 리소스를 지정하고 tileMode를 "repeat"로 하면 된다. (titleMode에는 clamp, micro, repeat 가 있다)

1:  <?xml version="1.0" encoding="utf-8"?>  
2:  <bitmap xmlns:android="http://schemas.android.com/apk/res/android"  
3:    android:src="@drawable/bg_pattern"   
4:    android:tileMode="repeat" />  


2) 해당 xml을 패턴을 설정할 레이아웃의 background로 지정하면 된다.

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




2. 코드로 배경 이미지 패턴 설정

1:  BitmapDrawable bg;  
2:  bg = new BitmapDrawable(BitmapFactory.decodeResource(getResources(),R.drawable.bg_pattern));  
3:  bg.setBounds(0, 0, getWidth(), getHeight());  
4:  bg.setTileModeX(Shader.TileMode.REPEAT);  
5:  bg.draw(canvas);  

코드로도 간단하게 배경을 채울 수 있다.

드로우하는 코드 안에서 BitmapDrawable을 하나 생성해서 리소스를 읽고, 해당 리소스를 전체 화면의 영역을 얻어와서 해당 영역에 타일 반복으로 캔버스에 그려주면 된다.


+ Recent posts