Home | History | Annotate | Download | only in testapp
      1 /*
      2  * Copyright (C) 2011 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.testapp;
     18 
     19 import java.io.Writer;
     20 import java.util.ArrayList;
     21 import java.util.concurrent.Semaphore;
     22 
     23 import android.renderscript.RSSurfaceView;
     24 import android.renderscript.RenderScript;
     25 import android.renderscript.RenderScriptGL;
     26 
     27 import android.content.Context;
     28 import android.content.res.Resources;
     29 import android.graphics.Bitmap;
     30 import android.graphics.drawable.BitmapDrawable;
     31 import android.graphics.drawable.Drawable;
     32 import android.os.Handler;
     33 import android.os.Message;
     34 import android.util.AttributeSet;
     35 import android.util.Log;
     36 import android.view.Surface;
     37 import android.view.SurfaceHolder;
     38 import android.view.SurfaceView;
     39 import android.view.KeyEvent;
     40 import android.view.MotionEvent;
     41 import android.view.ScaleGestureDetector;
     42 
     43 public class TestAppView extends RSSurfaceView {
     44 
     45     public TestAppView(Context context) {
     46         super(context);
     47         mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
     48     }
     49 
     50     private RenderScriptGL mRS;
     51     TestAppRS mRender;
     52 
     53     private ScaleGestureDetector mScaleDetector;
     54     private static final int INVALID_POINTER_ID = -1;
     55     private int mActivePointerId = INVALID_POINTER_ID;
     56 
     57     public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
     58         super.surfaceChanged(holder, format, w, h);
     59         if (mRS == null) {
     60             RenderScriptGL.SurfaceConfig sc = new RenderScriptGL.SurfaceConfig();
     61             sc.setDepth(16, 24);
     62             sc.setSamples(1, 2, 1);
     63             mRS = createRenderScriptGL(sc);
     64             mRS.setSurface(holder, w, h);
     65             mRender = new TestAppRS();
     66             mRender.init(mRS, getResources(), w, h);
     67         }
     68     }
     69 
     70     @Override
     71     protected void onDetachedFromWindow() {
     72         if (mRS != null) {
     73             mRender = null;
     74             mRS = null;
     75             destroyRenderScriptGL();
     76         }
     77     }
     78 
     79     @Override
     80     public boolean onKeyDown(int keyCode, KeyEvent event)
     81     {
     82         // break point at here
     83         // this method doesn't work when 'extends View' include 'extends ScrollView'.
     84         return super.onKeyDown(keyCode, event);
     85     }
     86 
     87 
     88     @Override
     89     public boolean onTouchEvent(MotionEvent ev) {
     90         mScaleDetector.onTouchEvent(ev);
     91 
     92         boolean ret = false;
     93         float x = ev.getX();
     94         float y = ev.getY();
     95 
     96         final int action = ev.getAction();
     97 
     98         switch (action & MotionEvent.ACTION_MASK) {
     99         case MotionEvent.ACTION_DOWN: {
    100             mRender.onActionDown(x, y);
    101             mActivePointerId = ev.getPointerId(0);
    102             ret = true;
    103             break;
    104         }
    105         case MotionEvent.ACTION_MOVE: {
    106             if (!mScaleDetector.isInProgress()) {
    107                 mRender.onActionMove(x, y);
    108             }
    109             mRender.onActionDown(x, y);
    110             ret = true;
    111             break;
    112         }
    113 
    114         case MotionEvent.ACTION_UP: {
    115             mActivePointerId = INVALID_POINTER_ID;
    116             break;
    117         }
    118 
    119         case MotionEvent.ACTION_CANCEL: {
    120             mActivePointerId = INVALID_POINTER_ID;
    121             break;
    122         }
    123 
    124         case MotionEvent.ACTION_POINTER_UP: {
    125             final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK)
    126                     >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
    127             final int pointerId = ev.getPointerId(pointerIndex);
    128             if (pointerId == mActivePointerId) {
    129                 // This was our active pointer going up. Choose a new
    130                 // active pointer and adjust accordingly.
    131                 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
    132                 x = ev.getX(newPointerIndex);
    133                 y = ev.getY(newPointerIndex);
    134                 mRender.onActionDown(x, y);
    135                 mActivePointerId = ev.getPointerId(newPointerIndex);
    136             }
    137             break;
    138         }
    139         }
    140 
    141         return ret;
    142     }
    143 
    144     private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
    145         @Override
    146         public boolean onScale(ScaleGestureDetector detector) {
    147             mRender.onActionScale(detector.getScaleFactor());
    148             return true;
    149         }
    150     }
    151 }
    152 
    153 
    154