1.效果图:
2.自定义的音量旋钮
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:MCCP_SeekBar="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<RelativeLayout
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_gravity="center"
android:gravity="center"
android:orientation="horizontal">
<com.example.longshine.zname.MCCP_SeekBar
android:id="@+id/id_mcl_seekbar_main_valume"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:layout_gravity="center_vertical"
android:thumb="@drawable/mhs_seekbar_thumb"
MCCP_SeekBar:mccp_end_angle="45"
MCCP_SeekBar:mccp_max="46"
MCCP_SeekBar:mccp_seekbar_bg_color="@color/mc_seekbar_bg_color"
MCCP_SeekBar:mccp_seekbar_bg_width="2dp"
MCCP_SeekBar:mccp_seekbar_inside_color="@color/mc_seekbar_inside_color"
MCCP_SeekBar:mccp_seekbar_outside_color="@color/mc_seekbar_outside_color"
MCCP_SeekBar:mccp_seekbar_progress_bg_color="@color/mc_seekbar_progress_bg_color"
MCCP_SeekBar:mccp_seekbar_progress_thumb_r="4dp"
MCCP_SeekBar:mccp_seekbar_thumb_color="@color/mc_seekbar_thumb_color"
MCCP_SeekBar:mccp_start_angle="135" />
<TextView
android:id="@+id/id_b_valume"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:gravity="center"
android:layout_centerInParent="true"
android:clickable="false"
android:singleLine="true"
android:textColor="@color/text_color_high_set"
android:textSize="60sp"
android:textAllCaps="false"
/>
</RelativeLayout>
</LinearLayout>
3.图片资源mhs_seekbar_thumb.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_activated="false"
android:state_pressed="false"
android:drawable="@drawable/mhs_thumb_normal"/>
<item
android:state_activated="true"
android:state_pressed="false"
android:drawable="@drawable/mhs_thumb_normal" />
<item
android:state_activated="false"
android:state_pressed="true"
android:drawable="@drawable/mhs_thumb_normal"/>
<item
android:state_activated="true"
android:state_pressed="true"
android:drawable="@drawable/mhs_thumb_normal" />
</selector>
4.创建一个attr:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MCCP_SeekBar">
<attr name="android:thumb" />
<attr name="mccp_max" format="integer"></attr>
<attr name="mccp_start_angle" format="integer"></attr>
<attr name="mccp_end_angle" format="integer"></attr>
<attr name="mccp_seekbar_progress_unit_text" format="string"></attr>
<attr name="mccp_seekbar_progress_textsize" format="integer"></attr>
<attr name="mccp_seekbar_progress_unit_textsize" format="integer"></attr>
<attr name="mccp_seekbar_bg_width" format="dimension"></attr>
<attr name="mccp_seekbar_progress_width" format="dimension"></attr>
<attr name="mccp_seekbar_outside_color" format="color"></attr>
<attr name="mccp_seekbar_bg_color" format="color"></attr>
<attr name="mccp_seekbar_inside_color" format="color"></attr>
<attr name="mccp_seekbar_progress_bg_color" format="color"></attr>
<attr name="mccp_seekbar_thumb_color" format="color"></attr>
<attr name="mccp_seekbar_progress_text_color" format="color"></attr>
<attr name="mccp_seekbar_progress_unit_color" format="color"></attr>
<attr name="mccp_seekbar_progress_start_color" format="color"></attr>
<attr name="mccp_seekbar_progress_mid_color" format="color"></attr>
<attr name="mccp_seekbar_progress_end_color" format="color"></attr>
<attr name="mccp_seekbar_progress_linelong" format="dimension"></attr>
<attr name="mccp_seekbar_progress_thumb_r" format="dimension"></attr>
<attr name="mccp_seekbar_progress_color_style" format="integer"></attr>
</declare-styleable>
</resources>
5.创建一个color
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<color name="text_color_user_group_sel_name">#FF0289ff</color>
<color name="text_color_user_group">#FF161616</color>
<color name="text_color_user_group_name">#FF161616</color>
<color name="text_color_main_valume">#FFe0e0e0</color>
<color name="text_color_high_set">#FF334456</color>
<color name="text_color_back">#FF0289ff</color>
<color name="mc_seekbar_outside_color">#FF2565c3</color>
<color name="mc_seekbar_bg_color">#FFbcb7b4</color>
<color name="mc_seekbar_inside_color">#FF88b6fb</color>
<color name="mc_seekbar_progress_bg_color">#FF0289ff</color>
<color name="mc_seekbar_thumb_color">#FFf6ff00</color>
<color name="text_color_mainvalume_t">#FF161616</color>
<color name="white">#FFFFFF</color>
</resources>
6.事件监听和功能调用
import android.media.AudioManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.SeekBar;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private AudioManager mAudioManager;
private MCCP_SeekBar VS_MainValume;
private int progress = 7;
private TextView tv_valume;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
VS_MainValume = (MCCP_SeekBar) findViewById(R.id.id_mcl_seekbar_main_valume);
tv_valume = (TextView)findViewById(R.id.id_b_valume);
mAudioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
//系统最大音量为15
VS_MainValume.setProgressMax(mAudioManager.getStreamMaxVolume(mAudioManager.STREAM_MUSIC));
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, progress, AudioManager.FLAG_SHOW_UI);
VS_MainValume.setProgress(progress);
tv_valume.setText(""+progress);
VS_MainValume.setOnMCLSeekBarChangeListener(new MCCP_SeekBar.OnMCCP_SeekBarChangeListener() {