ResourceTexture.java revision f9a0a4306d589b4a4e20554fed512a603426bfa1
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
17package com.android.gallery3d.ui;
18
19import com.android.gallery3d.common.Utils;
20
21import android.content.Context;
22import android.graphics.Bitmap;
23import android.graphics.BitmapFactory;
24
25// ResourceTexture is a texture whose Bitmap is decoded from a resource.
26// By default ResourceTexture is not opaque.
27public class ResourceTexture extends UploadedTexture {
28
29    protected final Context mContext;
30    protected final int mResId;
31
32    public ResourceTexture(Context context, int resId) {
33        mContext = Utils.checkNotNull(context);
34        mResId = resId;
35        setOpaque(false);
36    }
37
38    @Override
39    protected Bitmap onGetBitmap() {
40        BitmapFactory.Options options = new BitmapFactory.Options();
41        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
42        return BitmapFactory.decodeResource(
43                mContext.getResources(), mResId, options);
44    }
45
46    @Override
47    protected void onFreeBitmap(Bitmap bitmap) {
48        if (!inFinalizer()) {
49            bitmap.recycle();
50        }
51    }
52}
53