Home | History | Annotate | Download | only in tinyplanet
      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.android.camera.tinyplanet;
     18 
     19 import android.content.Context;
     20 import android.graphics.Bitmap;
     21 import android.graphics.Canvas;
     22 import android.graphics.Paint;
     23 import android.util.AttributeSet;
     24 import android.view.View;
     25 
     26 import java.util.concurrent.locks.Lock;
     27 
     28 /**
     29  * Shows a preview of the TinyPlanet on the screen while editing.
     30  */
     31 public class TinyPlanetPreview extends View {
     32     /**
     33      * Classes implementing this interface get informed about changes to the
     34      * preview size.
     35      */
     36     public static interface PreviewSizeListener {
     37         /**
     38          * Called when the preview size has changed.
     39          *
     40          * @param sizePx the size in pixels of the square preview area
     41          */
     42         public void onSizeChanged(int sizePx);
     43     }
     44 
     45     private Paint mPaint = new Paint();
     46     private Bitmap mPreview;
     47     private Lock mLock;
     48     private PreviewSizeListener mPreviewSizeListener;
     49     private int mSize = 0;
     50 
     51     public TinyPlanetPreview(Context context) {
     52         super(context);
     53     }
     54 
     55     public TinyPlanetPreview(Context context, AttributeSet attrs) {
     56         super(context, attrs);
     57     }
     58 
     59     public TinyPlanetPreview(Context context, AttributeSet attrs, int defStyle) {
     60         super(context, attrs, defStyle);
     61     }
     62 
     63     /**
     64      * Sets the bitmap and waits for a draw to happen before returning.
     65      */
     66     public void setBitmap(Bitmap preview, Lock lock) {
     67         mPreview = preview;
     68         mLock = lock;
     69         invalidate();
     70     }
     71 
     72     public void setPreviewSizeChangeListener(PreviewSizeListener listener) {
     73         mPreviewSizeListener = listener;
     74         if (mSize > 0) {
     75             mPreviewSizeListener.onSizeChanged(mSize);
     76         }
     77     }
     78 
     79     @Override
     80     protected void onDraw(Canvas canvas) {
     81         super.onDraw(canvas);
     82         if (mLock != null && mLock.tryLock()) {
     83             try {
     84                 if (mPreview != null && !mPreview.isRecycled()) {
     85                     canvas.drawBitmap(mPreview, 0, 0, mPaint);
     86                 }
     87             } finally {
     88                 mLock.unlock();
     89             }
     90         }
     91     }
     92 
     93     @Override
     94     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
     95         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
     96 
     97         // Make sure the view is square
     98         int size = Math.min(getMeasuredWidth(), getMeasuredHeight());
     99         setMeasuredDimension(size, size);
    100     }
    101 
    102     @Override
    103     protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    104         super.onLayout(changed, left, top, right, bottom);
    105         if (changed && mPreviewSizeListener != null) {
    106             int width = right - left;
    107             int height = bottom - top;
    108 
    109             // These should be the same as we enforce a square layout, but let's
    110             // be safe.
    111             int mSize = Math.min(width, height);
    112 
    113             // Tell the listener about our new size so the renderer can adapt.
    114             if (mSize > 0 && mPreviewSizeListener != null) {
    115                 mPreviewSizeListener.onSizeChanged(mSize);
    116             }
    117         }
    118     }
    119 }
    120