Android 屏幕七彩闪动

来源:互联网 发布:js怎么判断分页 编辑:程序博客网 时间:2024/04/28 03:14

这是我第一次发博客,希望大家多多支持。

昨天一个朋友说,能不能用手机屏幕做手电筒,我就说把屏幕弄成白颜色,亮度调到最大就OK了。最后我在做的时候,感觉没什么技术含量,就稍微的扩展一下。让屏幕不听的闪动,实现了霓虹灯的功能。

不说了,直接上代码。

首先,在MainActivity中,加载自己创建的screen界面,并且将屏幕的亮度调大1f;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;


public class MainActivity extends Activity {

private ScreenView screenView = null;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//去掉标题栏
requestWindowFeature(Window.FEATURE_NO_TITLE);
//去掉状态栏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
screenView = new ScreenView(this);
setContentView(screenView);
//设置屏幕亮度   屏幕亮度为0~1f
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = 1.0f;
getWindow().setAttributes(lp);
}

}


然后创建自己的View,继承View类并且实现Runnable接口


import android.annotation.SuppressLint;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.View;

public class ScreenView extends View implements Runnable {

private int index = 0;

//得到彩虹色数组
int[] colors = new int[] { Color.rgb(255, 0, 0), Color.rgb(255, 165, 0),
Color.rgb(255, 255, 0), Color.rgb(0, 255, 00),
Color.rgb(0, 127, 255), Color.rgb(0, 0, 255),
Color.rgb(139, 0, 255) };

public ScreenView(MainActivity mainActivity) {
super(mainActivity);
//启动线程
new Thread(this).start();
}

protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//绘制界面
canvas.drawColor(colors[index]);
}


public void run() {
while (true) {
postInvalidate();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
index++;
if (index == 7) {
index = 0;
}
}
}
}

同时在XMl文件中部署自己的界面


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    
    <com.app
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
     />


</RelativeLayout>




0 0
原创粉丝点击