1 /* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.camera.ui; 18 19 import android.content.Context; 20 import android.graphics.Bitmap; 21 import android.graphics.BitmapFactory; 22 import android.graphics.Rect; 23 24 class NinePatchTexture extends ResourceTexture { 25 private NinePatchChunk mChunk; 26 27 public NinePatchTexture(Context context, int resId) { 28 super(context, resId); 29 } 30 31 @Override 32 protected Bitmap getBitmap() { 33 if (mBitmap != null) return mBitmap; 34 35 BitmapFactory.Options options = new BitmapFactory.Options(); 36 options.inPreferredConfig = Bitmap.Config.ARGB_8888; 37 Bitmap bitmap = BitmapFactory.decodeResource( 38 mContext.getResources(), mResId, options); 39 mBitmap = bitmap; 40 setSize(bitmap.getWidth(), bitmap.getHeight()); 41 mChunk = NinePatchChunk.deserialize(bitmap.getNinePatchChunk()); 42 if (mChunk == null) { 43 throw new RuntimeException("invalid nine-patch image: " + mResId); 44 } 45 return bitmap; 46 } 47 48 public Rect getPaddings() { 49 // get the paddings from nine patch 50 if (mChunk == null) getBitmap(); 51 return mChunk.mPaddings; 52 } 53 54 public NinePatchChunk getNinePatchChunk() { 55 if (mChunk == null) getBitmap(); 56 return mChunk; 57 } 58 59 @Override 60 public void draw(GLRootView root, int x, int y, int w, int h) { 61 root.drawNinePatch(this, x, y, w, h); 62 } 63 } 64