按钮-声音-onHoverListener-老虎
Button - sound - onHoverListener - tiger
我需要做什么来制作我的按钮(或者其他什么,请告诉我应该做什么
我用):
Start playing music on ACTION_HOVER_ENTER
Keep playing (without reset, just do nothing) on ACTION_HOVER_MOVE
Stop playing and reset music on ACTION_HOVER_EXIT
Work when I start my move from anywhere on the screen, then without release hover enter
my button
Have no problem with starting the same activity but triggered by another button located in another part of the screen - without finger's release
我应该使用 onHoverListener()
,对吗?
如果可以请写代码示例:)
这是我目前拥有的:
package com.example.android.appname;
import android.content.Intent;
import android.graphics.Rect;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
public class DisplayActivity extends AppCompatActivity {
ImageView b1;
MediaPlayer mPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen);
b1 = (ImageView) findViewById(R.id.button2);
b1.setOnHoverListener(new View.OnHoverListener() {
@Override
public boolean onHover(View v, MotionEvent event) {
mPlayer = MediaPlayer.create(DisplayActivity.this, R.raw.sound);
switch (event.getAction()) {
case MotionEvent.ACTION_HOVER_ENTER:
if (mPlayer == null)
mPlayer.start();
break;
case MotionEvent.ACTION_HOVER_MOVE:
break;
case MotionEvent.ACTION_HOVER_EXIT:
mPlayer.release();
mPlayer = null;
break;
}
return false;
}
});
}
试试这样实现
这将需要您告诉系统您已经消费了事件并且有兴趣消费连续的事件。这可以通过返回true
(表示感兴趣),false
(表示不感兴趣)来完成。
@Override
public boolean onHover(View v, MotionEvent event) {
mPlayer = MediaPlayer.create(DisplayActivity.this, R.raw.sound);
// Flag to indicate interest
boolean consumable = false;
switch (event.getAction()) {
case MotionEvent.ACTION_HOVER_ENTER:
if (mPlayer == null) {
mPlayer.start();
consumable = true;
}
break;
case MotionEvent.ACTION_HOVER_MOVE:
consumable = true;
break;
case MotionEvent.ACTION_HOVER_EXIT:
mPlayer.release();
mPlayer = null;
consumable = true;
break;
}
return consumable;
}
注:There might be Hardware Limitations to detect any Hover
.
我需要做什么来制作我的按钮(或者其他什么,请告诉我应该做什么 我用):
Start playing music on
ACTION_HOVER_ENTER
Keep playing (without reset, just do nothing) on
ACTION_HOVER_MOVE
Stop playing and reset music on
ACTION_HOVER_EXIT
Work when I start my move from anywhere on the screen, then without release
hover enter
my buttonHave no problem with starting the same activity but triggered by another button located in another part of the screen - without finger's release
我应该使用 onHoverListener()
,对吗?
如果可以请写代码示例:)
这是我目前拥有的:
package com.example.android.appname;
import android.content.Intent;
import android.graphics.Rect;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
public class DisplayActivity extends AppCompatActivity {
ImageView b1;
MediaPlayer mPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen);
b1 = (ImageView) findViewById(R.id.button2);
b1.setOnHoverListener(new View.OnHoverListener() {
@Override
public boolean onHover(View v, MotionEvent event) {
mPlayer = MediaPlayer.create(DisplayActivity.this, R.raw.sound);
switch (event.getAction()) {
case MotionEvent.ACTION_HOVER_ENTER:
if (mPlayer == null)
mPlayer.start();
break;
case MotionEvent.ACTION_HOVER_MOVE:
break;
case MotionEvent.ACTION_HOVER_EXIT:
mPlayer.release();
mPlayer = null;
break;
}
return false;
}
});
}
试试这样实现
这将需要您告诉系统您已经消费了事件并且有兴趣消费连续的事件。这可以通过返回true
(表示感兴趣),false
(表示不感兴趣)来完成。
@Override
public boolean onHover(View v, MotionEvent event) {
mPlayer = MediaPlayer.create(DisplayActivity.this, R.raw.sound);
// Flag to indicate interest
boolean consumable = false;
switch (event.getAction()) {
case MotionEvent.ACTION_HOVER_ENTER:
if (mPlayer == null) {
mPlayer.start();
consumable = true;
}
break;
case MotionEvent.ACTION_HOVER_MOVE:
consumable = true;
break;
case MotionEvent.ACTION_HOVER_EXIT:
mPlayer.release();
mPlayer = null;
consumable = true;
break;
}
return consumable;
}
注:There might be Hardware Limitations to detect any Hover
.