手电筒对于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="http://schemas.android.com/apk/res/android" 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和其中设置等一些操作需要我们注意!