Home | History | Annotate | Download | only in ui
      1 /*******************************************************************************
      2  *      Copyright (C) 2012 Google Inc.
      3  *      Licensed to The Android Open Source Project.
      4  *
      5  *      Licensed under the Apache License, Version 2.0 (the "License");
      6  *      you may not use this file except in compliance with the License.
      7  *      You may obtain a copy of the License at
      8  *
      9  *           http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *      Unless required by applicable law or agreed to in writing, software
     12  *      distributed under the License is distributed on an "AS IS" BASIS,
     13  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *      See the License for the specific language governing permissions and
     15  *      limitations under the License.
     16  *******************************************************************************/
     17 
     18 package com.android.mail.ui;
     19 
     20 import android.content.Context;
     21 import android.graphics.Bitmap;
     22 import android.graphics.Bitmap.Config;
     23 import android.graphics.Canvas;
     24 import android.util.AttributeSet;
     25 import android.view.View;
     26 
     27 import com.android.mail.utils.LogTag;
     28 import com.android.mail.utils.LogUtils;
     29 
     30 /**
     31  * A normally inert view that draws a bitmap copy of the existing conversation list view when
     32  * transitioning.
     33  *
     34  */
     35 public class ConversationListCopy extends View {
     36 
     37     private Bitmap mBitmap;
     38 
     39     private static final String LOG_TAG = LogTag.getLogTag();
     40 
     41     public ConversationListCopy(Context c) {
     42         this(c, null);
     43     }
     44 
     45     public ConversationListCopy(Context c, AttributeSet attrs) {
     46         super(c, attrs);
     47     }
     48 
     49     @Override
     50     protected void onDraw(Canvas canvas) {
     51         if (mBitmap == null) {
     52             return;
     53         }
     54         canvas.drawBitmap(mBitmap, 0, 0, null);
     55     }
     56 
     57     /**
     58      * Copy a bitmap of the source view.
     59      * <p>
     60      * Callers MUST call {@link #unbind()} when the copy is no longer needed.
     61      *
     62      * @param v
     63      */
     64     public void bind(View v) {
     65         unbind();
     66 
     67         if (v.getWidth() == 0 || v.getHeight() == 0) {
     68             return;
     69         }
     70 
     71         try {
     72             mBitmap = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Config.ARGB_8888);
     73             v.draw(new Canvas(mBitmap));
     74         } catch (OutOfMemoryError e) {
     75             LogUtils.e(LOG_TAG, e, "Unable to create fancy list transition bitmap");
     76             // mBitmap will just be null, and this won't draw, which is fine
     77         }
     78     }
     79 
     80     /**
     81      * Release resources from a previous {@link #bind(View)}.
     82      */
     83     public void unbind() {
     84         if (mBitmap != null) {
     85             mBitmap.recycle();
     86             mBitmap = null;
     87         }
     88     }
     89 
     90 }
     91