首页 > 其他分享 >Android Brightness Changing

Android Brightness Changing

时间:2023-01-28 18:31:17浏览次数:47  
标签:layout Brightness SeekBar Settings System import Changing Android android


这内容挺用的,特别是需要调节亮度的程序,自己收藏起来,呵呵。这段代码不只能改变当前的Activity,也可以改变整个System的亮度

代码:

package com.jimmy;
import android.app.Activity;
import android.os.Bundle;
import android.provider.Settings;
import android.view.Window;
import android.view.WindowManager;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;public class MyActivity extends Activity {
/** Called when the activity is first created. */
TextView textView; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView = (TextView) findViewById(R.id.MyTextView);
updateToggles();
}

     

private void updateToggles() {
// TODO Auto-generated method stub
SeekBar seekBar = (SeekBar) findViewById(R.id.MySeekBar);
seekBar.setProgress((int) (android.provider.Settings.System.getInt(
getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS, 255) ));
seekBar.setOnSeekBarChangeListener(seekListener);
} private OnSeekBarChangeListener seekListener = new OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
if (fromUser) {
Integer tmpInt = seekBar.getProgress();
System.out.println(tmpInt);
// 51 (seek scale) * 5 = 255 (max brightness)
// Old way
android.provider.Settings.System.putInt(getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS,
tmpInt); // 0-255
tmpInt = Settings.System.getInt(getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS, -1);
// Cupcake way..... sucks
WindowManager.LayoutParams lp = getWindow().getAttributes();
// lp.screenBrightness = 1.0f;
// Float tmpFloat = (float)tmpInt / 255;
if (0<= tmpInt && tmpInt <= 255) {
lp.screenBrightness = tmpInt;
}


getWindow().setAttributes(lp);
} }
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
// put awesomeness here
} @Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
// and here too
}
};

这是主程序

下面是权限

<xml>

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical" android:layout_width="fill_parent"

android:layout_height="fill_parent" android:background="@color/white">

<TextView android:id="@+id/MyTextView"

android:layout_width="fill_parent" android:layout_height="wrap_content"

android:text="亮度是:" />



<SeekBar

android:layout_gravity="center_horizontal"

android:id="@+id/MySeekBar"

android:paddingLeft="5.0dip"

android:paddingRight="5.0dip"

android:layout_width="fill_parent"

android:layout_height="150dip"

android:layout_marginTop="10.0dip"

android:layout_marginBottom="10.0dip"

android:max="255"

>

</SeekBar>

</LinearLayout>


<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.jimmy" android:versionCode="1" android:versionName="1.0">

<application android:icon="@drawable/icon" android:label="@string/app_name">

<activity android:name=".MyActivity" android:label="@string/app_name">

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>


</application>

<uses-sdk android:minSdkVersion="5" />

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>

<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>

<uses-permission android:name="android.permission.BATTERY_STATS"></uses-permission>

<uses-permission android:name="android.permission.DEVICE_POWER"></uses-permission>

<uses-permission android:name="android.permission.SET_DEBUG_APP"></uses-permission>



<uses-permission android:name="android.permission.CHANGE_CONFIGURATION"></uses-permission>

<uses-permission android:name="android.permission.WRITE_SETTINGS"></uses-permission>



<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>


</manifest>

标签:layout,Brightness,SeekBar,Settings,System,import,Changing,Android,android
From: https://blog.51cto.com/tody/6025089

相关文章