Android 自定义音量调节控件

第一步:写一个类MySoundView继承View
package com.geek.Sound.view;
import com.geek.Sound.R;
import com.geek.Sound.imp.MySoundIMP;
import android.R.integer;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.media.AudioManager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;


public class MySoundView extends View{
//定义一个自己写的接口
private MySoundIMP mySoundIMP;

    //定义一个上下文
private Context context;
//定义二张图片
private Bitmap green,gray;
//定义一张图片画多高
private int height;
//定义当前音量
private int scale;
//定义总音量是多少
private int most;
//定义媒体处理者
AudioManager  am;
 
    //一个参数的构造方法
public MySoundView(Context context) {
super(context);
this.context=context;
        init(null);
}


    //二个参数的构造方法
public MySoundView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context=context;
init(attrs);


}


//三个参数的构造方法
public MySoundView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.context=context;
init(attrs);
}
  
//触摸事件
@Override
public boolean onTouchEvent(MotionEvent event) {
//拿到这个控件被触摸的Y轴坐标是多少
int y=(int)event.getY();
//调一个自己写的逻辑方法
    setyscale(y);
    //这个方法的作用相当于调一次onDraw(Canvas canvas)方法
invalidate();
return true;
}
//自己写的逻辑方法
public void setyscale(int y){
if(y<10){
scale=15;
}else if(y>155){
scale=0;
}else{
scale=(most)-(y/10)+1;
}
  //改变系统的音量,
  am.setStreamVolume(AudioManager.STREAM_MUSIC,  scale , AudioManager.FX_FOCUS_NAVIGATION_DOWN);
}

//逻辑方法
public void init(AttributeSet attrs){
 //拿到一张图片(绿色的图片)
 green=BitmapFactory.decodeResource( context.getResources(), R.drawable.sound_line);
 //拿到一张图片(灰色的图片)
 gray=BitmapFactory.decodeResource(context.getResources(), R.drawable.sound_line1);
 //一张图片画多高(图片的高乘2)
 height = green.getHeight()*2;
    //拿到媒体处理者
   am=(AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
   //拿到媒体总音量的大小
   most=  am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
   //拿到媒体的当前音量
   scale=  am.getStreamVolume(AudioManager.STREAM_MUSIC);
   //获得属性信息
   TypedArray  typedArray=context.obtainStyledAttributes(attrs,R.styleable.MySoundView);
   //获取属性对应的属性值
   scale =typedArray.getInt(R.styleable.MySoundView_scale, scale);
   //改变系统的音量
   am.setStreamVolume(AudioManager.STREAM_MUSIC, scale, AudioManager.FX_FOCUS_NAVIGATION_DOWN);
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//根据总音量循环
for(int i=0;i<most;i++){
//如果小于当前的音量就画绿色的图片
if(i<scale){
//从下面开始画
canvas.drawBitmap(green,0 , (height*most)-(height*i), new Paint());
//否则就画灰色的图片
}else{

canvas.drawBitmap(gray,0 , (height*most)-(height*i), new Paint());
}
}
//判断是否为空
if(mySoundIMP!=null){
//调自己写的接口方法
mySoundIMP.addSoundChangeListener(this,scale);
}
}
//get
public MySoundIMP getMySoundIMP() {
return mySoundIMP;
}
//set
public void setMySoundIMP(MySoundIMP mySoundIMP) {
this.mySoundIMP = mySoundIMP;
}



}


第二步:在Values资源文件里建一个attrs.xml


<?xml version="1.0" encoding="utf-8"?>
<resources>     
                <declare-styleable name = "MySoundView"> 


                   <attr name = "scale" format="integer" /> 


            </declare-styleable> 
</resources>




第三步:在layout资源文件里的main.xml里面用
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:sound="http://schemas.android.com/apk/res/com.geek.Sound"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >




    
    <com.geek.Sound.view.MySoundView
        android:id="@+id/myviewid"
        android:layout_width="20dp"
        android:layout_height="155dp"
        sound:scale="10"
        >
    </com.geek.Sound.view.MySoundView>


</LinearLayout>




第四步:写一个接口
package com.geek.Sound.imp;


import android.view.View;


public interface MySoundIMP {
         public void addSoundChangeListener(View view,int row);
}




第五步:在MainActivity.java里面的用
package com.geek.Sound;
import com.geek.Sound.imp.MySoundIMP;
import com.geek.Sound.view.MySoundView;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;


public class MainActivity extends Activity {
MySoundView bitmap;
public Toast toast;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        bitmap=(MySoundView) findViewById(R.id.myviewid);
        bitmap.setMySoundIMP(new MySoundIMP() {

@Override
public void addSoundChangeListener(View view, int row) {
if(toast!=null){
toast=Toast.makeText(MainActivity.this, row+"", 500);
   }else{
   toast=Toast.makeText(MainActivity.this, row+"", 500);
   }
toast.show();
}
});
    }
}









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值