android 用java实现 一串文字 从上到下显示 又从下到上结束
时间: 2024-03-17 07:46:36 浏览: 34
你可以通过使用属性动画和自定义控件来实现文字从上到下显示,又从下到上结束的效果。下面是实现的基本思路:
1. 自定义一个继承自TextView的控件,重写onDraw()方法,在该方法中绘制文字。
2. 创建一个属性动画,设置动画的起始和结束值,动画时长,以及动画的插值器等属性。
3. 在动画的更新监听器中,获取动画的当前值,根据当前值来计算文字的位置,然后调用invalidate()方法触发重新绘制。
4. 在Activity中使用自定义的控件,并启动属性动画。
下面是示例代码:
```
public class VerticalTextView extends AppCompatTextView {
private float mProgress = 0f;
public VerticalTextView(Context context) {
super(context);
}
public VerticalTextView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public VerticalTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onDraw(Canvas canvas) {
int width = getWidth();
int height = getHeight();
float textHeight = getTextSize();
Paint paint = getPaint();
paint.setColor(getCurrentTextColor());
canvas.translate(0, (1 - mProgress) * height);
canvas.drawText(getText().toString(), 0, textHeight, paint);
}
public void startAnimation() {
ValueAnimator animator = ValueAnimator.ofFloat(0f, 1f);
animator.setDuration(3000);
animator.setInterpolator(new AccelerateDecelerateInterpolator());
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
mProgress = (float) animation.getAnimatedValue();
invalidate();
}
});
animator.start();
}
}
```
在Activity中使用该控件和动画:
```
VerticalTextView textView = findViewById(R.id.text_view);
textView.startAnimation();
```
这样就可以实现一串文字从上到下显示,又从下到上结束的效果了。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)