android---手电筒之警告灯
警告灯的实现是通过线程操作两个图片来回切换实现的,原理很简单,但是小细节比较多,下面叙述下
class changewarnlight extends Thread{
@Override
public void run() {
warnlightstates = true;
while(warnlightstates){
try {
Thread.sleep(100);
handler.sendEmptyMessage(0);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
下面是handler
/*
* 因为线程只能访问当前线程所在的java文件里面的资源,所以使用handler来传递继承下来的资源
*/
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
if(warnlightstyle){
warnlightoff.setImageResource(R.drawable.warning_light_on);
warnlighton.setImageResource(R.drawable.warning_light_off);
warnlightstyle = false;
}else {
warnlightoff.setImageResource(R.drawable.warning_light_off);
warnlighton.setImageResource(R.drawable.warning_light_on);
warnlightstyle = true;
}
}
};
下面是对屏幕亮度的控制
/*
* 设置屏幕亮度
*/
protected void setscreenlight(float value) {
WindowManager.LayoutParams layout = getWindow().getAttributes();
layout.screenBrightness = value;
getWindow().setAttributes(layout);
}
/*
* 恢复屏幕亮度
*/
protected int defaultscreenlight() {
int value = 0;
try {
value = android.provider.Settings.System.getInt(
getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS);
} catch (Exception e) {
}
return value;
}
下面是页面布局的实现
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearlayout_warnlight"
android:layout_width="match_parent"
android:layout_marginTop="40dp"
android:layout_height="match_parent"
android:visibility="gone"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView_warning_light_on"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:src="@drawable/warning_light_on" />
<ImageView
android:id="@+id/imageView_warning_light_off"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/warning_light_off" />
</LinearLayout>
版权声明:本文为博主原创文章,未经博主允许不得转载。