标签:
1. 透明动画 按钮透明
① 用代码控制
创建一个按钮
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:weightSum="1"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="New Button" android:id="@+id/btnClickMe" /> </LinearLayout>
public class MainActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.findViewById(R.id.btnClickMe).setOnClickListener(new View.OnClickListener() { //按钮点击监听 @Override public void onClick(View v) { // 透明动画 透明效果从 0->1 AlphaAnimation aa = new AlphaAnimation(0, 1); aa.setDuration(1000); // 时间设置 1s v.startAnimation(aa); // 把按钮设置改动画 } }); System.out.println("onCreate"); } }
② 用 anim目录下面的xml配置文件来实现
在 res/anim/aa.xml
<?xml version="1.0" encoding="utf-8"?> <!-- fromAlpha从0 toAlpha到1 duration时间1s--> <alpha xmlns:android="http://schemas.android.com/apk/res/android" android:fromAlpha="0" android:toAlpha="1" android:duration="1000" />
public class MainActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.findViewById(R.id.btnClickMe).setOnClickListener(new View.OnClickListener() { //按钮点击监听 @Override public void onClick(View v) { /*// 透明动画 透明效果从 0->1 AlphaAnimation aa = new AlphaAnimation(0, 1); aa.setDuration(1000); // 时间设置 1s v.startAnimation(aa); // 把按钮设置改动画*/ // 用配置文件来实现 v.startAnimation(AnimationUtils.loadAnimation(v.getContext(), R.anim.aa)); } }); System.out.println("onCreate"); } }
2. 旋转动画
视图界面 -- 一个按钮
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:weightSum="1"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="New Button" android:id="@+id/btnClickMe" android:layout_centerVertical="true" android:layout_alignParentStart="true" /> </RelativeLayout>
①直接使用java代码
public class MainActivity extends Activity{ private RotateAnimation ra = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //ra = new RotateAnimation(0, 360); // 默认从0,0 旋转360度 //ra = new RotateAnimation(0, 360 ,10, 10); // 从 10,10 旋转360度 ra = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); ra.setDuration(1000); //时间 1s this.findViewById(R.id.btnClickMe).setOnClickListener(new View.OnClickListener() { //按钮点击监听 @Override public void onClick(View v) { v.startAnimation(ra); //用代码 } }); System.out.println("onCreate"); } }
② 用配置文件 xml /res/anim/rotate.xml
<?xml version="1.0" encoding="utf-8"?> <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:fromDegrees="0" android:toDegrees="360" android:duration="1000" android:pivotX="50%" android:pivotY="50%" />
java代码
public class MainActivity extends Activity{ private RotateAnimation ra = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.findViewById(R.id.btnClickMe).setOnClickListener(new View.OnClickListener() { //按钮点击监听 @Override public void onClick(View v) { //用xml文件 v.startAnimation(AnimationUtils.loadAnimation(v.getContext(), R.anim.rotate)); } }); System.out.println("onCreate"); } }
3. 移动动画
视图----- 一个按钮
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:weightSum="1"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Button" android:id="@+id/btnClickMe" android:layout_centerVertical="true" android:layout_centerHorizontal="true" /> </RelativeLayout>
① 直接用java
public class MainActivity extends Activity{ private TranslateAnimation ta = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ta = new TranslateAnimation(0, 200, 0, 200); // x + 200 y+200 ta.setDuration(1000);// 1s 时间 this.findViewById(R.id.btnClickMe).setOnClickListener(new View.OnClickListener() { //按钮点击监听 @Override public void onClick(View v) { v.startAnimation(ta); } }); System.out.println("onCreate"); } }
② anim/translate.xml 文件
<?xml version="1.0" encoding="utf-8"?> <translate android:fromXDelta="0" android:fromYDelta="0" android:toXDelta="200" android:toYDelta="200" android:duration="1000" xmlns:android="http://schemas.android.com/apk/res/android" />
public class MainActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.findViewById(R.id.btnClickMe).setOnClickListener(new View.OnClickListener() { //按钮点击监听 @Override public void onClick(View v) { // 调用配置文件 执行 v.startAnimation(AnimationUtils.loadAnimation(v.getContext(), R.anim.translate)); } }); System.out.println("onCreate"); } }
标签:
原文地址:http://www.cnblogs.com/shaoshao/p/5898206.html