手電筒對於Android來說是極其常見的一個應用,常見的是通過攝像頭發出光線進行照明,這裡我們沒有采取那樣做而是通過采取通過界面Layout的一些方法來設置可擁有顯示不同顏色。其整體的思路不是太難,首先我們應該在values建立一個color.xml文件來存儲不同的顏色信息:
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <color name="white">#FFFFFF</color>
- <color name="yellow">#FFD700</color>
- <color name="red">#FF0000</color>
- <color name="pink">#FF34B3</color>
- <color name="black">#000000</color>
- <color name="lightSkyBlue">#87CEFA</color>
- </resources>
然後以下是我們的源碼文件:
- public class MainActivity extends Activity {
- /** Called when the activity is first created. */
-
-
- private LinearLayout mylayout;
- private Resources mycolor;
-
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- //改背layout背景顏色
- mylayout=(LinearLayout)findViewById(R.id.myline);
- //手電筒默認為白色的光
- setColor(R.color.white);
- //這裡默認最大亮度
- setBright(1.0f);
-
-
- }
- /*
- * 選擇設置背景顏色
- * (non-Javadoc)
- * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
- */
- @Override
- public boolean onCreateOptionsMenu(Menu menu)
- {
- // TODO Auto-generated method stub
- menu.addSubMenu(0, Menu.FIRST,1, "選擇背景顏色");
- menu.addSubMenu(0, Menu.FIRST+1,2,"調節背景亮度");
- menu.addSubMenu(0,Menu.FIRST+2,3, "關於");
- menu.addSubMenu(0,Menu.FIRST+3,4,"退出");
- return super.onCreateOptionsMenu(menu);
- }
- /*
- * 選擇處理
- * (non-Javadoc)
- * @see android.app.Activity#onMenuItemSelected(int, android.view.MenuItem)
- */
- @Override
- public boolean onMenuItemSelected(int featureId, MenuItem item)
- {
- // TODO Auto-generated method stub
- switch (item.getItemId())
- {
- case Menu.FIRST:
- selectColor();
- break;
- case Menu.FIRST+1:
- selectBright();
- break;
- case Menu.FIRST+2:
- about();
- break;
- case Menu.FIRST+3:
- MainActivity.this.finish();
- break;
- default:
- break;
- }
- return super.onMenuItemSelected(featureId, item);
- }
- /*
- * 選擇背景顏色進行設置
- */
- private void selectColor()
- {
- // 顯示並且設置一些參數
- final String[] items = {"白色", "紅色", "黑色","黃色","粉色","亮藍色"};
- new AlertDialog.Builder(MainActivity.this).setTitle("請選擇顏色").setItems(items,
- new DialogInterface.OnClickListener(){
-
- @Override
- public void onClick(DialogInterface dialog, int which)
- {
- // TODO Auto-generated method stub
- switch (which)
- {
- case 0:
- setColor(R.color.white);
- break;
- case 1:
- setColor(R.color.red);
- break;
- case 2:
- setColor(R.color.black);
- break;
- case 3:
- setColor(R.color.yellow);
- break;
- case 4:
- setColor(R.color.pink);
- break;
- case 5:
- setColor(R.color.lightSkyBlue);
- break;
- default:
- break;
- }
-
- }
-
- }).create().show();
-
-
- }
- /*
- * 設置亮度
- */
- private void selectBright()
- {
- final String[] items = {"100%", "75%", "50%","25%","10%"};
- new AlertDialog.Builder(MainActivity.this).setTitle("請選擇亮度").setItems(items,
- new DialogInterface.OnClickListener(){
-
- @Override
- public void onClick(DialogInterface dialog, int which)
- {
- // TODO Auto-generated method stub
- switch (which)
- {
- case 0:
- setBright(1.0f);
- break;
- case 1:
- setBright(0.75f);
- break;
- case 2:
- setBright(0.5f);
- break;
- case 3:
- setBright(0.25f);
- break;
- case 4:
- setBright(0.1f);
- break;
-
- default:
- break;
- }
-
- }
-
- }).create().show();
-
- }
- private void about()
- {
- new AlertDialog.Builder(MainActivity.this).setTitle("關於我們!!")
- .setMessage("ZY只為你做更實用的軟件!\n郵箱:[email protected]\n聯系我們!!")
- .setPositiveButton("確定", new DialogInterface.OnClickListener()
- {
-
- @Override
- public void onClick(DialogInterface dialog, int which)
- {
- // TODO Auto-generated method stub
-
- }
- }).create().show();
-
- }
- /*
- * 設置背景顏色
- */
- private void setColor(int color)
- {
- //得到資源的一個對象
- mycolor=getBaseContext().getResources();
- Drawable usecolor=mycolor.getDrawable(color);
- //設置手電筒光的背景顏色
- mylayout.setBackgroundDrawable(usecolor);
-
- }
- /*
- * 設置亮度
- */
- private void setBright(float light)
- {
- WindowManager.LayoutParams lp=getWindow().getAttributes();
- lp.screenBrightness=light;
- getWindow().setAttributes(lp);
- }
-
- }
布局文件:
- <LinearLayout xmlns:android="<A href="http://schemas.android.com/apk/res/android">http://schemas.android.com/apk/res/android</A>"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:id="@+id/myline"
- >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello"
- android:textSize="30sp"
- android:textColor="@color/white"
- />
- </LinearLayout>
整體來說,代碼並不是太難,很容易理解,Menu和其中設置等一些操作需要我們注意!