TranslateAnimation如何在Android上运作? [英] How does TranslateAnimation work on Android?

查看:272
本文介绍了TranslateAnimation如何在Android上运作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经历了

TranslateAnimation (float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)

,但对Translate animation的工作方式仍然感到困惑.

but am still confused about how Translate animation works.

有人可以解释一下它是如何工作的吗?我看了说的文档

Can someone please explain how it works? I read the docs which say

fromXDelta  Change in X coordinate to apply at the start of the animation
toXDelta    Change in X coordinate to apply at the end of the animation
fromYDelta  Change in Y coordinate to apply at the start of the animation
toYDelta    Change in Y coordinate to apply at the end of the animation 

但是我仍然不清楚它是如何工作的.

but it is still not clear to me how it works.

编辑:我有一个ButtonLinearLayout,没有任何子级.当我单击Button时,我想动态生成一个TextView并对该TextView进行动画处理以显示在LinearLayout中. TextView的数量将取决于在按钮上单击的次数.

EDIT: I have a Button and a LinearLayout without any children. When I am clicking on the Button I want to dynamically generate a TextView and animate that TextView to appear in the LinearLayout. The number of TextViews will depend upon the number of clicks on the Button.

推荐答案

AFAIK,两者之间存在相对联系.

AFAIK,there would be relative connection between this.

也就是说,如果您要将隐藏的文本视图从屏幕的右侧转换为屏幕的左侧,则只需单击按钮,实际上就需要从X方向的100%转换它( 屏幕右侧)到X方向的0%(屏幕左侧).

That is,if you want to translate a hidden textview from right of screen to left of screen,on click of a button,you actually need to translate it from 100% of X-direction(right side of screen) to 0% of X-direction(left side of screen).

这时,您根本不需要更改Y方向.因此,两个选项的Y方向都将为0%.因此,最后,您将拥有:

At this point,you don't need to change Y-direction at all.so that would be 0% for both the options.So finally,you will have:

来自XDelta 100%

toXDelta 0%

来自YDelta 0%

toYDelta 0%

您可以根据需要通过在0到100之间设置此百分比来限制组件的显示.

you can restrict view of the component by setting this percentages between 0 to 100,as per your requirement.

同样,如果您还需要在Y方向上平移组件,则需要将0%更改为其他值.

Similarly,if you need to translate your component on Y-direction as well,then you need to change 0% to some other value.

希望,您现在知道了.

根据需要,您需要覆盖button-1的单击,然后可以控制button-2的可见性和翻译.

for your requirement,you need to override onclick of button-1,and there you can control button-2's visibility as well as translation.

在res的anim文件夹中创建动画文件.

create animation file in anim folder in your res.

translate_button.xml:

<?xml version="1.0" encoding="utf-8"?>
<!-- translating button from right to left -->
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
    <translate
        android:fromXDelta="100%" android:toXDelta="0%"
        android:fromYDelta="0%" android:toYDelta="0%"
        android:duration="900"
    />
</set>

现在,在您的活动文件中,

now,in your activity file,

...

// ll  is linear layout containing button_2
//counter is used to manage visibility of button_2 on click of button_1,i.e.1st click-button_2 would be visible,on 2nd click on button_1,it would be invisible.

//you can change behavior as per your need

button_2.setVisibility(View.GONE);
button_1.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {

        if(counter<1)
        {
            counter++;                  
            button_2.setVisibility(View.VISIBLE);
            Animation anim=AnimationUtils.loadAnimation(context, R.anim.translate_button);
            button_2.startAnimation(anim);
        }
        else
        {
            counter=0;
            button_2.setVisibility(View.GONE);
        }
    }
});
ll.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {

        if(counter==1)
        {
            counter=0;
            button_2.setVisibility(View.GONE);
        }
    }
});

...

这篇关于TranslateAnimation如何在Android上运作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆