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 
17 package com.example.bitmapsample;
18 
19 import android.content.Context;
20 import android.graphics.Canvas;
21 import android.util.AttributeSet;
22 import android.widget.ListView;
23 
24 import com.android.bitmap.drawable.ExtendedBitmapDrawable;
25 import com.android.bitmap.view.BitmapDrawableImageView;
26 
27 public class BitmapView extends BitmapDrawableImageView {
28     private final float mDensity;
29 
30     private ListView mListView;
31 
BitmapView(Context c)32     public BitmapView(Context c) {
33         this(c, null);
34     }
35 
BitmapView(Context c, AttributeSet attrs)36     public BitmapView(Context c, AttributeSet attrs) {
37         super(c, attrs);
38         mDensity = getResources().getDisplayMetrics().density;
39     }
40 
41     @Override
getSuggestedMinimumHeight()42     protected int getSuggestedMinimumHeight() {
43         return (int) (100 * mDensity);
44     }
45 
46     @Override
onSizeChanged(final int w, final int h, int oldw, int oldh)47     protected void onSizeChanged(final int w, final int h, int oldw, int oldh) {
48         ExtendedBitmapDrawable drawable = getTypedDrawable();
49         drawable.setDecodeDimensions(w, h);
50     }
51 
setListView(final ListView listView)52     public void setListView(final ListView listView) {
53         mListView = listView;
54     }
55 
56     @Override
onDraw(final Canvas canvas)57     protected void onDraw(final Canvas canvas) {
58         ExtendedBitmapDrawable drawable = getTypedDrawable();
59         float fraction = (float) getBottom() / (mListView.getHeight() + getHeight());
60         drawable.setParallaxFraction(fraction);
61 
62         super.onDraw(canvas);
63     }
64 }