Yasic / ParticleTextView
- вторник, 16 мая 2017 г. в 03:12:19
Java
一个用粒子动画显示文字的 Android 自定义 View
ParticleTextView 是一个 Android 平台的自定义 view 组件,可以用彩色粒子组成指定的文字,并配合多种动画效果和配置属性,呈现出丰富的视觉效果。
compile 'yasic.library.ParticleTextView:particletextview:0.0.5'
<com.yasic.library.particletextview.View.ParticleTextView
android:id="@+id/particleTextView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
ParticleTextView particleTextView = (ParticleTextView) findViewById(R.id.particleTextView);
RandomMovingStrategy randomMovingStrategy = new RandomMovingStrategy();
ParticleTextViewConfig config = new ParticleTextViewConfig.Builder()
.setRowStep(8)
.setColumnStep(8)
.setTargetText("Random")
.setReleasing(0.2)
.setParticleRadius(4)
.setMiniDistance(0.1)
.setTextSize(150)
.setMovingStrategy(randomMovingStrategy)
.instance();
particleTextView.setConfig(config);
particleTextView.startAnimation();
particleTextView1.stopAnimation();
移动轨迹策略继承自抽象类 MovingStrategy,可以自己继承并实现其中的 setMovingPath 方法,以下是自带的几种移动轨迹策略。
配置信息类采用工厂模式创建,以下属性均为可选属性。
setTargetText(String targetText)
setTextSize(int textSize)
setParticleRadius(float radius)
采样间隔越小生成的粒子数目越多,但绘制帧数也随之降低,建议结合文字大小与粒子半径进行调节。
setColumnStep(int columnStep)
setRowStep(int rowStep)
setReleasing(double releasing)
指定时刻,粒子的运动速度由下列公式决定,其中 Vx 和 Vy 分别是 X 与 Y 方向上的运动速度,target 与 source 分别是粒子的目的坐标与当前坐标
Vx = (targetX - sourceX) * releasing
Vy = (targetY - sourceY) * releasing
当粒子与目的坐标距离小于最小判决距离时将直接移动到目的坐标,从而减少不明显的动画过程。
setMiniDistance(double miniDistance)
默认使用完全随机的颜色域
setParticleColorArray(String[] particleColorArray)
默认使用随机分布式策略
setMovingStrategy(MovingStrategy movingStrategy)
delay < 0 时动画不循环
setDelay(Long delay)
setConfig(ParticleTextViewConfig config)
void startAnimation()
void stopAnimation()
暂停是指动画完成了一段路径后的暂留状态
boolean isAnimationPause()
停止是指动画完成了一次完整路径后的停止状态
boolean isAnimationStop()
继承自 SurfaceView 类,利用子线程进行 Canvas 绘制,在多个组件渲染情况下效率更高。所有 API 与 ParticleTextView 类一致。
Copyright 2017 Yasic
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.