Home > Devices > Fire Phone

Programmatically Generating ZSceneViews and Other Controls

Introduction

The ZSceneView class included in the Euclid package of the Dynamic Perspective UI  enables you to create custom controls for your app. This page describes how to programmatically generate these ZSceneView widgets, so that you do not have to hard code all of your controls into a layout XML file. Generating your widgets dynamically can be helpful when you are creating many objects with the same content, such as pages within a book. This topic uses the scenario of the fictional ZReader app that can flip through multiple pages as an example.

Generating a Control

To dynamically generate N number of pages on the screen:

  1. In the main.xml file for your app, specify a Z- layout for your widgets (ZLinearLayout, ZFrameLayout, or ZRelativeLayout). This example specifies a ZFrameLayout and names it overlay_frame. You do not need to make any additional changes to your main.xml file.
  2. Initialize your Z- layout in your Java code.
  3. Place your custom assets in your res/raw directory.
  4. Set your layout parameters. Use the appropriate layout parameters for the Z- layout that you are using. The following code snippet sets the layout parameters for the example ZFrameLayout:
    final ZFrameLayout.LayoutParams params = new ZFrameLayout.LayoutParams(ZFrameLayout.LayoutParams.WRAP_CONTENT, ZFrameLayout.LayoutParams.WRAP_CONTENT);
    params.depth = ZFrameLayout.LayoutParams.WRAP_CONTENT;
    

    Note that you must set the depth parameter for your layout.
  5. Define the objects that you are generating, including the number of objects to be generated.

    For the ZReader app, assume that you need a pool of 30 pages. Create a loop, and every time that you generate a page, place it into an array:
    ZSceneView mPagesCollector[] = new ZSceneView[30];
    for (int i = 0; i < mPagesCollector.length; ++i) {
               final ZSceneView mPageGenerated = new ZSceneView(getContext());
               mPageGenerated.getDelegate().setScene(R.raw.reader_proto, "reader_page.scene");
               mPageGenerated.setLayoutParams(params);
               overlayFrame.addView(mPageGenerated);
               mPagesCollector[i] = mPageGenerated;
           }
    

    In this example, generate a ZSceneView, set your specified params, and add this view to your Z layout from the first step (overlayFrame).
  6. Now you have many ZSceneViews in the same place of the screen, you can move them by specifying width, height in the layout params.