VideoView 自定义控制器可见性
VideoView CustomControllers Visibility
我在 videoview 上创建了带有搜索栏的自定义控制器。这些控制器采用线性布局并且工作正常。现在我想像任何其他 android 播放器一样设置此自定义控制器布局的可见性。就像我开始播放视频时它出现 7 秒然后消失,当用户触摸视频时它出现并保持 7 秒然后再次触摸它消失。我的密码是
myMusicView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
try {
if (SystemClock.elapsedRealtime() - mLastClickTime < 700){
return false;
}
mLastClickTime = SystemClock.elapsedRealtime();
if(isShowing){
musicControllBar.setVisibility(View.GONE);
isShowing=false;
}else{
musicControllBar.setVisibility(View.VISIBLE);
HideLayout();
isShowing=true;
}
} catch (Exception e) {
// TODO: handle exception
}
return false;
}
});
private void HideLayout() {
try {
musicControllBar.setVisibility(View.VISIBLE);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
musicControllBar.setVisibility(View.INVISIBLE);
}
}, 7000);
} catch (Exception e) {
// TODO: handle exception
}
}
我建议使用 CountDownTimer,而不是使用 Handler。
将 class 的全局 CountDownTimer 变量声明为:
CountDownTimer countDownTimer;
对 HideLayout() 函数进行以下更改:
try{
musicControlBar.setVisibility(View.VISIBLE);
countDownTimer = new CountDownTimer(7000, 1000){
@Override
public void onTick(long millisUntilFinished) {
//do nothing
}
@Override
public void onFinish() {
musicControllBar.setVisibility(View.INVISIBLE);
}
}.start();
}catch(Exception e){}
此外,替换您的代码
if (SystemClock.elapsedRealtime() - mLastClickTime < 700){
return false;
}
与
countDownTimer.cancel();
1) First add this code as mention in above answer
private void loadTimer(){
try{
musicControlBar.setVisibility(View.VISIBLE);
countDownTimer = new CountDownTimer(7000, 1000){
@Override
public void onTick(long millisUntilFinished) {
//do nothing
}
@Override
public void onFinish() {
//this hide your layout when reach at 7 sec
musicControllBar.setVisibility(View.INVISIBLE);
}
}.start();
}catch(Exception e){}
}
2) Try onClickListener at VideoView in onClick() repeat your above code
if(isShowing){
musicControllBar.setVisibility(View.GONE);
isShowing=false;
}else{
if(countDownTimer!=null){
countDownTimer.cancel();
loadTimer();//start timer
}
musicControllBar.setVisibility(View.VISIBLE);
isShowing=true;
}
我在 videoview 上创建了带有搜索栏的自定义控制器。这些控制器采用线性布局并且工作正常。现在我想像任何其他 android 播放器一样设置此自定义控制器布局的可见性。就像我开始播放视频时它出现 7 秒然后消失,当用户触摸视频时它出现并保持 7 秒然后再次触摸它消失。我的密码是
myMusicView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
try {
if (SystemClock.elapsedRealtime() - mLastClickTime < 700){
return false;
}
mLastClickTime = SystemClock.elapsedRealtime();
if(isShowing){
musicControllBar.setVisibility(View.GONE);
isShowing=false;
}else{
musicControllBar.setVisibility(View.VISIBLE);
HideLayout();
isShowing=true;
}
} catch (Exception e) {
// TODO: handle exception
}
return false;
}
});
private void HideLayout() {
try {
musicControllBar.setVisibility(View.VISIBLE);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
musicControllBar.setVisibility(View.INVISIBLE);
}
}, 7000);
} catch (Exception e) {
// TODO: handle exception
}
}
我建议使用 CountDownTimer,而不是使用 Handler。
将 class 的全局 CountDownTimer 变量声明为:
CountDownTimer countDownTimer;
对 HideLayout() 函数进行以下更改:
try{
musicControlBar.setVisibility(View.VISIBLE);
countDownTimer = new CountDownTimer(7000, 1000){
@Override
public void onTick(long millisUntilFinished) {
//do nothing
}
@Override
public void onFinish() {
musicControllBar.setVisibility(View.INVISIBLE);
}
}.start();
}catch(Exception e){}
此外,替换您的代码
if (SystemClock.elapsedRealtime() - mLastClickTime < 700){
return false;
}
与
countDownTimer.cancel();
1) First add this code as mention in above answer
private void loadTimer(){
try{
musicControlBar.setVisibility(View.VISIBLE);
countDownTimer = new CountDownTimer(7000, 1000){
@Override
public void onTick(long millisUntilFinished) {
//do nothing
}
@Override
public void onFinish() {
//this hide your layout when reach at 7 sec
musicControllBar.setVisibility(View.INVISIBLE);
}
}.start();
}catch(Exception e){}
}
2) Try onClickListener at VideoView in onClick() repeat your above code
if(isShowing){
musicControllBar.setVisibility(View.GONE);
isShowing=false;
}else{
if(countDownTimer!=null){
countDownTimer.cancel();
loadTimer();//start timer
}
musicControllBar.setVisibility(View.VISIBLE);
isShowing=true;
}