Android动画显示从左到右的视图

时间:2014-12-10 17:35:38

标签: android animation slide

我正在尝试将视图的状态从不可见状态更改为可见状态,并且我还想让动画从左到右显示(不移动)。

我在jquery中做过的例子:

$(".slide").animate({width:'toggle'},3000);

Javascript EXAMPLE

由于

1 个答案:

答案 0 :(得分:0)

您不一定需要更改文字的可见性,您可以用另一个覆盖视图,然后设置重叠的动画从Visible到Gone:

的活动:

public class MainActivity extends Activity {
    private View coverView;
    private TextView txtText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);
        coverView = findViewById(R.id.coverView);
        txtText = (TextView) findViewById(R.id.txtText);
        coverView.post(new Runnable() {

            @Override
            public void run() {
                move();
            }
        });
    }

    private void move() {

        //Load animation
        Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.text_translate);
        //Know when it ends to change visibility
        animation.setAnimationListener(new AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                coverView.setVisibility(View.GONE);
            }
        });
        coverView.startAnimation(animation);
    }
}

布局xml(main_activity.xml):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/txtText"
        android:layout_width="120dp"
        android:layout_height="50dp"
        android:text="testtestetstesttest" />

    <View
        android:id="@+id/coverView"
        android:layout_width="120dp"
        android:layout_height="50dp"
        android:background="@android:color/white" />

</RelativeLayout>

动画XML。(位于res / anim中)text_translate.xml:

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="3000"
    android:fromXDelta="0"
    android:toXDelta="240" >

</translate>

你必须玩其他东西,比如背景。如果您想要的只是使用TextView并对其进行翻译,请在&#34; fromDeltaX&#34;中使用负值。和#34; toDeltaX&#34;。但是,结果将不会在您的链接中显示。

参考文献
Android Developers: Adding Animations