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