Android窗口抖动之动画实现



Android窗口抖动之动画实现

Android的窗口抖动,可以通过写xml动画配置文件实现,比如常见的微信摇一摇,其实就是通过Android动画的一些基本属性如translate,cycleInterpolator实现的。
现在写一个代码,实现一个基本的窗口view抖动的效果,简单期间,就以一个TextView为例,让其抖动。activity_main.xml代码文件:

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context="zhangphil.anim.MainActivity" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:layout_centerInParent="true"  
  11.         android:background="#e53935"  
  12.         android:id="@+id/text"  
  13.         android:text="窗口抖动  Zhang Phil @ CSDN" />  
  14.   
  15. </RelativeLayout>  

测试的activity MainActivity.Java

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package zhangphil.anim;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.Menu;  
  6. import android.view.MenuItem;  
  7. import android.view.animation.Animation;  
  8. import android.view.animation.AnimationUtils;  
  9. import android.widget.TextView;  
  10.   
  11. public class MainActivity extends Activity {  
  12.   
  13.     private TextView text;  
  14.       
  15.     @Override  
  16.     protected void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.activity_main);  
  19.           
  20.         text=(TextView) findViewById(R.id.text);  
  21.     }  
  22.   
  23.     @Override  
  24.     public boolean onCreateOptionsMenu(Menu menu) {  
  25.         getMenuInflater().inflate(R.menu.main, menu);  
  26.         return true;  
  27.     }  
  28.   
  29.     @Override  
  30.     public boolean onOptionsItemSelected(MenuItem item) {  
  31.         int id = item.getItemId();  
  32.         if (id == R.id.action_anim) {  
  33.               
  34.             Animation anim = AnimationUtils.loadAnimation(MainActivity.this, R.anim.myanim);  
  35.             text.startAnimation(anim);  
  36.               
  37.             return true;  
  38.         }  
  39.         return super.onOptionsItemSelected(item);  
  40.     }  
  41. }  


TextView所在的布局文件及测试的Activity本身可以写的很简单,复杂的地方在res/anim目录下所写的那些动画属性配置文件。

res/anim目录下的myanim.xml文件代码:

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:interpolator="@anim/cycle" >  
  4.   
  5.     <translate  
  6.         android:duration="300"  
  7.         android:fromXDelta="0"  
  8.         android:fromYDelta="0"  
  9.         android:toXDelta="-10"  
  10.         android:toYDelta="-10" />  
  11.     <translate  
  12.         android:duration="300"  
  13.         android:fromXDelta="0"  
  14.         android:fromYDelta="0"  
  15.         android:startOffset="300"  
  16.         android:toXDelta="10"  
  17.         android:toYDelta="-10" />  
  18.     <translate  
  19.         android:duration="300"  
  20.         android:fromXDelta="0"  
  21.         android:fromYDelta="0"  
  22.         android:startOffset="600"  
  23.         android:toXDelta="-10"  
  24.         android:toYDelta="10" />  
  25.     <translate  
  26.         android:duration="300"  
  27.         android:fromXDelta="0"  
  28.         android:fromYDelta="0"  
  29.         android:startOffset="900"  
  30.         android:toXDelta="10"  
  31.         android:toYDelta="10" />  
  32. </set>  

myanim.xml代码文件需要用到的cycleInterpolator配置文件cycle.xml代码文件:

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:cycles="2" />  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值