Android5.1-setting-音量添加数值显示

1.xml

\packages\apps\Settings\res\layout\preference_volume_slider.xml


  +      <TextView
  +      android:layout_marginStart="1550dp"
  +      android:id="@+id/seek_bar_volume"
  +      android:layout_width="wrap_content"
  +      android:layout_height="wrap_content"
  +      android:text="@string/rtc_id"
  +      android:textSize="30sp" />    

2.添加代码

packages/apps/Settings/src/com/android/settings/notification/VolumeSeekBarPreference.java


    private String mSuppressionText;
+	private TextView mVolText;
    private boolean mMuted;
......

    protected void onBindView(View view) {
        super.onBindView(view);
        if (mStream == 0) {
            Log.w(TAG, "No stream found, not binding volumizer");
            return;
        }
        mSeekBar = (SeekBar) view.findViewById(com.android.internal.R.id.seekbar);
        mIconView = (ImageView) view.findViewById(com.android.internal.R.id.icon);
        mSuppressionTextView = (TextView) view.findViewById(R.id.suppression_text); 		
+		mVolText= (TextView) view.findViewById(R.id.seek_bar_volume);	
	
......


            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {
                if (mCallback != null) {
                    mCallback.onStreamValueChanged(mStream, progress);
+Progress=progress;
+		mVolText.setText(String.valueOf((100*Progress)/15));		
+ 	    Log.w(TAG, "onProgressChanged="+Progress);	

......

            public void onMuted(boolean muted) {
                if (mMuted == muted) return;
                mMuted = muted;
                updateIconView();
            }
        };
        final Uri sampleUri = mStream == AudioManager.STREAM_MUSIC ? getMediaVolumeUri() : null;
        if (mVolumizer == null) {
            mVolumizer = new SeekBarVolumizer(getContext(), mStream, sampleUri, sbvc);
        }
        mVolumizer.start();
        mVolumizer.setSeekBar(mSeekBar);
        updateIconView();
        mCallback.onStreamValueChanged(mStream, mSeekBar.getProgress());
+	mVolText.setText(String.valueOf((100*mSeekBar.getProgress())/15));		 
+ 	    Log.w(TAG, "onProgressChanged1111="+Progress);	
        updateSuppressionText();
    }
......


	

3.全部文件

//packages/apps/Settings/src/com/android/settings/notification/VolumeSeekBarPreference.java
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.settings.notification;

import android.content.ContentResolver;
import android.content.Context;
import android.media.AudioManager;
import android.net.Uri;
import android.preference.PreferenceManager;
import android.preference.SeekBarPreference;
import android.preference.SeekBarVolumizer;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import com.android.settings.R;

import java.util.Objects;

/** A slider preference that directly controls an audio stream volume (no dialog) **/
public class VolumeSeekBarPreference extends SeekBarPreference
        implements PreferenceManager.OnActivityStopListener {
    private static final String TAG = "VolumeSeekBarPreference";

    private int mStream;
    private SeekBar mSeekBar;
    private SeekBarVolumizer mVolumizer;
    private Callback mCallback;
    private ImageView mIconView;
    private TextView mSuppressionTextView;
    private String mSuppressionText;
//add long 	
	private TextView mVolText;
//end long 	
    private boolean mMuted;
    private int mIconResId;
    private int mMuteIconResId;
    private boolean mStopped;
	int Progress;
    public VolumeSeekBarPreference(Context context, AttributeSet attrs, int defStyleAttr,
            int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        setLayoutResource(R.layout.preference_volume_slider);
    }

    public VolumeSeekBarPreference(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    public VolumeSeekBarPreference(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public VolumeSeekBarPreference(Context context) {
        this(context, null);
    }

    public void setStream(int stream) {
        mStream = stream;
//add long 		
		Log.w(TAG, "setStream"+stream);
//end long 		
    }

    public void setCallback(Callback callback) {
        mCallback = callback;
    }

    public void onActivityResume() {
        if (mStopped) {
            init();
        }
    }

    @Override
    public void onActivityStop() {
        mStopped = true;
        if (mVolumizer != null) {
            mVolumizer.stop();
        }
    }

    @Override
    protected void onBindView(View view) {
        super.onBindView(view);
        if (mStream == 0) {
            Log.w(TAG, "No stream found, not binding volumizer");
            return;
        }
        mSeekBar = (SeekBar) view.findViewById(com.android.internal.R.id.seekbar);
        mIconView = (ImageView) view.findViewById(com.android.internal.R.id.icon);
        mSuppressionTextView = (TextView) view.findViewById(R.id.suppression_text);
//add long 		
		mVolText= (TextView) view.findViewById(R.id.seek_bar_volume);	
//end long 	
/*	 if(SystemProperties.get("persist.audio.volune",1).equals(1)) Progress=1 ;	
	else if(SystemProperties.get("persist.audio.volune",2).equals(2)) Progress=2 ;	
else if(SystemProperties.get("persist.audio.volune",3).equals(3)) Progress=3 ;	
else if(SystemProperties.get("persist.audio.volune",4).equals(4)) Progress=4 ;	
else if(SystemProperties.get("persist.audio.volune",5).equals(5)) Progress=5 ;	
else if(SystemProperties.get("persist.audio.volune",6).equals(6)) Progress=6 ;	
else if(SystemProperties.get("persist.audio.volune",7).equals(7)) Progress=7 ;	
else if(SystemProperties.get("persist.audio.volune",8).equals(8)) Progress=8 ;	
else if(SystemProperties.get("persist.audio.volune",9).equals(9)) Progress=9 ;	
else if(SystemProperties.get("persist.audio.volune",10).equals(10)) Progress=10 ;	
else if(SystemProperties.get("persist.audio.volune",11).equals(11)) Progress=11 ;	
else if(SystemProperties.get("persist.audio.volune",12).equals(12)) Progress=12 ;	
else if(SystemProperties.get("persist.audio.volune",13).equals(13)) Progress=13 ;	
else if(SystemProperties.get("persist.audio.volune",14).equals(14)) Progress=14 ;	
else if(SystemProperties.get("persist.audio.volune",15).equals(15)) Progress=15 ;	
else if(SystemProperties.get("persist.audio.volune",0).equals(0)) Progress=0 ;		
*/
        init();
    }

    private void init() {
        if (mSeekBar == null) return;
        getPreferenceManager().registerOnActivityStopListener(this);
        final SeekBarVolumizer.Callback sbvc = new SeekBarVolumizer.Callback() {
            @Override
            public void onSampleStarting(SeekBarVolumizer sbv) {
                if (mCallback != null) {
                    mCallback.onSampleStarting(sbv);
                }
            }
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {
                if (mCallback != null) {
                    mCallback.onStreamValueChanged(mStream, progress);
//add long 
Progress=progress;
		mVolText.setText(String.valueOf((100*Progress)/15));		
 	    Log.w(TAG, "onProgressChanged="+Progress);	
//		 SystemProperties.set("persist.audio.volune",Progress);		
//end long 		                
				}
            }
            @Override
            public void onMuted(boolean muted) {
                if (mMuted == muted) return;
                mMuted = muted;
                updateIconView();
            }
        };
        final Uri sampleUri = mStream == AudioManager.STREAM_MUSIC ? getMediaVolumeUri() : null;
        if (mVolumizer == null) {
            mVolumizer = new SeekBarVolumizer(getContext(), mStream, sampleUri, sbvc);
        }
        mVolumizer.start();
        mVolumizer.setSeekBar(mSeekBar);
        updateIconView();
        mCallback.onStreamValueChanged(mStream, mSeekBar.getProgress());
//add long 
	mVolText.setText(String.valueOf((100*mSeekBar.getProgress())/15));		 
 	    Log.w(TAG, "onProgressChanged1111="+Progress);	
//		 SystemProperties.set("persist.audio.volune",Progress);		
//end long 	
        updateSuppressionText();
    }

    // during initialization, this preference is the SeekBar listener
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {
        super.onProgressChanged(seekBar, progress, fromTouch);
        mCallback.onStreamValueChanged(mStream, progress);
	
    }

    private void updateIconView() {
        if (mIconView == null) return;
        if (mIconResId != 0) {
            mIconView.setImageResource(mIconResId);
        } else if (mMuteIconResId != 0 && mMuted) {
            mIconView.setImageResource(mMuteIconResId);
        } else {
            mIconView.setImageDrawable(getIcon());
        }
//add long 		
		 Log.w(TAG, "mprogress=="+Progress);
//end long 	
    }

    public void showIcon(int resId) {
        // Instead of using setIcon, which will trigger listeners, this just decorates the
        // preference temporarily with a new icon.
        if (mIconResId == resId) return;
        mIconResId = resId;
        updateIconView();
    }

    public void setMuteIcon(int resId) {
        if (mMuteIconResId == resId) return;
        mMuteIconResId = resId;
        updateIconView();
    }

    private Uri getMediaVolumeUri() {
        return Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://"
                + getContext().getPackageName()
                + "/" + R.raw.media_volume);
    }

    public void setSuppressionText(String text) {
        if (Objects.equals(text, mSuppressionText)) return;
        mSuppressionText = text;
        updateSuppressionText();
    }

    private void updateSuppressionText() {
        if (mSuppressionTextView != null && mSeekBar != null) {
            mSuppressionTextView.setText(mSuppressionText);
            final boolean showSuppression = !TextUtils.isEmpty(mSuppressionText);
            mSuppressionTextView.setVisibility(showSuppression ? View.VISIBLE : View.INVISIBLE);
            mSeekBar.setVisibility(showSuppression ? View.INVISIBLE : View.VISIBLE);
        }
    }

    public interface Callback {
        void onSampleStarting(SeekBarVolumizer sbv);
        void onStreamValueChanged(int stream, int progress);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

旋风旋风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值