android之简单手电筒

有时间弄个小电筒来学习,该小项目比较简单,但个人觉得该小项目可以学到很多知识,希望对大家用,如果有什么好的android教程,请分享谢谢

java源码:

 

package color.test;import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.Toast;public class ColorTestActivity extends Activity {private LinearLayout mylayout;//布局对象private Resources myColor;//资源对对象int li;public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);//设置界面为全屏SetStatusWindows();setContentView(R.layout.main);//获取实例mylayout = (LinearLayout)findViewById(R.id.mylayout);SetColor(R.color.white);//改变屏幕亮度li=0;SetBright(1.0f);}/*** 捕抓事件*/public boolean onOptionsItemSelected(MenuItem item) {switch(item.getItemId()){case R.id.about:about();return true;case R.id.setcolor:selectColor();return true;case R.id.setbright:selectBright();return true;case R.id.seteffer:finish();return true;}return super.onOptionsItemSelected(item);}/*** 设置亮度*/private void selectBright() {final String[] items ={"100%","75%","50%","25%","10%"};new AlertDialog.Builder(this).setTitle("选择亮度").setSingleChoiceItems(items, li, new DialogInterface.OnClickListener() {///此处数字为选项的下标,从0开始, 表示默认哪项被选中 public void onClick(DialogInterface dialog, int item) {Toast.makeText(getApplicationContext(),items[item],Toast.LENGTH_LONG).show();li = item;switch(li){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);default:SetBright(1.0F);break;					}		dialog.cancel();}}).show();}/*** 设置亮度* @param light*/private void SetBright(float light){WindowManager.LayoutParams lp=getWindow().getAttributes();lp.screenBrightness=light;getWindow().setAttributes(lp);}/*** 设置颜色*/private void selectColor() {final String[] items ={"白色","红色","黑色","黄色","粉色"};new AlertDialog.Builder(this).setTitle("选择背景颜色").setItems(items, new DialogInterface.OnClickListener() {/*** getApplicationContext() 得到的也是当前的Activity对象* 可用当前Activity对象的名字.this代替(Activity.this) */public void onClick(DialogInterface dialog, int item) {Toast.makeText(getApplicationContext(),items[item],Toast.LENGTH_LONG).show();switch(item){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.fs);}}}).show();}/*** 关于*/private void about() {new AlertDialog.Builder(ColorTestActivity.this).setTitle("关于人生").setMessage("人生需要奋斗").setIcon(R.drawable.ic_launcher).setPositiveButton("确定",new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int whichButton) {}}).setNegativeButton("返回",new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog,int which) {}}).show();}/*** 监听屏幕点击事件显示菜单*/public boolean onTouchEvent(MotionEvent event) {openOptionsMenu();return false;}/*** 关联菜单*/public boolean onCreateOptionsMenu(Menu menu) {super.onCreateOptionsMenu(menu);getMenuInflater().inflate(R.menu.menu, menu);return true;}/*** 设置背景颜色* @param white*/private void SetColor(int white) {//根据上下文获取对象myColor = getBaseContext().getResources();//获取颜色对象Drawable colorview = myColor.getDrawable(white);//显示mylayout.setBackgroundDrawable(colorview);		}/*** 全屏设置*/private void SetStatusWindows() {//设置没有标题requestWindowFeature(Window.FEATURE_NO_TITLE);//设置充满int flag = WindowManager.LayoutParams.FLAG_FULLSCREEN;Window myWindow = this.getWindow();myWindow.setFlags(flag, flag);}}

布局文件

main.xml

string.xml


多功能手电筒多功能手电筒更换颜色更换亮度退出软件关于


 

menu.xml






 

color.xml


#FFFFFF#FFD700#FF0000#FF34B3#000000


结果:


 


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部