【Andriod开发】智能TV优化Android应用界面布局
?
当你应用程序运行在电视机时,你应当考虑用户是坐在距离电视屏幕10英尺的地方.当然用户环境被称为10-foot UI.为了给你的用户提供一个可用和愉快的体验,你相应地应该奠定你自己的UI风格...
这个要点向你展示如何针对TV来优化你自己的布局,通过:
电视屏幕通常都是取决于景观.针对电视屏幕按照这些技巧来优化你的景观布局:
例如接下来的布局就是针对TV优化的:
在这个布局中,控制是位于左边界.这个UI界面里面显示了一个GridView控件,这个GridView是非常适合景观方向的UI.在这个布局中GridView和Fragment
都是动态设置宽和高,以便能自适应屏幕的分辨率.在运行时控制视图被添加到了左边片段编程.这UI布局文件位res/layoutlandlarge/photogrid_tv.xml下.(这个布局文件放置在layout-land-large,因为电视在景观方向上有大的屏幕.详细请参考Supporting Multiple Screens.)
例如如下一个res/layout-land-large/photogrid_tv.xml:
1 2 3 4 5 6 7 8 9101112
<RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent"> <fragment android:id="@+id/leftsidecontrols" android:layout_width="0dip" android:layout_marginLeft="5dip" android:layout_height="match_parent" /> <GridView android:id="@+id/gridview" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout>
为了在屏幕的左边设立操作条选项,你可以在你的应用程序中通过导入Left navigation bar library来实现它,以替代创建一个定制的片段来添加控制视图:
1
= (.instance()).getLeftNavBar(this);
当你有一个内容垂直滚动的活动时,总是使用一个左边导航条;反之,你的用户不得不拖动到内容的顶部来在内容视图和操作条之间进行切换.参考Left navigation bar sample app来看看它是如何简单的在你的应用程序中导入左边导航条的.
在一个电视的应用程序的UI里,文字和控制应当是容易看见并且在一定距离内可以导航的.例如这些技巧能让它们在一定距离内更加容易看见:
1234567
<TextView android:id="@+id/atext" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_vertical" android:singleLine="true" android:textAppearance="?android:attr/textAppearanceMedium"/>
普遍的高清电视显示分辨率是720p,1080i,和1080p.把你的UI设置为1080p,并且允许系统在必要的时候将你的UI分辨率下调到720p.通常下调并不意味着是缩小你的UI界面(注意反之则不然,你应该避免倍增,因为它会降低UI质量).
为了获得最佳的图片显示,如果可能的话以9-patch image元素来展示.在你的布局中如果提供低质量或者小图片的话,它们将会出现像素化,模糊,或呈颗粒状.对用户来说这不是一个好的用户体验.取而代之的是采用高质量的图片.
在大屏幕上针对优化应用的更多信息请参考Designing for multiple screens.
android系统有一个内存限制量,所以在你的应用程序中下载和保存一个高分辨率的图片经常会导致内存溢出的错误.为了避免这些,使用如下这些技巧:
范例代码:
1 2 3 4 5 6 7 8 9101112131415161718192021222324252627282930313233343536373839
// Get the source image's dimensions .Options = new .Options(); // This does not download the actual image, just downloads headers. .inJustDecodeBounds = true; .decodeFile(, ); // The actual width of the image. int = .outWidth; // The actual height of the image. int = .outHeight; // Only scale if the source is bigger than the width of the destination view. if(> ) = ; // Calculate the correct inSampleSize/scale value. This helps reduce memory use. It should be a power of 2. int = 1; while(/ 2 > ){ /= 2; /= 2; * = 2; } float = (float) / ; // Decode with inSampleSize .inJustDecodeBounds = false; .inDither = false; .inSampleSize = ; .inScaled = false; // Ensures the image stays as a 32-bit ARGB_8888 image. // This preserves image quality. .inPreferredConfig = .Config.ARGB_8888; = .decodeFile(, ); // Resize = new (); .postScale(, ); = .createBitmap(, 0, 0, .getWidth(), .getHeight(), , true); = null; // Save = new (); .compress(.CompressFormat.JPEG, 100, ); = null;
?