橘子味的心
标题:13.5 Android TV应用实例

实例 TvDemo 演示了开发电视应用的基本过程。该实例完成了在电视上对三个 ImageButton 的导航效果,当图像按键获得焦点时会变大,其运行效果如图 1 所示。

TvDemo的运行效果
图 1  TvDemo 的运行效果

为符合 Google Play Store 的商品规范,需要首先为 TvDemo 应用程序取消触摸屏支持。在该工程的 AndroidManifest.xml 文件中加入:

<uses-feature android:required="false" android:name="android.hardware.touchscreen"/>

为了能够将该应用显示到电视的 App 列表中,需要在该工程的启动 Activity 的过滤器中添加如下代码:

<category android:name="android.intent.category.LEANBACK_LAUNCHER" />

添加后,TvDemo 就会出现在电视的 App 列表中,如图 2 所示。

电视的App列表
图 2  电视的 App 列表

该工程的 Manifest 文件内容如下:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="introduction.android.tvdemo"
  4. android:versionCode="1"
  5. android:versionName="1.0">
  6.  
  7. <uses-sdk
  8. android:minSdkVersion="22"
  9. android:targetSdkVersion="22" />
  10. <uses-feature
  11. android:name="android.hardware.touchscree"
  12. android:required="false" />
  13. <application
  14. android:allowBackup="true"
  15. android:icon="@mipmap/ic_launcher"
  16. android:label="@string/app_name"
  17. android:roundIcon="@mipmap/ic_launcher_round"
  18. android:supportsRtl="true"
  19. android:theme="@style/AppTheme">
  20. <activity android:name=".MainActivity">
  21. <intent-filter>
  22. <action android:name="android.intent.action.MAIN" />
  23. <category android:name="android.intent.category.LAUNCHER" />
  24. </intent-filter>
  25. </activity>
  26. </application>
  27.  
  28. </manifest>
其布局文件 activity_main.xml 的代码如下:
  1. <?xml version="l.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:paddingBottom="@dimen/activity_vertical_margin"
  7. android:paddingLeft="@dimen/activity_horizontal_margin"
  8. android:paddingRight="@dimen/activity_horizontal_margin"
  9. android:paddingTop="@dimen/activity_vertical_margin"
  10. tools:context="introduction.android.tvdemo.MainActivity">
  11.  
  12. <imageButton
  13. android:id="@+id/imageButton3"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:layout_alignTop="@+id/imageButton2"
  17. android:layout_marginLeft="90dp"
  18. android:layout_toRight0f="@+id/imageButton2"
  19. android:nextFocusDown="@+id/imageButton1"
  20. android: src='/uploads/android/2019/1031/22/15725328312376.jpg' />
  21.  
  22. <imageButton
  23.  
  24. android:id="@+id/imageButton1"
  25. android:layout_width="wrap_content"
  26. android:layout_height="wrap_content"
  27. android:layout_alignParentLeft="true"
  28. android:layout_alignParentTop="true"
  29. android:layout_marginLeft="39dp"
  30. android:layout_marginTop="5ldp"
  31. android:nextFocusDown="@+id/imageButton2"
  32. android: src='/uploads/android/2019/1031/22/1572532831427.jpg' />
  33.  
  34. <imageButton
  35.  
  36. android:id="@+id/imageButton2"
  37. android:layout_width="wrap_content"
  38. android:layout_height="wrap_content"
  39. android:layout_alignTop="@+id/imageButton1"
  40. android:layout_marginLeft="69dp"
  41. android:layout_toRightOf="@+id/imageButton1"
  42. android:nextFocusDown="@+id/imageButton3"
  43. android: src='/uploads/android/2019/1031/22/15725328318004.jpg' />
  44. </RelativeLayout>
布局中的三个 ImageButton 默认情况下可以通过电视的方向键进行焦点的转换。

本实例中通过“android:nextFocusDown”属性为三个图像按键添加按下按键焦点循环改变的功能。读者可以通过相关属性直接改变应用程序中的导航效果。

其主 Activity 的 Java 类代码如下:
  1. import android.app.Activity;
  2. import android.os.Bundle;
  3. import android.view.Menu;
  4. import android.view.MenuItem;
  5. import android.view.View;
  6. import android.widget.ImageButton;
  7. import android.widget.ImageView.ScaleType;
  8.  
  9. public class MainActivity extends Activity {
  10. ImageButton ivl, iv2, iv3;
  11. private String tag = "TV";
  12.  
  13. @Override
  14. protected void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.activity_main);
  17. ivl = (ImageButton) this.findViewById(R.id.imageButtonl);
  18. iv2 = (ImageButton) this.findViewById(R.id.imageButton2);
  19. iv3 = (ImageButton) this.findViewById(R.id.imageButton3);
  20. ivl.setOnFocusChangeListener(new View.OnFocusChangeListener() {
  21. @Override
  22. public void onFocusChange(View v, boolean hasFocus) {
  23. // TODO Auto-generated method stub
  24. ivl.setScaleType(ScaleType.CENTER);
  25. if (hasFocus) {
  26. ivl.setScaleX(1.3f);
  27. ivl.setScaleY(1.3f);
  28. } else {
  29. ivl.setScaleX(1.0f);
  30. ivl.setScaleY(1.0f);
  31. }
  32. }
  33. });
  34. iv2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
  35. @Override
  36. public void onFocusChange(View v, boolean hasFocus) {
  37. // TODO Auto-generated method stub
  38. iv2.setScaleType(ScaleType.CENTER);
  39. if (hasFocus) {
  40. iv2.setScaleX(1.3f);
  41. iv2.setScaleY(1.3f);
  42. } else {
  43. iv2.setScaleX(1.0f);
  44. iv2.setScaleY(1.0f);
  45. }
  46. }
  47. });
  48. iv3.setOnFocusChangeListener(new View.OnFocusChangeListener() {
  49. @Override
  50. public void onFocusChange(View v, boolean hasFocus) {
  51. // TODO Auto-generated method stub
  52. iv2.setScaleType(ScaleType.CENTER);
  53. if (hasFocus) {
  54. iv3.setScaleX(1.3f);
  55. iv3.setScaleY(1.3f);
  56. } else {
  57. iv3.setScaleX(1.0f);
  58. iv3.setScaleY(1.0f);
  59. }
  60. }
  61. });
  62. }
  63.  
  64. @Override
  65. public boolean onCreateOptionsMenu(Menu menu) {
  66. // Inflate the menu; this adds items to the action bar if it is present.
  67. getMenuInflater().inflate(R.menu.main, menu);
  68. return true;
  69. }
  70.  
  71. @Override
  72. public boolean onOptionsItemSelected(MenuItem item) {
  73. // Handle action bar item clicks here. The action bar will
  74. // automatically handle clicks on the Home/Up button, so long
  75. // as you specify a parent activity in AndroidManifest.xml.
  76. int id = item.getItemId();
  77. if (id == R.id.action_settings) {
  78. return true;
  79. return super.onOptionsItemSelected(item);
  80. }
  81. }
  82. }
其中的代码段:
  1. ivl.setOnFocusChangeListener(new View.OnFocusChangeListener() {
  2. @Override
  3. public void onFocusChange(View v, boolean hasFocus) {
  4. // TODO Auto-generated method stub
  5. ivl.setScaleType(ScaleType.CENTER);
  6. if (hasFocus) {
  7. ivl.setScaleX(1.3f);
  8. ivl.setScaleY(1.3f);
  9. } else {
  10. ivl.setScaleX(1.0f);
  11. ivl.setScaleY(1.0f);
  12. }
  13. }
  14. });
表示图像按键 1 获得焦点后即调用 onFocusChange 方法进行处理。具体的处理方法为改变按键的显示范围为原来的 1.3 倍,即增大显示。