android 動態改變屏幕亮度和聲音大小方式

package com.jit.video;

import android.app.Activity;
import android.content.ContentResolver;
import android.provider.Settings;
import android.view.WindowManager;
import android.widget.Toast;
import android.provider.Settings.System;

public class LightnessController {

    // 判斷是否開啓了自動亮度調節
    public static boolean isAutoBrightness(Activity act) {
        boolean automicBrightness = false;
        ContentResolver aContentResolver = act.getContentResolver();
        try {
            automicBrightness = Settings.System.getInt(aContentResolver,
                    Settings.System.SCREEN_BRIGHTNESS_MODE) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
        } catch (Exception e) {
            Toast.makeText(act, "無法獲取亮度", Toast.LENGTH_SHORT).show();
        }
        return automicBrightness;
    }

    // 改變亮度
    public static void setLightness(Activity act, int value) {
        try {
            System.putInt(act.getContentResolver(), System.SCREEN_BRIGHTNESS, value);
            WindowManager.LayoutParams lp = act.getWindow().getAttributes();
            lp.screenBrightness = (value <= 0 ? 1 : value) / 255f;
            act.getWindow().setAttributes(lp);
        } catch (Exception e) {
            Toast.makeText(act, "無法改變亮度", Toast.LENGTH_SHORT).show();
        }
    }

    // 獲取亮度
    public static int getLightness(Activity act) {
        return System.getInt(act.getContentResolver(), System.SCREEN_BRIGHTNESS, -1);
    }

    // 停止自動亮度調節
    public static void stopAutoBrightness(Activity activity) {
        Settings.System.putInt(activity.getContentResolver(),
                Settings.System.SCREEN_BRIGHTNESS_MODE,
                Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
    }

    // 開啓亮度自動調節
    public static void startAutoBrightness(Activity activity) {
        Settings.System.putInt(activity.getContentResolver(),
                Settings.System.SCREEN_BRIGHTNESS_MODE,
                Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
    }
}
// 音頻管理器
    private AudioManager mAudioManager;

mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

//參數爲手勢滑動距離值,這裏只是一個參數,不用糾結
private void volumeDown(float delatY) {
        int max = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        int current = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        int down = (int) (delatY / height * max * 3);
        int volume = Math.max(current - down, 0);
        mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, volume, 0);
        int transformatVolume = volume * 100 / max;
        volumnController.show(transformatVolume);
    }

    private void volumeUp(float delatY) {
        int max = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        int current = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        int up = (int) ((delatY / height) * max * 3);
        int volume = Math.min(current + up, max);
        mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, volume, 0);
        int transformatVolume = volume * 100 / max;
        volumnController.show(transformatVolume);
    }
package com.jit.video;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

public class VolumnController {
    private Toast t;
    private VolumnView tv;

    private Context context;

    public VolumnController(Context context) {
        this.context = context;
    }

    public void show(float progress) {
        if (t == null) {
            t = new Toast(context);
            View layout = LayoutInflater.from(context).inflate(R.layout.vv, null);
            tv = (VolumnView) layout.findViewById(R.id.volumnView);
            t.setView(layout);
            t.setGravity(Gravity.BOTTOM, 0, 100);
            t.setDuration(Toast.LENGTH_SHORT);
        }
        tv.setProgress(progress);
        t.show();
    }
}
package com.jit.video;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

/**
 * 仿小米聲音調節環形進度條
 */
public class VolumnView extends View {

    // 半徑
    float r1 = 0;
    float r2 = 0;
    float r3 = 0;
    // 外圓寬度
    float w1 = 15;
    // 內圓寬度
    float w2 = 30;
    Paint paint;

    // 進度
    float progress = 0;

    Bitmap bitmap;

    RectF oval;

    public VolumnView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    public VolumnView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public VolumnView(Context context) {
        super(context);
        init(context);
    }

    void init(Context context) {
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setStyle(Style.STROKE);
        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ling);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        float cx = getMeasuredWidth() / 2;
        float cy = getMeasuredHeight() / 2;
        r1 = cx - w1 / 2;
        r2 = cx - w1 / 2 - w2 / 2;
        r3 = cx - w1 / 2 - w2;

        // 繪製外圓
        paint.setStrokeWidth(w1);
        paint.setColor(Color.parseColor("#454547"));
        canvas.drawCircle(cx, cy, r1, paint);

        // 繪製中間圓環
        paint.setColor(Color.parseColor("#747476"));
        paint.setStrokeWidth(w2);
        canvas.drawCircle(cx, cy, r2, paint);

        // 繪製內圓
        paint.setColor(Color.parseColor("#464648"));
        paint.setStyle(Style.FILL);
        canvas.drawCircle(cx, cy, r3, paint);

        // 繪製中間的圖片
        canvas.drawBitmap(bitmap, cx - bitmap.getWidth() / 2,
                cx - bitmap.getHeight() / 2, paint);

        // 繪製文本
        paint.setColor(Color.WHITE);
        paint.setStrokeWidth(0);
        paint.setTextSize(40);
        float textWidth = paint.measureText("鈴聲"); // 測量字體寬度,我們需要根據字體的寬度設置在圓環中間

        canvas.drawText("鈴聲", cx - textWidth / 2, cx + bitmap.getHeight() / 2
                + 40, paint);

        // 繪製進度
        paint.setStyle(Style.STROKE);
        paint.setStrokeWidth(w2);
        paint.setColor(Color.WHITE);
        if (oval == null) {
            oval = new RectF(cx - r2, cy - r2, cx + r2, cy + r2); // 用於定義的圓弧的形狀和大小的界限
        }
        canvas.drawArc(oval, 270, 360 * progress / 100, false, paint);

        super.onDraw(canvas);
    }

    /**
     * 設置進度
     * 
     * @param progress
     *            範圍(0-100)
     */
    public void setProgress(float progress) {
        this.progress = progress;
        if (this.progress >= 100)
            this.progress = 100;
        if (this.progress <= 0)
            this.progress = 0;
        postInvalidate();
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章