Android简单手电筒的应用可以设置不同的颜色


手电筒对于Android来说是极其常见的一个应用,常见的是通过摄像头发出光线进行照明,这里我们没有采取那样做而是通过采取通过界面Layout的一些方法来设置可拥有显示不同颜色。其整体的思路不是太难,首先我们应该在values建立一个color.xml文件来存储不同的颜色信息:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3. <color name="white">#FFFFFF</color>  
  4. <color name="yellow">#FFD700</color>  
  5. <color name="red">#FF0000</color>  
  6. <color name="pink">#FF34B3</color>  
  7. <color name="black">#000000</color>  
  8. <color name="lightSkyBlue">#87CEFA</color>  
  9. </resources>  

然后以下是我们的源码文件:

  1. public class MainActivity extends Activity {  
  2.     /** Called when the activity is first created. */  
  3.   
  4.       
  5.     private LinearLayout mylayout;  
  6.     private Resources  mycolor;  
  7.   
  8.     public void onCreate(Bundle savedInstanceState) {  
  9.         super.onCreate(savedInstanceState);  
  10.         setContentView(R.layout.main);  
  11.         //改背layout背景颜色   
  12.         mylayout=(LinearLayout)findViewById(R.id.myline);  
  13.         //手电筒默认为白色的光       
  14.         setColor(R.color.white);  
  15.         //这里默认最大亮度   
  16.         setBright(1.0f);  
  17.           
  18.   
  19.     }  
  20.     /* 
  21.      * 选择设置背景颜色 
  22.      * (non-Javadoc) 
  23.      * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu) 
  24.      */  
  25.         @Override  
  26.     public boolean onCreateOptionsMenu(Menu menu)  
  27.     {  
  28.         // TODO Auto-generated method stub   
  29.         menu.addSubMenu(0, Menu.FIRST,1"选择背景颜色");  
  30.         menu.addSubMenu(0, Menu.FIRST+1,2,"调节背景亮度");  
  31.         menu.addSubMenu(0,Menu.FIRST+2,3"关于");  
  32.         menu.addSubMenu(0,Menu.FIRST+3,4,"退出");  
  33.         return super.onCreateOptionsMenu(menu);  
  34.     }  
  35.     /* 
  36.      * 选择处理 
  37.      * (non-Javadoc) 
  38.      * @see android.app.Activity#onMenuItemSelected(int, android.view.MenuItem) 
  39.      */  
  40.     @Override  
  41.     public boolean onMenuItemSelected(int featureId, MenuItem item)  
  42.     {  
  43.         // TODO Auto-generated method stub   
  44.         switch (item.getItemId())  
  45.         {  
  46.         case Menu.FIRST:  
  47.             selectColor();  
  48.             break;  
  49.         case Menu.FIRST+1:  
  50.             selectBright();  
  51.             break;  
  52.         case Menu.FIRST+2:  
  53.             about();  
  54.         break;  
  55.         case Menu.FIRST+3:  
  56.             MainActivity.this.finish();  
  57.         break;  
  58.         default:  
  59.             break;  
  60.         }  
  61.         return super.onMenuItemSelected(featureId, item);  
  62.     }  
  63.     /* 
  64.      * 选择背景颜色进行设置 
  65.      */  
  66.     private void selectColor()  
  67.     {  
  68.         // 显示并且设置一些参数   
  69.         final String[] items = {"白色""红色""黑色","黄色","粉色","亮蓝色"};  
  70.         new AlertDialog.Builder(MainActivity.this).setTitle("请选择颜色").setItems(items,  
  71.                 new DialogInterface.OnClickListener(){  
  72.   
  73.                     @Override  
  74.                     public void onClick(DialogInterface dialog, int which)  
  75.                     {  
  76.                         // TODO Auto-generated method stub   
  77.                         switch (which)  
  78.                         {  
  79.                         case 0:  
  80.                             setColor(R.color.white);                      
  81.                             break;  
  82.                         case 1:  
  83.                             setColor(R.color.red);  
  84.                             break;  
  85.                         case 2:  
  86.                             setColor(R.color.black);  
  87.                             break;  
  88.                         case 3:  
  89.                             setColor(R.color.yellow);  
  90.                             break;  
  91.                         case 4:  
  92.                             setColor(R.color.pink);  
  93.                             break;  
  94.                         case 5:  
  95.                             setColor(R.color.lightSkyBlue);  
  96.                             break;  
  97.                         default:  
  98.                             break;  
  99.                         }  
  100.                           
  101.                     }  
  102.               
  103.         }).create().show();  
  104.           
  105.           
  106.     }  
  107.     /* 
  108.      * 设置亮度 
  109.      */  
  110.     private void selectBright()  
  111.     {  
  112.         final String[] items = {"100%""75%""50%","25%","10%"};   
  113.         new AlertDialog.Builder(MainActivity.this).setTitle("请选择亮度").setItems(items,  
  114.                 new DialogInterface.OnClickListener(){  
  115.   
  116.                     @Override  
  117.                     public void onClick(DialogInterface dialog, int which)  
  118.                     {  
  119.                         // TODO Auto-generated method stub   
  120.                         switch (which)  
  121.                         {  
  122.                         case 0:  
  123.                             setBright(1.0f);                      
  124.                             break;  
  125.                         case 1:  
  126.                             setBright(0.75f);  
  127.                             break;  
  128.                         case 2:  
  129.                             setBright(0.5f);  
  130.                             break;  
  131.                         case 3:  
  132.                             setBright(0.25f);  
  133.                             break;  
  134.                         case 4:  
  135.                             setBright(0.1f);  
  136.                             break;  
  137.   
  138.                         default:  
  139.                             break;  
  140.                         }  
  141.                           
  142.                     }  
  143.               
  144.         }).create().show();  
  145.   
  146.     }  
  147.     private void about()  
  148.     {  
  149.         new AlertDialog.Builder(MainActivity.this).setTitle("关于我们!!")  
  150.         .setMessage("ZY只为你做更实用的软件!\n邮箱:xxxx@163.com\n联系我们!!")  
  151.         .setPositiveButton("确定"new DialogInterface.OnClickListener()  
  152.         {  
  153.               
  154.             @Override  
  155.             public void onClick(DialogInterface dialog, int which)  
  156.             {  
  157.                 // TODO Auto-generated method stub   
  158.                   
  159.             }  
  160.         }).create().show();  
  161.           
  162.     }  
  163.     /* 
  164.      * 设置背景颜色 
  165.      */  
  166.     private void setColor(int color)  
  167.     {  
  168.         //得到资源的一个对象   
  169.         mycolor=getBaseContext().getResources();  
  170.         Drawable usecolor=mycolor.getDrawable(color);  
  171.         //设置手电筒光的背景颜色   
  172.         mylayout.setBackgroundDrawable(usecolor);  
  173.           
  174.     }  
  175.     /* 
  176.      * 设置亮度 
  177.      */  
  178.     private void setBright(float light)  
  179.     {  
  180.         WindowManager.LayoutParams lp=getWindow().getAttributes();  
  181.         lp.screenBrightness=light;  
  182.         getWindow().setAttributes(lp);  
  183.     }  
  184.       
  185. }  

布局文件:

  1. <LinearLayout xmlns:android="<A href="http://schemas.android.com/apk/res/android">http://schemas.android.com/apk/res/android</A>"   
  2.     android:orientation="vertical"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:id="@+id/myline"  
  6.     >  
  7. <TextView    
  8.     android:layout_width="fill_parent"   
  9.     android:layout_height="wrap_content"   
  10.     android:text="@string/hello"  
  11.     android:textSize="30sp"  
  12.     android:textColor="@color/white"  
  13.     />  
  14. </LinearLayout>  

整体来说,代码并不是太难,很容易理解,Menu和其中设置等一些操作需要我们注意!

相关内容