Animation动画配置文件原来还可以设置加上p去,加了p后,移动就是从屏幕边开始一直移动到指定的地方。
效果图:
代码很简单:
(1)MainActivity.java
package com.example.animationtest; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.widget.TextView; public class MainActivity extends Activity { private TextView textView = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView) findViewById(R.id.hello_tv); /** * 点击出动画 */ View testButton = findViewById(R.id.hello_btn); testButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.slide_left_in); textView.startAnimation(animation); } }); } }
(2)布局activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:id="@+id/hello_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" android:layout_centerInParent="true" /> <Button android:id="@+id/hello_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_alignParentBottom="true" android:text="测试动画" /> </RelativeLayout>
扫描二维码关注公众号,回复:
565354 查看本文章
(3)左平移slide_left_in.xml(这里很关键,当设值android:fromXDelta带p时,平移是从屏幕最右侧一直滑动到控件所停位置,不带p就只滑动一个控件的宽度)
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:duration="200" android:fromXDelta="100.0%p" android:toXDelta="0.0" /> </set>