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>
(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>
Android动画实现教程
本文介绍了一个简单的Android动画实现案例,通过点击按钮触发TextView从屏幕右侧平移到指定位置的动画效果。涉及MainActivity.java、布局文件及动画配置文件slide_left_in.xml。
867

被折叠的 条评论
为什么被折叠?



