본문 바로가기

Android

[android]Bitmap to NinePatch

NinePatch 용량 최적화는 sdk/tools/draw9patch.bat 이용


1. 나인패치 이미지 준비 - test.9.png


2. 버튼 Listener

// 9patch 적용
public void SetNine(View v) {
View layoutView = findViewById(R.id.view);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.test);
if (bitmap.getNinePatchChunk() == null)
return;
// chunk::array형 이미지정보(반드시 NinePatch정보를 포함해야 not null)
byte[] chunk = bitmap.getNinePatchChunk();
NinePatch patch = new NinePatch(bitmap, chunk, null);
NinePatchDrawable drawable = new NinePatchDrawable(getResources(), patch);
// Image 설정
drawable.getPaint().setAntiAlias(false); // default::false
drawable.getPaint().setDither(false); // default::false
drawable.getPaint().setFilterBitmap(false); // Filter끄기 _ default::true
layoutView.setBackground(drawable);
}


3. Image 설정 부분 code를 xml로 정의해서 사용할 수도 있음(drawable폴더 생성 후 작성)

<Bitmap
android:src="@drawable/bitmap"
android:antialias="false"
android:dither="false"
android:filter="false"
android:tilemode="clamp"/>