DriverView.java.template 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * Copyright (C) 2012 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. package %PACKAGE%;
  17. import android.renderscript.RSSurfaceView;
  18. import android.renderscript.RenderScriptGL;
  19. import android.content.Context;
  20. import android.view.MotionEvent;
  21. public class DriverView extends RSSurfaceView {
  22. // Renderscipt context
  23. private RenderScriptGL mRS;
  24. // Script that does the rendering
  25. private DriverRS mRender;
  26. public DriverView(Context context) {
  27. super(context);
  28. ensureRenderScript();
  29. }
  30. private void ensureRenderScript() {
  31. if (mRS == null) {
  32. // Initialize renderscript with desired surface characteristics.
  33. // In this case, just use the defaults
  34. RenderScriptGL.SurfaceConfig sc = new RenderScriptGL.SurfaceConfig();
  35. mRS = createRenderScriptGL(sc);
  36. // Create an instance of the script that does the rendering
  37. mRender = new DriverRS();
  38. mRender.init(mRS, getResources());
  39. }
  40. }
  41. @Override
  42. protected void onAttachedToWindow() {
  43. super.onAttachedToWindow();
  44. ensureRenderScript();
  45. }
  46. @Override
  47. protected void onDetachedFromWindow() {
  48. // Handle the system event and clean up
  49. mRender = null;
  50. if (mRS != null) {
  51. mRS = null;
  52. destroyRenderScriptGL();
  53. }
  54. }
  55. }