Android模擬下雨效果

使用RippleDrawable和模擬屏幕點擊實現(xiàn)下雨時水波紋效果。

布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_drawable"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="amorypepelu.github.io.drawabledemo.DrawableActivity">

    <Button
        android:id="@+id/RippleDrawableBtn"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/ripple"
        android:text="" />
</LinearLayout>

Drawable:

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@android:color/holo_purple"></ripple>

java代碼:

private int width;
private int height;
private Random random;

random = new Random();
width = getResources().getDisplayMetrics().widthPixels;
height = getResources().getDisplayMetrics().heightPixels;

Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        int x = (int) random.nextLong() % width;
        int y = (int) random.nextLong() % height;
        clickEvent(x, y);
        sendEmptyMessageDelayed(0, 600);
    }
};
handler.sendEmptyMessage(0);
handler.sendEmptyMessageDelayed(0,200);
handler.sendEmptyMessageDelayed(0,400);

模擬點擊事件:

private void clickEvent(int x, int y) {
    long downTime = SystemClock.uptimeMillis();
    MotionEvent downEvent = MotionEvent.obtain(downTime, downTime,
            MotionEvent.ACTION_DOWN, x, y, 0);
    MotionEvent upEvent = MotionEvent.obtain(downTime, downTime,
            MotionEvent.ACTION_UP, x, y, 0);
    mRippleDrawableBtn.onTouchEvent(downEvent);
    mRippleDrawableBtn.onTouchEvent(upEvent);
    downEvent.recycle();
    upEvent.recycle();
}

參考:

Drawable家族的前世今生

Android模擬點擊的四種方式

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容