ResourceTexture.java revision f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809e
1package com.cooliris.media; 2 3import java.io.IOException; 4import java.io.InputStream; 5 6import android.graphics.Bitmap; 7import android.graphics.BitmapFactory; 8 9public final class ResourceTexture extends Texture { 10 private final int mResourceId; 11 private final boolean mScaled; 12 13 @Override 14 public boolean isCached() { 15 return true; 16 } 17 18 public ResourceTexture(int resourceId, boolean scaled) { 19 mResourceId = resourceId; 20 mScaled = scaled; 21 } 22 23 @Override 24 protected Bitmap load(RenderView view) { 25 // Load a bitmap from the resource. 26 Bitmap bitmap = null; 27 if (mScaled) { 28 BitmapFactory.Options options = new BitmapFactory.Options(); 29 options.inPreferredConfig = Bitmap.Config.ARGB_8888; 30 bitmap = BitmapFactory.decodeResource(view.getResources(), mResourceId, options); 31 } else { 32 InputStream inputStream = view.getResources().openRawResource(mResourceId); 33 if (inputStream != null) { 34 try { 35 BitmapFactory.Options options = new BitmapFactory.Options(); 36 options.inPreferredConfig = Bitmap.Config.ARGB_8888; 37 bitmap = BitmapFactory.decodeStream(inputStream, null, options); 38 } catch (Exception e) { 39 } finally { 40 try { 41 inputStream.close(); 42 } catch (IOException e) { /* ignore */ 43 } 44 } 45 } 46 } 47 return bitmap; 48 } 49} 50