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