android调节系统音量

这篇博客介绍了如何在Android中实现自定义的音量调节视图,包括展示效果图,详细步骤涉及自定义音量旋钮的设计,图片资源的配置,属性的创建,颜色的选择,事件监听和功能实现的方法,最后提供了自定义View类的实现细节。
摘要由CSDN通过智能技术生成

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
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值