如何使用加载动画创建自定义视图 - 从左到右填充自定义文本?

How to a create custom view with loading animation - filling custom text from left to right?

我用 xml 创建了加载动画并在 activity 中播放了它。

xml-文件:anim.xml

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false">
    <item android:drawable="@drawable/loading_00001" android:duration="50" />
    <item android:drawable="@drawable/loading_00002" android:duration="50" />
    <item android:drawable="@drawable/loading_00003" android:duration="50" />
    <item android:drawable="@drawable/loading_00004" android:duration="50" />
    <item android:drawable="@drawable/loading_00005" android:duration="50" />
</animation-list>

activity 中的方法:playAnimation

private void playAnimation() {    
    ImageView imageView = (ImageView) findViewById(R.id.imageView);
    imageView.setBackgroundResource(R.drawable.anim);
    AnimationDrawable animation = (AnimationDrawable) imageView.getBackground();
    animation.start();
}

得到:

需要创建相同的,但需要使用另一种方式 - 没有可绘制资源的自定义视图。仅以编程方式绘制。

像这样:

public CustomLoadingView extends View {

   public CustomLoadingView(Context context) {
       this(context, null);
   }

   public CustomLoadingView(Context context, AttributeSet attrs) {
       this(context, attrs, 0);
   }

   public CustomLoadingView(Context context, AttributeSet attrs, int defStyleAttr) {
       super(context, attrs, defStyleAttr);
       init(attrs);
   }

   private void init(AttributeSet attrs) {}

   @Override
   protected void onDraw(Canvas canvas) {}
}

CustomLoadingView里面需要写什么,才能得到

???

我会使用透明文本,其下方有一个填充矩形。以下是我的做法:

  • 在某些可视化编辑软件中,例如 paint.net,在白色背景上创建文本,将边框设为橙色并使填充透明。 (显然,你可以使用任何你想要的颜色,但透明度是必要的)
  • 在 activity () 上放置一个高度相同但宽度为 0 的矩形。将其左对齐并将其(背景)颜色设置为橙色。
  • 在您的 onStart 方法中,安排一个 TimerTask 让矩形的宽度每 50 毫秒增加 5dp(玩弄这些数字)。 (You can use LayoutParams for changing the width and TimerTask for looping every x milliseconds)
  • 当矩形到达屏幕末端时,将其宽度重置为 0。(You can use DisplayMetrics to determine the width of the screen in dp)

我不会使用自定义视图。