简体   繁体   English

如何制作可点击的动画文本视图?

[英]How to make a clickable and animated textview?

I need to implement a moving/scrolling textview that got a clickable span. 我需要实现一个具有可点击范围的移动/滚动textview。 I also need to make the animation look like those banner ads below the news that repeat and repeat endlessly. 我还需要使动画看起来像新闻下面不断重复的横幅广告。 I used TranslateAnimation before for a tv app and able to have it work because what I get is the keyEvent and the the touch. 我之前在电视应用程序上使用过TranslateAnimation并能够使其工作,因为我得到的是keyEvent和touch。 I need to implement it now on a tablet app which doesn't have any external input source other than the touchscreen itself. 我现在需要在平板电脑应用程序上实现它,除了触摸屏本身,该应用程序没有任何外部输入源。

I tried to use TranslateAnimation again for the tablet app but it doesn't work. 我尝试再次将TranslateAnimation用于平板电脑应用程序,但它不起作用。 Upon searching about the different animation I could use, I've learned that TranslateAnimation just animates the look of the view and the the whole view itself. 在搜索了我可以使用的不同动画之后,我了解到TranslateAnimation只是对视图的外观和整个视图本身进行了动画处理。 It doesn't move the clickable parts with the look so I looked for a different one. 它不会移动可点击部分的外观,因此我寻找了另一部分。

Then I stumbled upon PropertyAnimator. 然后我偶然发现了PropertyAnimator。 It's what I clearly need for my problem. 这正是我显然需要解决的问题。 But there's also a drawback to it. 但是它也有一个缺点。 It doesn't support repeating. 它不支持重复。

What I currently have is this code below. 我目前所拥有的是下面的这段代码。

        lbl.animate();
        lbl.animate().x(-lblWidth).y(0);
        lbl.animate().setDuration(animDuration);
        lbl.animate().setInterpolator(new LinearInterpolator());
        lbl.animate().setListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                lbl.animate();
                lbl.animate().x(lblWidth).y(0);
                lbl.animate().setDuration(0);
                lbl.animate().x(-lblWidth).y(0);
                lbl.animate().setDuration(animDuration);
                lbl.animate().setInterpolator(new LinearInterpolator());
                lbl.animate().setListener(this);
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });

After the first run, the animation ends. 第一次运行后,动画结束。 It repeats because it prints my log but it's not showing on screen. 重复它是因为它打印了我的日志,但未在屏幕上显示。

I was trying to make a repeating effect with that code. 我试图用该代码产生重复效果。 I was trying to recreate something like this code below. 我试图在下面重新创建类似这样的代码。

            transAnim = new TranslateAnimation(
                    TranslateAnimation.RELATIVE_TO_PARENT, 1.0f,
                    TranslateAnimation.RELATIVE_TO_SELF, -1.0f,
                    TranslateAnimation.ABSOLUTE, 0f,
                    TranslateAnimation.ABSOLUTE, 0f
            );

            transAnim.setRepeatCount(-1);
            transAnim.setRepeatMode(Animation.RESTART);
            transAnim.setInterpolator(new LinearInterpolator());
            transAnim.setDuration(animDuration);
            transAnim.setFillAfter(true);

What the code above does is from the right, outside the screen, I start an animation that goes to the left, outside the screen. 上面的代码所做的是从屏幕外部的右侧开始,在屏幕外部,从左侧开始创建一个动画。 Then, if the view's end reach the left, outside the screen, tell the animation to restart from the beginning. 然后,如果视图的末端到达屏幕外部的左侧,请告诉动画从头开始重新播放。

I made it work now by adding a single short line of code above everything. 我现在通过在所有内容之上添加一小段代码来使其工作。

    lbl.setX(screenWidth);
    lbl.animate().x(-lblWidth).y(0);
    lbl.animate().setDuration(animDuration);
    lbl.animate().setInterpolator(new LinearInterpolator());

Then using those line of code again on the onAnimationEnd of the animate listener. 然后,再次在动画监听器的onAnimationEnd上使用这些代码行。 Now I thought of another thing, can I pause this animation? 现在我想到了另一件事,可以暂停此动画吗? Because I found a way to do it for TranslateAnimation. 因为我找到了一种针对TranslateAnimation的方法。 Is there a hack way to do it? 有破解的方法吗?

Try this I hope it work for you. 试试这个,希望对您有用。

 lbl.animate();
 lbl.animate().x(-lblWidth).y(0);
 lbl.animate().setDuration(animDuration);
 lbl.animate().setInterpolator(new LinearInterpolator());
 lbl.animate().setRepeatCount(Animation.INFINITE);

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

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