简体   繁体   English

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

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

I created loading animation with xml and played it in activity.我用xml创建了加载动画并在活动中播放。

xml-file: anim.xml 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>

method in activity: playAnimation活动中的方法:playAnimation

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

Got:得到了:

在android上从左到右填充自定义文本

Need create the same, but need use another way - custom view without drawable resources.需要创建相同的,但需要使用另一种方式 - 没有可绘制资源的自定义视图。 Only programmatically drawing.只能以编程方式绘制。

Something like this:像这样的东西:

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) {}
}

What need to write inside CustomLoadingView, to get this需要在CustomLoadingView里面写什么,才能得到这个

在android上从左到右填充自定义文本

??? ???

I would use transparent text with a filling rectangle underneath it.我会使用透明文本,下面有一个填充矩形。 Here's how I would do it:这是我将如何做到的:

I wouldn't use a custom view.我不会使用自定义视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM