第一步:写一个类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//如果小于当前的音量就画绿色的图片
if(i//从下面开始画
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"?>
    
                 


                    


           
 





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




    
            android:id="@+id/myviewid"
        android:layout_width="20dp"
        android:layout_height="155dp"
        sound:scale="10"
        >
   







第四步:写一个接口
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();
}
});
    }
}









更多相关文章

  1. 浅谈Java中Collections.sort对List排序的两种方法
  2. Python list sort方法的具体使用
  3. python list.sort()根据多个关键字排序的方法实现
  4. android 多点触摸实现图片缩放
  5. android 图片缩放
  6. Android(安卓)加密解密
  7. android批量获取res文件中的drawable图片
  8. 从AsyTask函数式封装到lambda表达式
  9. Android(安卓)图标转换、点9格式图片在线生成的好网站

随机推荐

  1. Android(安卓)9.0 Launcher Workspace 加
  2. Android(安卓)Studio1.5 配置Android(安
  3. 申请Google Map密钥
  4. Port SDL/TinySDGL to android with nati
  5. 高通平台android9.0设置开机默认横屏显示
  6. GridView 中Item项居中显示
  7. Android上传图片(PHP服务器)
  8. android腾讯微博吹一吹实现原理
  9. 查看android系统设备信息
  10. Android(安卓)SimapleDateFormat 日期格