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.android.listviewitemanimations; 18 19 import android.content.Context; 20 import android.graphics.Canvas; 21 import android.graphics.drawable.Drawable; 22 import android.util.AttributeSet; 23 import android.widget.FrameLayout; 24 25 public class BackgroundContainer extends FrameLayout { 26 27 boolean mShowing = false; 28 Drawable mShadowedBackground; 29 int mOpenAreaTop, mOpenAreaBottom, mOpenAreaHeight; 30 boolean mUpdateBounds = false; 31 32 public BackgroundContainer(Context context) { 33 super(context); 34 init(); 35 } 36 37 public BackgroundContainer(Context context, AttributeSet attrs) { 38 super(context, attrs); 39 init(); 40 } 41 42 public BackgroundContainer(Context context, AttributeSet attrs, int defStyle) { 43 super(context, attrs, defStyle); 44 init(); 45 } 46 47 private void init() { 48 mShadowedBackground = getContext().getResources().getDrawable(R.drawable.shadowed_background); 49 } 50 51 public void showBackground(int top, int bottom) { 52 setWillNotDraw(false); 53 mOpenAreaTop = top; 54 mOpenAreaHeight = bottom; 55 mShowing = true; 56 mUpdateBounds = true; 57 } 58 59 public void hideBackground() { 60 setWillNotDraw(true); 61 mShowing = false; 62 } 63 64 @Override 65 protected void onDraw(Canvas canvas) { 66 if (mShowing) { 67 if (mUpdateBounds) { 68 mShadowedBackground.setBounds(0, 0, getWidth(), mOpenAreaHeight); 69 } 70 canvas.save(); 71 canvas.translate(0, mOpenAreaTop); 72 mShadowedBackground.draw(canvas); 73 canvas.restore(); 74 } 75 } 76 77 } 78