Skip to content

Repository files navigation

SnowingView

winter is coming...

介绍

一个看起来还不错的下雪动画。

功能&&实现

  1. 使用 Matrix 产生随机大小、透明度的雪花
  2. 使用 HandlerThread 来计算雪花的下一个位置
  3. 使用加速度传感器判断用户在 X 轴的倾斜方向,使雪花产生左右飘动的效果

缺点&改进

  1. 还未用 LeakCanary 检测是否有内存泄漏的情况
  2. 暂时还不支持 wrap_content
  3. 可能会采用 SurfaceView 来绘制提高效率

实际效果

SnowingView效果

使用(如果有人想用的话)

Step1

在布局中添加 SnowingView

<?xml version="1.0" encoding="utf-8"?>
<FrameLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@android:color/black"
>
<info.hellovass.snowingview.widgets.SnowingView
android:id="@+id/snowing_view"android:layout_width="match_parent"android:layout_height="match_parent"android:visibility="gone"app:src="@drawable/ic_snowflake"
/>
<Switchandroid:id="@+id/sw_snowing"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:text="winter is coming..."android:textColor="@android:color/white"android:textSize="16sp"
/>
</FrameLayout>

Step2

在 Activity 中,调用 SnowingView 的 startFall() 或者 stopFall() 方法。

publicclassSampleActivityextendsAppCompatActivity {
privateSnowingViewmSnowingView;
privateSwitchmSwitch;
@OverrideprotectedvoidonCreate(@NullableBundlesavedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample);
mSwitch = (Switch) findViewById(R.id.sw_snowing);
mSnowingView = (SnowingView) findViewById(R.id.snowing_view);
mSwitch.setOnCheckedChangeListener(newCompoundButton.OnCheckedChangeListener() {
@OverridepublicvoidonCheckedChanged(CompoundButtonbuttonView, booleanisChecked) {
if (isChecked) {
mSnowingView.startFall(); // 开始下雪
} else {
mSnowingView.stopFall(); // 停止下雪
}
}
});
}
}

参考

About

winter is coming...

Resources

Stars

262 stars

Watchers

5 watching

Forks

Releases

Packages

Contributors

Languages