使用TextView在MotionEvent.ACTION_MOVE上显示当前音量

时间:2018-08-18 06:52:50

标签: android textview

我正在尝试在TextView中显示当前音量,但无法这样做,

我正在尝试实现两件事

1:要显示当前音量onTouch(不像吐司在几秒钟后消失),而是一种响应式布局,该布局仅在onTouch处于活动状态时可见,而在未触发音量时立即删除。

2:要在用户按住音量布局时保持(音量指示器)Textview布局处于活动状态,而在用户使用按钮时不显示该视图。

这是myservice.java     MService扩展了Service实现的OnTouchListener {

private WindowManager mWindowManager;
LinearLayout touchLayout;
float downY;
AudioManager audioManager;
TextView popTextView;

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@SuppressLint("ClickableViewAccessibility")
@Override
public void onCreate() {
    super.onCreate();

    touchLayout = new LinearLayout(this);
    LayoutParams lp = new LayoutParams(40, LayoutParams.MATCH_PARENT);
    touchLayout.setLayoutParams(lp);
   //   touchLayout.setBackgroundColor(Color.GREEN);//test period jkhfkbllnlknkljbkkbkbjkluglglblkkvkjv

    audioManager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);

    touchLayout.setOnTouchListener(this);

    mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
    WindowManager.LayoutParams mParams;
        mParams = new WindowManager.LayoutParams(40,
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT);

    mParams.gravity = Gravity.END | Gravity.TOP;

    mWindowManager.addView(touchLayout, mParams);
}

public boolean onTouch(View v, MotionEvent event) {
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
        case MotionEvent.ACTION_UP:
            downY = event.getY();
        case MotionEvent.ACTION_MOVE:
            float y2 = event.getY();
            if (downY < y2) {
                audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_LOWER, AudioManager.FLAG_PLAY_SOUND);

  //this is causing the crash.......      popTextView.setText(" "+downY);

            } else if (downY > y2) {
                audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, AudioManager.FLAG_PLAY_SOUND);
        }
    }
    return true;
}

这是我要在其上显示音量指示器的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/poplayout"
android:background="#000"
android:padding="5dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp">

<TextView
    android:id="@+id/popTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=""
    android:textColor="#fff"/>

</LinearLayout>

0 个答案:

没有答案