Android中调节Activity变亮变暗(屏幕亮度)

import android.os.Bundle;
import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;


public class MainActivity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}


public void click(View view){
switch (view.getId()) {
case R.id.button1:
dimBackground(1.0f,0.0f);
break;
case R.id.button2:
dimBackground(0.5f,1.0f);
break;
default:
break;
}
}
@SuppressLint("NewApi")
private void dimBackground(final float from, final float to) {
    final Window window = getWindow();
    ValueAnimator valueAnimator = ValueAnimator.ofFloat(from, to);
    valueAnimator.setDuration(500);
    valueAnimator.addUpdateListener(new AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            WindowManager.LayoutParams params = window.getAttributes();
            params.alpha = (Float) animation.getAnimatedValue();
            window.setAttributes(params);
        }
    });


    valueAnimator.start();
}
}


你可能感兴趣的:(Android中调节Activity变亮变暗(屏幕亮度))