不幸的是沒有可用的方法可用於播放漣漪動畫。但我能想到的兩種可能的方式來實現這一
#1使用反射
public static void simulateButtonPress(final View view)
{
Drawable drawable = view.getBackground();
if(drawable instanceof RippleDrawable)
{
try
{
final RippleDrawable rd = ((RippleDrawable)drawable);
final Method setRippleActive = rd.getClass().getDeclaredMethod("setRippleActive", boolean.class);
setRippleActive.setAccessible(true);
setRippleActive.invoke(rd, true); //setRippleActive(true)
//exit ripple effect after 250 milliseconds
new Handler().postDelayed(new Runnable()
{
@Override
public void run()
{
try
{
setRippleActive.invoke(rd, false); //setRippleActive(false)
}
catch (Exception e)
{
e.printStackTrace();
}
}
}, 250);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
#2通過模擬運動活動ACTION_DOWN和ACTION_CANCEL
public static void simulateButtonPress(final View view)
{
final long now = SystemClock.uptimeMillis();
final MotionEvent pressEvent = MotionEvent.obtain(now, now, MotionEvent.ACTION_DOWN, view.getWidth()/2, view.getHeight()/2, 0);
view.dispatchTouchEvent(pressEvent);
new Handler().postDelayed(new Runnable()
{
@Override
public void run()
{
final long now = SystemClock.uptimeMillis();
final MotionEvent cancelEvent = MotionEvent.obtain(now, now, MotionEvent.ACTION_CANCEL, view.getWidth()/2, view.getHeight()/2, 0);
view.dispatchTouchEvent(cancelEvent);
}
}, 250);
}
經過這可能是它可以幫助你可能dublicate的http://stackoverflow.com/a/26604471/5305430 – sushildlh