你好,游客 登录 注册 搜索
背景:
阅读新闻

Android 自定义Button 及Animation的基本使用

[日期:2012-02-25] 来源:Linux社区  作者:zhwycan [字体:大 中 小]

具体代码如下:

淡入淡出

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:Android="http://schemas.android.com/apk/res/android" >  
  3.   
  4.     <alpha  
  5.         android:duration="3000"  
  6.         android:fromAlpha="0.1"  
  7.         android:toAlpha="1.0" />  
  8.   
  9. </set>  

旋转效果

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.   
  4.     <rotate  
  5.         android:duration="3000"  
  6.         android:fromDegrees="0"  
  7.         android:pivotX="50%"  
  8.         android:pivotY="50%"  
  9.         android:toDegrees="+360" />  
  10.   
  11. </set>  

缩放效果

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.   
  4.     <scale  
  5.         android:duration="3000"  
  6.         android:fillAfter="false"  
  7.         android:fromXScale="0.0"  
  8.         android:fromYScale="0.0"  
  9.         android:interpolator="@android:anim/accelerate_decelerate_interpolator"  
  10.         android:pivotX="50%"  
  11.         android:pivotY="50%"  
  12.         android:toXScale="1"  
  13.         android:toYScale="1" />  
  14.   
  15. </set>  

移动效果

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.   
  4.     <translate  
  5.         android:duration="3000"  
  6.         android:fromXDelta="0"  
  7.         android:fromYDelta="0"  
  8.         android:interpolator="@android:anim/accelerate_interpolator"  
  9.         android:toXDelta="100"  
  10.         android:toYDelta="0" />  
  11.   
  12. </set>  

这是使用的xml的形式进行Animation的设定,还可以在java代码中直接写出,而且一个文件可以有多种效果,多种结合方式,更多设置选项还有各子的意义这里就不一一列举出来,自己在网站上都能找到,最后是Activity的代码:

  1. package sina.CreAmazing.muti_button;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.view.View.OnClickListener;  
  7. import android.view.animation.AnimationUtils;  
  8. import android.widget.Button;  
  9.   
  10. public class MutiButtonActivity extends Activity {  
  11.     /** Called when the activity is first created. */  
  12.       
  13.     private Button btStart;  
  14.     private Button bt1;  
  15.     private Button bt2;  
  16.     private Button bt3;  
  17.     private Button bt4;  
  18.       
  19.     @Override  
  20.     public void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);  
  22.         setContentView(R.layout.main);  
  23.           
  24.         findViews();  
  25.         btStart.setOnClickListener(new OnClickListener() {  
  26.               
  27.             @Override  
  28.             public void onClick(View v) {  
  29.                 // TODO Auto-generated method stub   
  30.                 startAnimation();  
  31.             }  
  32.         });  
  33.     }  
  34.   
  35.       
  36.   
  37.     private void findViews() {  
  38.         // TODO Auto-generated method stub   
  39.         btStart = (Button) findViewById(R.id.bt_start);  
  40.         bt1 = (Button) findViewById(R.id.bt1);  
  41.         bt2 = (Button) findViewById(R.id.bt2);  
  42.         bt3 = (Button) findViewById(R.id.bt3);  
  43.         bt4 = (Button) findViewById(R.id.bt4);  
  44.     }  
  45.       
  46.       
  47.       
  48.     private void startAnimation(){  
  49.         bt1.startAnimation(AnimationUtils.loadAnimation(this, R.anim.bt_alpha));  
  50.         bt2.startAnimation(AnimationUtils.loadAnimation(this, R.anim.bt_rotate));  
  51.         bt3.startAnimation(AnimationUtils.loadAnimation(this, R.anim.bt_scale));  
  52.         bt4.startAnimation(AnimationUtils.loadAnimation(this, R.anim.bt_translate));  
  53.     }  
  54. }  

其实只要巧妙的利用Animation就能为应用作出很多很酷效果,在这篇讲到。见 http://www.linuxidc.com/Linux/2012-02/55175.htm

linux
相关资讯       Android源码 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款