BitmapView.java revision 9d46a01e044696eb4d101778992fed4966520251
1/*
2 * Copyright (C) 2013 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.example.bitmapsample;
18
19import android.content.Context;
20import android.os.Build;
21import android.util.AttributeSet;
22import android.view.View;
23
24import com.android.bitmap.BitmapCache;
25import com.android.bitmap.drawable.BasicBitmapDrawable;
26
27public class BitmapView extends View {
28    private BasicBitmapDrawable mBitmapDrawable;
29    private float mDensity;
30
31    public BitmapView(Context c) {
32        this(c, null);
33    }
34
35    public BitmapView(Context c, AttributeSet attrs) {
36        super(c, attrs);
37        mDensity = getResources().getDisplayMetrics().density;
38    }
39
40    @Override
41    protected int getSuggestedMinimumHeight() {
42        return (int) (100 * mDensity);
43    }
44
45    @Override
46    protected void onSizeChanged(final int w, final int h, int oldw, int oldh) {
47        mBitmapDrawable.setDecodeDimensions(w, h);
48    }
49
50    public void setImage(String uriString) {
51        if (mBitmapDrawable != null) {
52            mBitmapDrawable.bind(new BitmapRequestKeyImpl(uriString));
53        }
54    }
55
56    public void initialize(BitmapCache cache) {
57        mBitmapDrawable = new BasicBitmapDrawable(getResources(), cache);
58        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
59            setBackground(mBitmapDrawable);
60        } else {
61            setBackgroundDrawable(mBitmapDrawable);
62        }
63    }
64
65}