现在的位置: 首页 > 综合 > 正文

Android性能优化—布局优化 Android性能优化—布局优化

2017年12月11日 ⁄ 综合 ⁄ 共 39567字 ⁄ 字号 评论关闭

Android性能优化---布局优化

分类: android 200人阅读 评论(0) 收藏 举报

我们从事Android开发编写布局的时候大多数是使用XML来布局,这给我们带来了方便性,这样操作可以布局界面的代码和逻辑控制的Java代码分离出来,使程序的结构更加清晰、明了。特别的复杂的布局,但是这样操作也同样带来了另一些问题,例如屏幕的适应性,大多数Android开发人员都会遇到这个问题,还有一个就是内容问题,如果使用xml布局,Android的虚拟机首先解析xml布局,然后加载内存,如果布局越复杂,那加载的时间越慢,而用java代码布局,可以解决这些问题,不过比xml布局麻烦一点,而且必须运行才能看见结果。下面讲解一个开发过程中的一个例子:

下面是使用xml布局的代码:

[html] view
plain
copy

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout  
  3.   xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   android:layout_width="fill_parent"  
  5.   android:layout_height="fill_parent"  
  6.   android:background="@drawable/info_back">  
  7.     
  8.      <LinearLayout   
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_above="@+id/linearRecordLayout">  
  12.         <RelativeLayout   
  13.              android:layout_width="wrap_content"  
  14.              android:layout_height="fill_parent"  
  15.              android:layout_marginLeft="15dp"  
  16.              android:layout_marginRight="15dp">  
  17.                    
  18.              <!-- <LinearLayout  
  19.                 android:id="@+id/LinearLayout_left"   
  20.                 android:layout_width="wrap_content"  
  21.                 android:layout_height="wrap_content"  
  22.                 android:orientation="vertical"  
  23.                 >  -->  
  24.                 <LinearLayout   
  25.                     android:id="@+id/contronLinearLayout"  
  26.                     android:layout_width="wrap_content"  
  27.                     android:layout_height="wrap_content"  
  28.                     android:orientation="vertical"  
  29.                     android:layout_alignParentTop="true"  
  30.                     android:layout_marginLeft="20dp">  
  31.                      <RelativeLayout  
  32.                          android:layout_width="wrap_content"  
  33.                          android:layout_height="wrap_content"  
  34.                           android:orientation="horizontal"  
  35.                         >  
  36.                     <TextView   
  37.                         android:id="@+id/temp_textview"  
  38.                         android:text="T:"  
  39.                         android:textSize="18sp"  
  40.                         android:layout_width="wrap_content"  
  41.                         android:layout_height="wrap_content"  
  42.                         android:textColor="#FFFFFF"              
  43.                         />  
  44.                     <ToggleButton   
  45.                         android:id="@+id/temp_max"  
  46.                         android:layout_width="15dp"  
  47.                         android:layout_height="15dp"  
  48.                         android:textOn=" "   
  49.                         android:textOff=" "   
  50.                         android:layout_toRightOf="@+id/temp_textview"  
  51.                         android:layout_alignParentTop="true"  
  52.                         android:layout_marginTop="5dp"  
  53.                         android:background="@drawable/max"  
  54.                         android:visibility="invisible"  
  55.                         />  
  56.                     </RelativeLayout>  
  57.                     <TextView   
  58.                         android:id="@+id/humidity_textiew"  
  59.                         android:text="H:"  
  60.                         android:textSize="18sp"  
  61.                         android:layout_width="wrap_content"  
  62.                         android:layout_height="wrap_content"  
  63.                         android:textColor="#FFFFFF"  
  64.                         android:layout_marginTop="5dp"/>  
  65.                 </LinearLayout>   
  66.                 <LinearLayout   
  67.                     android:id="@+id/BtnControl"  
  68.                     android:layout_width="150dp"  
  69.                     android:layout_height="150dp"  
  70.                     android:orientation="vertical"  
  71.                     android:layout_marginTop="20dp"  
  72.                     android:layout_alignParentBottom="true"  
  73.                     android:layout_marginBottom="35dp"  
  74.                     android:gravity="center_vertical|center_horizontal"  
  75.                     android:background="@drawable/no_control">  
  76.                      <Button   
  77.                         android:id="@+id/up"  
  78.                         android:layout_width="50dp"  
  79.                         android:layout_height="60dp"  
  80.                          android:background="@drawable/kong"/>  
  81.                     <LinearLayout   
  82.                         android:layout_width="wrap_content"  
  83.                         android:layout_height="wrap_content"  
  84.                         android:orientation="horizontal">  
  85.                         <Button   
  86.                             android:id="@+id/left"  
  87.                             android:layout_width="80dp"  
  88.                             android:layout_height="50dp"  
  89.                             android:background="@drawable/kong"/>  
  90.                          <Button   
  91.                             android:id="@+id/right"  
  92.                             android:layout_width="80dp"  
  93.                             android:layout_height="50dp"  
  94.                             android:layout_marginLeft="10dp"  
  95.                              android:background="@drawable/kong"  
  96.                             />  
  97.                     </LinearLayout>  
  98.                     <Button   
  99.                         android:id="@+id/down"  
  100.                         android:layout_width="50dp"  
  101.                         android:layout_height="60dp"  
  102.                          android:background="@drawable/kong"  
  103.                         />  
  104.                 </LinearLayout>  
  105.                   
  106.           <!--  </LinearLayout>  -->  
  107.             <LinearLayout   
  108.                 android:layout_width="wrap_content"  
  109.                 android:layout_height="wrap_content"  
  110.                 android:orientation="horizontal"  
  111.                 android:layout_alignParentBottom="true"  
  112.                 android:layout_marginBottom="5dp"  
  113.                 android:layout_alignParentLeft="true"  
  114.                 android:layout_marginLeft="0dp">  
  115.                 <Button   
  116.                     android:id="@+id/about_button"  
  117.                     android:layout_width="27dp"  
  118.                     android:layout_height="27dp"  
  119.                     android:background="@drawable/info_off"  
  120.                     android:layout_marginRight="10dp"  
  121.                     />  
  122.                  <Button   
  123.                      android:id="@+id/webserver"  
  124.                      android:layout_width="27dp"  
  125.                     android:layout_height="27dp"  
  126.                     android:layout_marginRight="10dp"  
  127.                      android:background="@drawable/net_in_off"  
  128.                      />  
  129.                 <Button   
  130.                     android:id="@+id/apConvertButton"  
  131.                     android:layout_width="27dp"  
  132.                     android:layout_height="27dp"  
  133.                     android:background="@drawable/wifi_switch_off"  
  134.                      />  
  135.             </LinearLayout>  
  136.               
  137.         </RelativeLayout>  
  138.         <RelativeLayout   
  139.              android:id="@+id/camerSurfaceRelativelayout"  
  140.              android:layout_width="wrap_content"  
  141.              android:layout_height="wrap_content">  
  142.             <LinearLayout   
  143.                 android:layout_width="wrap_content"   
  144.                 android:id="@+id/controlAbsoluteLayout"   
  145.                 android:layout_height="fill_parent"   
  146.                 android:gravity="center_vertical|center_horizontal">  
  147.   
  148.                 <com.pcareroute.surface.CameraSurfaceView  
  149.                     android:id="@+id/car_camera_surfaceview"  
  150.                     android:layout_width="wrap_content"  
  151.                     android:layout_height="fill_parent" />  
  152.             </LinearLayout>  
  153.             <RelativeLayout  
  154.                 android:id="@+id/scale_control_linearLayout"  
  155.                 android:layout_width="wrap_content"  
  156.                 android:layout_height="wrap_content"  
  157.                 android:layout_alignParentTop="true"  
  158.                 android:padding="5dip" >  
  159.   
  160.                 <Button  
  161.                     android:id="@+id/zoom_in_button"  
  162.                     android:layout_width="40dp"  
  163.                     android:layout_height="40dp"  
  164.                     android:layout_marginLeft="3dp"  
  165.                      android:layout_marginTop="3dp"  
  166.                     android:background="@drawable/button_zoom_out_icon" >  
  167.                 </Button>  
  168.                     <LinearLayout   
  169.                          android:layout_width="wrap_content"  
  170.                          android:layout_height="wrap_content"  
  171.                          android:layout_alignParentRight="true"  
  172.                          android:layout_marginRight="60dp"  
  173.                          android:layout_marginTop="3dp"  
  174.                          >  
  175.                         <ToggleButton  
  176.                              android:textOn=" "   
  177.                              android:textOff=" "   
  178.                              android:id="@+id/record_red"  
  179.                              android:layout_width="18dp"  
  180.                              android:layout_height="18dp"  
  181.                              android:background="@drawable/video_off_led"  
  182.                              android:visibility="invisible"/>  
  183.                        <Chronometer  
  184.                             android:id="@+id/chronometer1"  
  185.                             android:layout_width="wrap_content"  
  186.                             android:layout_height="wrap_content"  
  187.                              android:layout_marginLeft="7dp"  
  188.                             android:text="Chronometer"   
  189.                             android:visibility="invisible"  
  190.                             />  
  191.                    </LinearLayout>  
  192.                 <Button  
  193.                     android:layout_alignParentRight="true"  
  194.                     android:id="@+id/zoom_out_button"  
  195.                     android:layout_width="40dp"  
  196.                     android:layout_height="40dp"  
  197.                     android:layout_marginRight="2dp"  
  198.                     android:layout_marginTop="3dp"  
  199.                     android:background="@drawable/button_zoom_in_icon"></Button>  
  200.           
  201.             </RelativeLayout>  
  202.             <LinearLayout  
  203.                 android:id="@+id/RelativeLayoutzoom"  
  204.                 android:layout_width="wrap_content"  
  205.                 android:layout_height="wrap_content"  
  206.                 android:gravity="center_vertical|center_horizontal"  
  207.                 android:layout_centerHorizontal="true"  
  208.                 android:layout_above="@+id/bottomEmpty"  
  209.                 >   
  210.                 <TextView  
  211.                      android:id="@+id/scale_textView"  
  212.                      android:layout_width="wrap_content"  
  213.                      android:layout_height="wrap_content"  
  214.                      android:gravity="center_horizontal"  
  215.                      android:shadowColor="#ff000000"  
  216.                      android:shadowDx="2"  
  217.                      android:shadowDy="0"  
  218.                      android:shadowRadius="2"  
  219.                      android:text="100%"  
  220.                      android:textAppearance="?android:attr/textAppearanceMedium"  
  221.                      android:textStyle="bold" />  
  222.             </LinearLayout>   
  223.             <LinearLayout   
  224.                 android:id="@+id/bottomEmpty"  
  225.                 android:layout_width="fill_parent"  
  226.                 android:layout_height="18dp"  
  227.                 android:layout_alignParentBottom="true">  
  228.             </LinearLayout>  
  229.         </RelativeLayout>  
  230.           
  231.     </LinearLayout>   
  232.         
  233.      <!-- <RelativeLayout  
  234.         android:id="@+id/scale_control_linearLayout"  
  235.         android:layout_width="fill_parent"  
  236.         android:layout_height="wrap_content"  
  237.         android:layout_alignParentTop="true"  
  238.         android:padding="5dip" >  
  239.   
  240.         <Button  
  241.             android:id="@+id/zoom_in_button"  
  242.             android:layout_width="40dp"  
  243.             android:layout_height="40dp"  
  244.             android:layout_marginLeft="25dp"  
  245.             android:background="@drawable/button_zoom_out_icon" >  
  246.         </Button>  
  247.         <Button  
  248.             android:layout_alignParentRight="true"  
  249.             android:id="@+id/zoom_out_button"  
  250.             android:layout_width="40dp"  
  251.             android:layout_height="40dp"  
  252.             android:layout_marginRight="25dp"  
  253.             android:background="@drawable/button_zoom_in_icon" >  
  254.         </Button>  
  255.         <LinearLayout   
  256.            android:id="@+id/linearlayout_temp_rh"  
  257.             android:layout_width="wrap_content"  
  258.             android:layout_height="wrap_content"  
  259.             android:layout_centerHorizontal="true"  
  260.             android:layout_above="@+id/linearRecordLayout"  
  261.             android:layout_centerVertical="true"     
  262.         >  
  263.          
  264.         <TextView   
  265.             android:id="@+id/temp_textview"  
  266.             android:text="T"  
  267.             android:layout_width="wrap_content"  
  268.             android:layout_height="wrap_content"  
  269.             android:textColor="#FFFFFF"              
  270.             android:shadowColor="#ff000000"  
  271.             android:shadowDx="2"  
  272.             android:shadowDy="0"  
  273.             android:shadowRadius="2"  
  274.             />  
  275.           
  276.         <TextView   
  277.             android:id="@+id/humidity_textiew"  
  278.             android:text="H"  
  279.             android:layout_width="wrap_content"  
  280.             android:layout_height="wrap_content"  
  281.             android:textColor="#FFFFFF"  
  282.             android:layout_marginLeft="100dp"  
  283.             android:shadowColor="#ff000000"  
  284.             android:shadowDx="2"  
  285.             android:shadowDy="0"  
  286.             android:shadowRadius="2"  
  287.             />  
  288.     </LinearLayout>  
  289.          
  290.              <LinearLayout  
  291.             android:id="@+id/RelativeLayoutpower"  
  292.             android:layout_width="wrap_content"  
  293.             android:layout_height="wrap_content"  
  294.             android:layout_above="@+id/linearbuttonLayout"  
  295.             android:layout_centerHorizontal="true"  
  296.             android:gravity="center_vertical|center_horizontal"  
  297.             android:layout_centerVertical="true"  
  298.             android:layout_toRightOf="@+id/RelativeLayoutzoom"  
  299.         >  
  300.         </LinearLayout>  
  301.   
  302.     </RelativeLayout> -->   
  303.    <!-- <LinearLayout    
  304.         android:orientation="vertical"   
  305.         android:id="@+id/linearControlLayout"   
  306.         android:visibility="visible"   
  307.         android:layout_alignParentRight="true"   
  308.         android:layout_marginRight="10dp"  
  309.         android:layout_width="50dip"   
  310.         android:layout_height="fill_parent"   
  311.         android:gravity="center_vertical|center_horizontal"  
  312.         >  
  313.          
  314.          <com.wificar.surface.DoubleAxisRightControllerSurfaceView   
  315.              android:layout_gravity="center_vertical"  
  316.             android:clickable="false"   
  317.             android:layout_height="180dp"   
  318.             android:id="@+id/stick_double_axis_right_controller_surfaceview"   
  319.             android:layout_width="wrap_content"></com.wificar.surface.DoubleAxisRightControllerSurfaceView>  
  320.           
  321.     </LinearLayout>   
  322.      <LinearLayout    
  323.           
  324.         android:id="@+id/function_linearLayout"   
  325.         android:orientation="vertical"   
  326.         android:layout_width="50dip"   
  327.         android:layout_alignParentLeft="true"   
  328.         android:layout_marginLeft="10dp"  
  329.         android:layout_height="fill_parent"   
  330.         android:gravity="center_vertical|center_horizontal">  
  331.           
  332.          <com.wificar.surface.DoubleAxisLeftControllerSurfaceView   
  333.             android:layout_gravity="center_vertical"  
  334.             android:clickable="true"   
  335.             android:layout_height="180dp"   
  336.             android:id="@+id/stick_double_axis_left_controller_surfaceview"   
  337.             android:layout_width="wrap_content"></com.wificar.surface.DoubleAxisLeftControllerSurfaceView>  
  338.        
  339.     </LinearLayout>  -->  
  340.       
  341.    <!--  <RelativeLayout   
  342.         android:id="@+id/relativelayout_temp_rh"  
  343.         android:layout_width="fill_parent"  
  344.         android:layout_height="80dp"  
  345.         android:layout_alignParentBottom="true"  
  346.          > -->  
  347.     <LinearLayout   
  348.         android:background="@drawable/button_bar"   
  349.         android:gravity="center_vertical|center_horizontal"   
  350.         android:layout_width="fill_parent"   
  351.         android:layout_height="45dp"   
  352.         android:id="@+id/linearRecordLayout"   
  353.         android:layout_alignParentBottom="true">  
  354.          <ToggleButton   
  355.                android:id="@+id/send_voice_button"   
  356.                android:layout_width="40dp"  
  357.                android:layout_height="40dp"  
  358.                android:textOn=""  
  359.                android:textOff=""  
  360.                android:background="@drawable/talk_off"  
  361.                android:layout_marginRight="23dip" />  
  362.          <Button   
  363.               android:id="@+id/spk_toggle_button"  
  364.               android:layout_width="40dp"  
  365.               android:layout_height="40dp"  
  366.               android:textOn=" "  
  367.               android:textOff=" "  
  368.               android:background="@drawable/music_off"  
  369.               android:layout_marginRight="23dp"  
  370.              />  
  371.          <ToggleButton  
  372.             android:textOn=" "   
  373.             android:textOff=" "   
  374.             android:layout_marginRight="23dp"    
  375.             android:layout_width="40dp"  
  376.             android:layout_height="40dp"  
  377.             android:background="@drawable/video_off"  
  378.             android:id="@+id/camera_shoot_button"  
  379.             />  
  380.         <ToggleButton   
  381.             android:background="@drawable/sound_mute"   
  382.             android:id="@+id/listen_toggle_button"   
  383.             android:layout_width="40dp"   
  384.             android:textOn=" "   
  385.             android:textOff=" "   
  386.             android:layout_height="40dp"   
  387.             android:layout_marginRight="23dp" ></ToggleButton>  
  388.          <Button   
  389.            android:id="@+id/take_picture_button"  
  390.            android:layout_width="40dp"  
  391.            android:layout_height="40dp"  
  392.            android:layout_marginRight="23dp"    
  393.            android:background="@drawable/button_take_photo_icon" />  
  394.         <ToggleButton   
  395.             android:background="@drawable/ir_off"   
  396.             android:id="@+id/light_toggle_button"   
  397.             android:layout_width="40dp"   
  398.             android:layout_height="40dp"  
  399.              android:layout_marginRight="23dp"   
  400.             android:textOn=" "   
  401.             android:textOff=" " ></ToggleButton>  
  402.         <Button   
  403.             android:background="@drawable/share_off"  
  404.             android:id="@+id/shared"  
  405.             android:layout_width="40dp"   
  406.             android:layout_height="40dp"  
  407.             />  
  408.     </LinearLayout>  
  409.      
  410.  <!-- </RelativeLayout> -->  
  411.       
  412. </RelativeLayout>  

直接在Eclipse里面查看的显示效果如下:


在真机上运行效果如下:


进行对比,真机是我们想要的结果,但是之前的编写的效果不对,而且对于不同的屏幕分辨率xml布局编写了三个xml布局来实现适应性。

下面使用java代码来实现布局,使用两个文件一个布局文件,另一个是布局参数文件

布局文件代码如下:

[java] view
plain
copy

  1. package com.pcareroute;  
  2.   
  3. import com.seuic.pcareroute.AppLog;  
  4. import com.seuic.pcareroute.surface.CameraSurface;  
  5. import com.seuic.pcareroute.util.PcareRouteParams;  
  6. import com.seuic.pcareroute.util.ToolsUnility;  
  7.   
  8. import android.app.Activity;  
  9. import android.os.Bundle;  
  10. import android.widget.Button;  
  11. import android.widget.LinearLayout;  
  12. import android.widget.RelativeLayout;  
  13. import android.widget.TextView;  
  14. import android.widget.ToggleButton;  
  15.   
  16. public class PcareRouteMain extends Activity{  
  17.     public static final String TAG = "PcareRouteMain";  
  18.     public PcareRouteMain instance;  
  19.     PcareRouteParams pRouteParams;  
  20.     @Override  
  21.     protected void onCreate(Bundle savedInstanceState) {  
  22.         super.onCreate(savedInstanceState);  
  23.         ToolsUnility.getToolsUnilityInstance(this).noTitleAndStaBar(this); //去掉标题栏和状态栏  
  24.         instance = this;  
  25.         pRouteParams = new PcareRouteParams(instance);  
  26.         AppLog.enableLogging(true);  
  27.         initLayout();  
  28.     }  
  29.       
  30.     //初始化布局和组件  
  31.     public void initLayout(){  
  32.         Parent = new RelativeLayout(getApplicationContext());  
  33.         Parent.setBackgroundResource(R.drawable.info_back);  
  34.         BottomInParent = new LinearLayout(getApplicationContext());  
  35.         BottomInParent.setId(_R.id.bottom_in_parent);  
  36.         BottomInParent.setBackgroundResource(R.drawable.button_bar);  
  37.         ButtonInBottom = new ToggleButton[pRouteParams.Bottom_Button_Number];  
  38.         for (int i = 0; i < pRouteParams.Bottom_Button_Number; i++) {  
  39.             ButtonInBottom[i] = new ToggleButton(getApplicationContext());  
  40.             ButtonInBottom[i].setBackgroundResource(ButtonInBottomImage[i]);  
  41.             ButtonInBottom[i].setTextOff(" ");  
  42.             ButtonInBottom[i].setTextOn(" ");  
  43.             ButtonInBottom[i].setText(" ");  
  44.             BottomInParent.addView(ButtonInBottom[i], pRouteParams.buttonInBottomParams);  
  45.         }  
  46.         Parent.addView(BottomInParent, pRouteParams.bottomInParentParams);  
  47.           
  48.         LeftInParent = new RelativeLayout(getApplicationContext());  
  49.         LeftInParent.setId(_R.id.left_in_parent);  
  50.         BottomInLeft = new LinearLayout(getApplicationContext());  
  51.         BottomInLeft.setId(_R.id.bottom_in_left);  
  52.         ButtonInLeft = new Button[pRouteParams.Left_Bottom_Button_Number];  
  53.         for (int i = 0; i < pRouteParams.Left_Bottom_Button_Number; i++) {  
  54.             ButtonInLeft[i] = new Button(getApplicationContext());  
  55.             ButtonInLeft[i].setText(" ");  
  56.             ButtonInLeft[i].setBackgroundResource(ButtonInLeftImage[i]);  
  57.             BottomInLeft.addView(ButtonInLeft[i], pRouteParams.Left_Bottom_Button_Params);  
  58.         }  
  59.         LeftInParent.addView(BottomInLeft, pRouteParams.bottomInLeftParams);  
  60.           
  61.         tempView = new TextView(getApplicationContext());  
  62.         tempView.setTextSize(pRouteParams.Left_Top_Temp_TextSize);  
  63.         tempView.setText("T:");  
  64.         tempView.setId(_R.id.tempview_in_left);  
  65.         LeftInParent.addView(tempView, pRouteParams.Left_Top_Temp_Params);  
  66.         tempRoll = new ToggleButton(getApplicationContext());  
  67.         tempRoll.setBackgroundResource(R.drawable.max);  
  68.         tempRoll.setText(" ");  
  69.         tempRoll.setTextOff(" ");  
  70.         tempRoll.setTextOn(" ");  
  71. //      tempRoll.setVisibility(View.INVISIBLE);  
  72.         LeftInParent.addView(tempRoll, pRouteParams.Left_Top_Temp_ToggButton_Params);  
  73.           
  74.         humidityView = new TextView(getApplicationContext());  
  75.         humidityView.setText("H:");  
  76.         humidityView.setId(_R.id.humiview_in_left);  
  77.         humidityView.setTextSize(pRouteParams.Left_Top_Temp_TextSize);  
  78.         LeftInParent.addView(humidityView, pRouteParams.Left_Top_Humi_Params);  
  79.           
  80.         CenterInLeft = new RelativeLayout(getApplicationContext());  
  81.         ControlBtn = new Button[CtrolBtnNumber];  
  82.         for (int i = 0; i < CtrolBtnNumber; i++) {  
  83.             ControlBtn[i] = new Button(getApplicationContext());  
  84.             ControlBtn[i].setBackgroundResource(Btn_Ctrol_In_Left_Image[i]);  
  85.             CenterInLeft.addView(ControlBtn[i], pRouteParams.Left_Center_CtrolButtons_Params[i]);  
  86.         }  
  87.         LeftInParent.addView(CenterInLeft, pRouteParams.Left_Center_CtrolButton_Params);  
  88.         Parent.addView(LeftInParent, pRouteParams.leftInParentParams);  
  89.           
  90.         RightInParent = new RelativeLayout(getApplicationContext());  
  91.         cameraSurface = new CameraSurface(getApplicationContext());  
  92.         RightInParent.addView(cameraSurface, pRouteParams.Right_Surface_Params);  
  93.         ScaleBtn = new Button[ScaleBtnNumber];  
  94.         for (int i = 0; i < ScaleBtnNumber; i++) {  
  95.             ScaleBtn[i] = new Button(getApplicationContext());  
  96.             ScaleBtn[i].setBackgroundResource(Btn_Scale_In_Right_Image[i]);  
  97.             RightInParent.addView(ScaleBtn[i], pRouteParams.Right_Scale_Button_Params[i]);  
  98.         }  
  99.         scaleView = new TextView(getApplicationContext());  
  100.         scaleView.setText("100%");  
  101.         scaleView.getPaint().setFakeBoldText(true);//加粗  
  102.         scaleView.setTextSize(20);  
  103.         RightInParent.addView(scaleView, pRouteParams.Right_Scale_View_Params);  
  104.         Parent.addView(RightInParent, pRouteParams.RightInParentParams);  
  105.           
  106.           
  107.         setContentView(Parent, pRouteParams.paramentParams);  
  108.     }  
  109.       
  110.     //声明组件和布局  
  111.     public RelativeLayout   Parent;  
  112.     public LinearLayout     BottomInParent;  
  113.     public RelativeLayout   LeftInParent;  
  114.     public LinearLayout     BottomInLeft;  
  115.     public RelativeLayout   CenterInLeft;  
  116.     public RelativeLayout   RightInParent;  
  117.     public CameraSurface    cameraSurface;  
  118.     public ToggleButton[]   ButtonInBottom;  
  119.     public Button[] ButtonInLeft;  
  120.     public Button[] ControlBtn;  
  121.     public Button[] ScaleBtn;  
  122.     public TextView tempView, humidityView;  
  123.     public TextView scaleView;  
  124.     public ToggleButton tempRoll;  
  125.     //声明变量  
  126.     public int[] ButtonInBottomImage = {R.drawable.talk_off, R.drawable.music_off, R.drawable.video_off, R.drawable.sound_mute, R.drawable.button_take_photo_icon, R.drawable.ir_off, R.drawable.share_off};//存储底部button的图片的数组  
  127.     public int[] ButtonInLeftImage = {R.drawable.info_off, R.drawable.net_in_off, R.drawable.wifi_switch_off};//左边布局的下面三个Button按钮图片  
  128.     public int[] Btn_Ctrol_In_Left_Image = {R.drawable.left_off, R.drawable.up_off, R.drawable.right_off, R.drawable.down_off};  
  129.     public int CtrolBtnNumber = 4;  
  130.     public int[] Btn_Scale_In_Right_Image = {R.drawable.zoom_out, R.drawable.zoom_in};  
  131.     public int ScaleBtnNumber = 2;  
  132. }  

在上面的代码中如果我们有多个相同的按钮,则可以使用数组来存储,这样做的好处就是代码更加清晰,而且能够减少冗余代码

[java] view
plain
copy

  1. ControlBtn = new Button[CtrolBtnNumber];  
  2.         for (int i = 0; i < CtrolBtnNumber; i++) {  
  3.             ControlBtn[i] = new Button(getApplicationContext());  
  4.             ControlBtn[i].setBackgroundResource(Btn_Ctrol_In_Left_Image[i]);  
  5.             CenterInLeft.addView(ControlBtn[i], pRouteParams.Left_Center_CtrolButtons_Params[i]);  
  6.         }  

这样的代码,如果我们用平时的一个组件一个名称,就比现在多余CtrolBtnNumber倍的代码冗余。

布局参数代码如下:

[java] view
plain
copy

  1. package com.seuic.pcareroute.util;  
  2.   
  3. import com.pcareroute._R;  
  4.   
  5. import android.app.Activity;  
  6. import android.util.DisplayMetrics;  
  7. import android.view.Gravity;  
  8. import android.widget.LinearLayout;  
  9. import android.widget.RelativeLayout;  
  10.   
  11. public class PcareRouteParams {  
  12.     public static final String TAG = "PcareRouteParams";  
  13.     Activity activity;  
  14.     public PcareRouteParams(Activity activity){  
  15.         this.activity = activity;  
  16.         getDisplayMetrics();  
  17.         initVar();  
  18.         initLayoutParams();  
  19.     }  
  20.     //初始化變量  
  21.     public void initVar(){  
  22.         if (screenSize > 5.8) {  
  23.             Bottom_Button_Width = dip2px(60);  
  24.             Left_Bottom_Button_Width = dip2px(35);  
  25.             Bottom_Back_Height = dip2px(70);  
  26.             Left_Top_TopMarge = dip2px(20);  
  27.             Left_Top_Temp_LeftMarge = dip2px(40);  
  28.             Left_Top_Temp_TextSize = 35;  
  29.             Left_Top_Temp_ToggButton_Width = dip2px(28);  
  30.             Left_Top_Temp_ToggButton_Height = dip2px(30);  
  31.             Left_Top_Temp_ToggButton_TopMarge = dip2px(20);  
  32.             Left_Top_Humi_View_TopMarge = dip2px(5);  
  33.             Left_Center_CtrolButton_Width = dip2px(300);  
  34.             Left_Center_CtrolButton_Height = dip2px(250);  
  35.             Left_Center_CtrolButton_LeftMarge = dip2px(20);  
  36.             Left_Center_CtrolButton_BottomMarge = dip2px(20);  
  37.             Right_Surface_LeftMarge = Left_Center_CtrolButton_LeftMarge;  
  38.             Left_Center_CtrolButton_LeftBtn_Width = dip2px(135);  
  39.             Left_Center_CtrolButton_LeftBtn_Height = dip2px(90);  
  40.             Left_Center_CtrolButton_UpBtn_Width = dip2px(100);  
  41.             Left_Center_CtrolButton_UpBtn_Height = dip2px(135);  
  42.             Right_Scale_Button_Width = dip2px(50);  
  43.             Right_Scale_Button_Marge = dip2px(7);  
  44.             Right_Scale_View_BottomMarge = dip2px(40);  
  45.         }else if(screenSize < 3.8){  
  46.             Bottom_Button_Width = dip2px(35);  
  47.             Left_Bottom_Button_Width = dip2px(18);  
  48.             Bottom_Back_Height = dip2px(35);  
  49.             Left_Top_TopMarge = dip2px(0);  
  50.             Left_Top_Temp_LeftMarge = dip2px(10);  
  51.             Left_Top_Temp_TextSize = 18;  
  52.             Left_Top_Temp_ToggButton_Width = dip2px(15);  
  53.             Left_Top_Temp_ToggButton_Height = dip2px(15);  
  54.             Left_Top_Temp_ToggButton_TopMarge = dip2px(5);  
  55.             Left_Top_Humi_View_TopMarge = dip2px(3);  
  56.             Left_Center_CtrolButton_Width = dip2px(120);  
  57.             Left_Center_CtrolButton_Height = dip2px(120);  
  58.             Left_Center_CtrolButton_LeftMarge = dip2px(20);  
  59.             Left_Center_CtrolButton_BottomMarge = dip2px(5);  
  60.             Right_Surface_LeftMarge = Left_Center_CtrolButton_LeftMarge;  
  61.             Left_Center_CtrolButton_LeftBtn_Width = dip2px(40);  
  62.             Left_Center_CtrolButton_LeftBtn_Height = dip2px(30);  
  63.             Left_Center_CtrolButton_UpBtn_Width = dip2px(30);  
  64.             Left_Center_CtrolButton_UpBtn_Height = dip2px(40);  
  65.             Right_Scale_Button_Width = dip2px(30);  
  66.             Right_Scale_Button_Marge = dip2px(3);  
  67.             Right_Scale_View_BottomMarge = dip2px(15);  
  68.         }else {  
  69.             Bottom_Button_Width = dip2px(40);  
  70.             Left_Bottom_Button_Width = dip2px(27);  
  71.             Bottom_Back_Height = dip2px(45);  
  72.             Left_Top_TopMarge = dip2px(0);  
  73.             Left_Top_Temp_LeftMarge = dip2px(20);  
  74.             Left_Top_Temp_TextSize = 18;  
  75.             Left_Top_Temp_ToggButton_Width = dip2px(15);  
  76.             Left_Top_Temp_ToggButton_Height = dip2px(15);  
  77.             Left_Top_Temp_ToggButton_TopMarge = dip2px(5);  
  78.             Left_Top_Humi_View_TopMarge = dip2px(5);  
  79.             Left_Center_CtrolButton_Width = dip2px(150);  
  80.             Left_Center_CtrolButton_Height = dip2px(150);  
  81.             Left_Center_CtrolButton_LeftMarge = dip2px(20);  
  82.             Left_Center_CtrolButton_BottomMarge = dip2px(10);  
  83.             Right_Surface_LeftMarge = Left_Center_CtrolButton_LeftMarge;  
  84.             Left_Center_CtrolButton_LeftBtn_Width = dip2px(80);  
  85.             Left_Center_CtrolButton_LeftBtn_Height = dip2px(50);  
  86.             Left_Center_CtrolButton_UpBtn_Width = dip2px(50);  
  87.             Left_Center_CtrolButton_UpBtn_Height = dip2px(60);  
  88.             Right_Scale_Button_Width = dip2px(40);  
  89.             Right_Scale_Button_Marge = dip2px(3);  
  90.             Right_Scale_View_BottomMarge = dip2px(18);  
  91.         }  
  92.         Left_Bottom_Button_LeftMarge = dip2px(10);  
  93.         BottomInLeft_BottomMarge = dip2px(5);  
  94.     }  
  95.     public int dip2px(float dpValue) {    
  96.         return (int)(dpValue * scale + 0.5f);  
  97.     }  
  98.     //获取屏幕的宽度,高度和密度以及dp / px  
  99.      public void getDisplayMetrics() {  
  100.         DisplayMetrics dm = new DisplayMetrics();  
  101.         dm = activity.getApplicationContext().getResources().getDisplayMetrics();  
  102.         Screen_width = dm.widthPixels;  
  103.         Screen_height = dm.heightPixels;  
  104.         scale = activity.getResources().getDisplayMetrics().density;  
  105.         density = dm.density;  
  106.         double bb = Math.sqrt(Math.pow(Screen_width, 2)+ Math.pow(Screen_height, 2));  
  107.         screenSize = bb / (160 * dm.density);  
  108.     }  
  109.            
  110.     //初始化布局参数  
  111.     public void initLayoutParams(){  
  112.         paramentParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);  
  113.         bottomInParentParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, Bottom_Back_Height);  
  114.         bottomInParentParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);  
  115.         buttonInBottomParams = new LinearLayout.LayoutParams(Bottom_Button_Width, Bottom_Button_Width);  
  116.         buttonInBottomParams.gravity = Gravity.CENTER_VERTICAL;  
  117.         //底部包含组件布局参数  
  118.         int jiange = (Screen_width - Bottom_Button_Width * Bottom_Button_Number)/(Bottom_Button_Number + 1);  
  119.         buttonInBottomParams.leftMargin = jiange;  
  120.           
  121.         leftInParentParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.MATCH_PARENT);  
  122.         leftInParentParams.addRule(RelativeLayout.ABOVE, _R.id.bottom_in_parent);  
  123.         bottomInLeftParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);  
  124.         bottomInLeftParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);  
  125.         bottomInLeftParams.bottomMargin = BottomInLeft_BottomMarge;  
  126.           
  127.         Left_Bottom_Button_Params = new LinearLayout.LayoutParams(Left_Bottom_Button_Width, Left_Bottom_Button_Width);  
  128.         Left_Bottom_Button_Params.leftMargin = Left_Bottom_Button_LeftMarge;  
  129.         Left_Bottom_Button_Params.gravity = Gravity.CENTER_VERTICAL;  
  130.           
  131.         Left_Top_Temp_Params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);  
  132.         Left_Top_Temp_Params.topMargin = Left_Top_TopMarge;  
  133.         Left_Top_Temp_Params.leftMargin = Left_Top_Temp_LeftMarge;  
  134.         Left_Top_Temp_ToggButton_Params = new RelativeLayout.LayoutParams(Left_Top_Temp_ToggButton_Width, Left_Top_Temp_ToggButton_Height);  
  135.         Left_Top_Temp_ToggButton_Params.addRule(RelativeLayout.RIGHT_OF, _R.id.tempview_in_left);  
  136.         Left_Top_Temp_ToggButton_Params.topMargin = Left_Top_Temp_ToggButton_TopMarge;  
  137.           
  138.         Left_Top_Humi_Params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);  
  139.         Left_Top_Humi_Params.addRule(RelativeLayout.BELOW, _R.id.tempview_in_left);  
  140.         Left_Top_Humi_Params.topMargin = Left_Top_Humi_View_TopMarge;  
  141.         Left_Top_Humi_Params.addRule(RelativeLayout.ALIGN_LEFT, _R.id.tempview_in_left);  
  142.           
  143.         Left_Center_CtrolButton_Params = new RelativeLayout.LayoutParams(Left_Center_CtrolButton_Width, Left_Center_CtrolButton_Height);  
  144.         Left_Center_CtrolButton_Params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);  
  145.         Left_Center_CtrolButton_Params.bottomMargin = BottomInLeft_BottomMarge + Left_Center_CtrolButton_BottomMarge + Left_Bottom_Button_Width;  
  146.         Left_Center_CtrolButton_Params.leftMargin = Left_Center_CtrolButton_LeftMarge;  
  147. //      Left_Center_CtrolButton_Params.rightMargin = Left_Center_CtrolButton_LeftMarge;  
  148.         Left_Center_CtrolButtons_Params = new RelativeLayout.LayoutParams[4];  
  149.         Left_Center_CtrolButtons_Params[0] = new RelativeLayout.LayoutParams(Left_Center_CtrolButton_UpBtn_Height, Left_Center_CtrolButton_UpBtn_Width);  
  150.         Left_Center_CtrolButtons_Params[0].addRule(RelativeLayout.CENTER_VERTICAL);  
  151.         Left_Center_CtrolButtons_Params[1] = new RelativeLayout.LayoutParams(Left_Center_CtrolButton_UpBtn_Width, Left_Center_CtrolButton_UpBtn_Height);  
  152.         Left_Center_CtrolButtons_Params[1].addRule(RelativeLayout.CENTER_HORIZONTAL);  
  153.         Left_Center_CtrolButtons_Params[2] = new RelativeLayout.LayoutParams(Left_Center_CtrolButton_UpBtn_Height, Left_Center_CtrolButton_UpBtn_Width);  
  154.         Left_Center_CtrolButtons_Params[2].addRule(RelativeLayout.ALIGN_PARENT_RIGHT);  
  155.         Left_Center_CtrolButtons_Params[2].addRule(RelativeLayout.CENTER_VERTICAL);  
  156.         Left_Center_CtrolButtons_Params[3] = new RelativeLayout.LayoutParams(Left_Center_CtrolButton_UpBtn_Width, Left_Center_CtrolButton_UpBtn_Height);  
  157.         Left_Center_CtrolButtons_Params[3].addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);  
  158.         Left_Center_CtrolButtons_Params[3].addRule(RelativeLayout.CENTER_HORIZONTAL);  
  159.           
  160.         RightInParentParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);  
  161.         RightInParentParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);  
  162.         RightInParentParams.addRule(RelativeLayout.RIGHT_OF, _R.id.left_in_parent);  
  163.         RightInParentParams.addRule(RelativeLayout.ABOVE, _R.id.bottom_in_parent);  
  164.         RightInParentParams.leftMargin = Right_Surface_LeftMarge;  
  165.         Right_Surface_Params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);  
  166.           
  167.         Right_Scale_Button_Params = new RelativeLayout.LayoutParams[2];  
  168.         Right_Scale_Button_Params[0] = new RelativeLayout.LayoutParams(Right_Scale_Button_Width, Right_Scale_Button_Width);  
  169.         Right_Scale_Button_Params[0].leftMargin = Right_Scale_Button_Marge;  
  170.         Right_Scale_Button_Params[0].topMargin = Right_Scale_Button_Marge;  
  171.         Right_Scale_Button_Params[1] = new RelativeLayout.LayoutParams(Right_Scale_Button_Width, Right_Scale_Button_Width);  
  172.         Right_Scale_Button_Params[1].addRule(RelativeLayout.ALIGN_PARENT_RIGHT);  
  173.         Right_Scale_Button_Params[1].rightMargin = Right_Scale_Button_Marge;  
  174.         Right_Scale_Button_Params[1].topMargin = Right_Scale_Button_Marge;  
  175.         Right_Scale_View_Params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);  
  176.         Right_Scale_View_Params.addRule(RelativeLayout.CENTER_HORIZONTAL);  
  177.         Right_Scale_View_Params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);  
  178.         Right_Scale_View_Params.bottomMargin = Right_Scale_View_BottomMarge;  
  179.     }  
  180.       
  181.       
  182.     //声明布局和组件  
  183.     public RelativeLayout.LayoutParams paramentParams;  
  184.     public RelativeLayout.LayoutParams bottomInParentParams;  
  185.     public LinearLayout.LayoutParams    buttonInBottomParams;  
  186.     public RelativeLayout.LayoutParams  leftInParentParams;  
  187.     public RelativeLayout.LayoutParams  bottomInLeftParams;  
  188.     public LinearLayout.LayoutParams    Left_Bottom_Button_Params;  
  189.     public RelativeLayout.LayoutParams  topInLeftParams;  
  190.     public RelativeLayout.LayoutParams  Left_Top_Temp_Params;  
  191.     public RelativeLayout.LayoutParams  Left_Top_Temp_ToggButton_Params;  
  192.     public RelativeLayout.LayoutParams  Left_Top_Humi_Params;  
  193.     public RelativeLayout.LayoutParams  Left_Center_CtrolButton_Params;  
  194.     public RelativeLayout.LayoutParams[] Left_Center_CtrolButtons_Params;//左上右下  
  195.     public RelativeLayout.LayoutParams  RightInParentParams;  
  196.     public RelativeLayout.LayoutParams  Right_Surface_Params;  
  197.     public RelativeLayout.LayoutParams[] Right_Scale_Button_Params;//大小  
  198.     public RelativeLayout.LayoutParams  Right_Scale_View_Params;  
  199.       
  200.     //声明变量  
  201.     public float scale;//dp -- px  
  202.     public double screenSize;  
  203.     public float density;  
  204.     public int Screen_width;  
  205.     public int Screen_height;  
  206.     public int Bottom_Button_Width = 40;//dp  
  207.     public int Bottom_Back_Height = 5;//dp  
  208.     public int Bottom_Button_Number = 7;  
  209.     public int Left_Bottom_Button_Width = 27;//dp  
  210.     public int BottomInLeft_BottomMarge = 5;//5dp  
  211.     public int Left_Bottom_Button_LeftMarge = 10;//dp  
  212.     public int Left_Bottom_Button_Number = 3;  
  213.     public int Left_Top_TopMarge = 5;  
  214.     public int Left_Top_Temp_LeftMarge = 5;  
  215.     public int Left_Top_Temp_TextSize = 18;  
  216.     public int Left_Top_Temp_ToggButton_Width = 15;  
  217.     public int Left_Top_Temp_ToggButton_Height = 15;  
  218.     public int Left_Top_Temp_ToggButton_TopMarge = 5;  
  219.     public int Left_Top_Humi_View_TopMarge = 5;  
  220.     public int Left_Center_CtrolButton_Width = 150;  
  221.     public int Left_Center_CtrolButton_Height = 150;  
  222.     public int Left_Center_CtrolButton_LeftMarge = 20;  
  223.     public int Left_Center_CtrolButton_BottomMarge = 20;  
  224.     public int Right_Surface_LeftMarge = 5;  
  225.     public int Left_Center_CtrolButton_LeftBtn_Width = 80;  
  226.     public int Left_Center_CtrolButton_LeftBtn_Height = 50;  
  227.     public int Left_Center_CtrolButton_UpBtn_Width = 50;  
  228.     public int Left_Center_CtrolButton_UpBtn_Height = 60;  
  229.     public int Right_Scale_Button_Width = 20;  
  230.     public int Right_Scale_Button_Marge = 3;  
  231.     public int Right_Scale_View_BottomMarge = 18;  
  232. }  

真机的显示效果如下:


两者虽然在真机上的效果差不多,但是实际是两种不同的实现方式。

抱歉!评论已关闭.