0:动画的设置是在res下面创建一个anim 的文件夹,然后再创建resouce的文件,记得root element 一定要选择set,集合,
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromYDelta="0" android:toYDelta="-100" android:duration="200"></translate> </set>
1: 创建布局 ,还有要注意的是要在TextView 上面设置点击事件之前 要先设置 这个 TextView 是可以被点击的 。
<?xml version="1.0" encoding="utf-8"?> <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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <TextView android:text="Hello World!" android:layout_width="wrap_content" android:layout_height="wrap_content" android:clickable="true" android:onClick="zanAdd"/> <TextView android:id="@+id/zan" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="+1" android:textColor="#600F" android:visibility="invisible"/> </RelativeLayout>
2:Animation 是一个抽象的类是不可以通过new Animation来创建的必须要通过AnimationUtils.loadAnimation(this,R.anim.btn_add);来创建
其次,启动动画的时候是需要在那个上面有动画就要这个View 来启动,例如这里就是zan的这个所以就是zan.startAnimation(animation)
package tech.androidstudio.animationdemo; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.view.animation.TranslateAnimation; import android.widget.TextView; public class MainActivity extends AppCompatActivity implements Animation.AnimationListener { private TextView mZan; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mZan = (TextView) findViewById(R.id.zan); } public void zanAdd(View view) { mZan.setVisibility(View.VISIBLE); Animation animation = new AnimationUtils().loadAnimation(this,R.anim.btn_zan_add); mZan.startAnimation(animation); animation.setAnimationListener(this); } @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { mZan.setVisibility(View.INVISIBLE); } @Override public void onAnimationRepeat(Animation animation) { } }
1万+



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



