Android Api Demos登顶之路(十二)Presentation

这个demo主要演示presentation的用法,presentation是一种特殊的对话框,主要用于在另外一块屏幕上显示内容。值得注意的是在创建presentation前必须把presentation与它的目标屏幕相关联。所以在使用它之前必须为它选择一个Display。
选择display的方法主要有两种
一是利用MediaRouter
二是利用DisplayManager,
本例演示的是后一种方法
创建主布局文件:activity_main.xml
主要是一个用于控制显示所有屏幕的复选框和一个显示所有屏幕的列表框

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="5dp"
    android:orientation="vertical">

    <TextView 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="       Tish demostrates how to use a presentation and the DisplayManger to show content on other display
        try connecting a second display and watch what happenes selecting a display and will open a presentatio on it."/>
    <CheckBox 
        android:id="@+id/show_all_presentation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="显示所有pressentation"/>
    <ListView 
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </ListView>


</LinearLayout>

编写MainActivity

public class MainActivity extends Activity implements OnCheckedChangeListener {
   

    private static final String PRESENTATION_KEY = "presentation";
    private DisplayManager displayManager;
    private CheckBox show_all_displays;
    private DisplayListAdapter displayAdapter;
    private ListView listView;
    // 定义存储photo的数组
    private int[] photos = new int[] { R.drawable.guild1, R.drawable.guild2,
            R.drawable.guild3, R.drawable.ic_launcher, R.drawable.puzzle,
            R.drawable.test };
    // 记录下一张照片的Id
    private int nextPhotoNumber = 0;
    /*
     * 利用SparseArray存储所有屏幕的prsentation上显示的内容(尤其是当屏幕不止一个时数组的作用更大)
     * SparseArray:稀疏数组,在存储数据不多的情况下可以大大地节省空间,此时可以代替hasmap使用
     */
    SparseArray<PresentationContents> mSavedPresentationContents;

    // 根据displayId记录当前显示的presentation
    SparseArray<DemoPresentation> mActivePresentations = new SparseArray<DemoPresentation>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 首先检查是否存在需要恢复的状态信息
        if (savedInstanceState != null) {
            mSavedPresentationContents = savedInstanceState
                    .getSparseParcelableArray(PRESENTATION_KEY);
        } else {
            mSavedPresentationContents = new SparseArray<PresentationContents>();
        }
        displayManager = (DisplayManager) getSystemService(Context.DISPLAY_SERVICE);
        show_all_displays = (CheckBox) findViewById(R.id.show_all_presentation);
        show_all_displays.setOnCheckedChangeListener(this);
        listView = (ListView) findViewById(R.id.listView);
        displayAdapter = new DisplayListAdapter(this);
        listView.setAdapter(displayAdapter);
    }

    /**
     * 定义listView的适配器,用于显示所有的屏幕信息
     * 
     * @author aaaa
     *
     */
    private class 
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值