package com.example.repeatingimagebutton;
import com.example.repeatingimagebutton.RepeatingImageButton.RepeatListener;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.Toast;
/**
* A button that will repeatedly call a 'listener' method
* as long as the button is pressed.
*/
class RepeatingImageButton extends ImageButton {
private long mStartTime;
private int mRepeatCount;
private RepeatListener mListener;
private long mInterval = 500;
public RepeatingImageButton(Context context) {
this(context, null);
}
public RepeatingImageButton(Context context, AttributeSet attrs) {
this(context, attrs, android.R.attr.imageButtonStyle);
}
public RepeatingImageButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setFocusable(true);
setLongClickable(true);
}
/**
* Sets the listener to be called while the button is pressed and
* the interval in milliseconds with which it will be called.
* @param l The listener that will be called
* @param interval The interval in milliseconds for calls
*/
public void setRepeatListener(RepeatListener l, long interval) {
mListener = l;
mInterval = interval;
}
@Override
public boolean performLongClick() {
mStartTime = SystemClock.elapsedRealtime();
mRepeatCount = 0;
post(mRepeater);
return true;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
// remove the repeater, but call the hook one more time
//Toast.makeText(getContext(), "UUUUPPPPPP", Toast.LENGTH_SHORT).show();
removeCallbacks(mRepeater);
if (mStartTime != 0) {
doRepeat(true);
mStartTime = 0;
}
}
return super.onTouchEvent(event);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_CENTER:
case KeyEvent.KEYCODE_ENTER:
// need to call super to make long press work, but return
// true so that the application doesn't get the down event.
super.onKeyDown(keyCode, event);
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_CENTER:
case KeyEvent.KEYCODE_ENTER:
// remove the repeater, but call the hook one more time
removeCallbacks(mRepeater);
if (mStartTime != 0) {
doRepeat(true);
mStartTime = 0;
}
}
return super.onKeyUp(keyCode, event);
}
private Runnable mRepeater = new Runnable() {
public void run() {
doRepeat(false);
if (isPressed()) {
postDelayed(this, mInterval);
}
}
};
private void doRepeat(boolean last) {
long now = SystemClock.elapsedRealtime();
if (mListener != null) {
mListener.onRepeat(this, now - mStartTime, last ? -1 : mRepeatCount++);
}
}
public interface RepeatListener {
/**
* This method will be called repeatedly at roughly the interval
* specified in setRepeatListener(), for as long as the button
* is pressed.
* @param v The button as a View.
* @param duration The number of milliseconds the button has been pressed so far.
* @param repeatcount The number of previous calls in this sequence.
* If this is going to be the last call in this sequence (i.e. the user
* just stopped pressing the button), the value will be -1.
*/
void onRepeat(View v, long duration, int repeatcount);
}
}
/*
* 长按按钮,将在屏幕顺序打印1,2,3,4,5...
*/
public class MainActivity extends Activity {
private int msg = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
RepeatingImageButton repeatBtn = new RepeatingImageButton(this);
repeatBtn.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
repeatBtn.setRepeatListener(new RepeatListener() {
@Override
public void onRepeat(View v, long duration, int repeatcount) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, String.valueOf(msg++), Toast.LENGTH_SHORT).show();
}
}, 1000);
layout.addView(repeatBtn);
setContentView(layout);
}
}
android自带音乐播放器分析RepeatingImageButton
最新推荐文章于 2021-05-28 19:39:34 发布