安卓常用的基本动画代码

安卓常用的动画


translationX and translationY
rotation, rotationX, and rotationY
scaleX and scaleY
pivotX and pivotY
x and y
alpha

主要是上诉几种代码效果,直接贴代码吧:

[java]  view plain copy
  1. package com.example.test;  
  2.   
  3. import android.animation.Animator;  
  4. import android.animation.Animator.AnimatorListener;  
  5. import android.animation.ObjectAnimator;  
  6. import android.animation.PropertyValuesHolder;  
  7. import android.app.Activity;  
  8. import android.os.Bundle;  
  9. import android.util.DisplayMetrics;  
  10. import android.view.Menu;  
  11. import android.view.MenuItem;  
  12. import android.view.View;  
  13. import android.widget.ImageView;  
  14. import android.widget.Toast;  
  15.   
  16. public class MainActivity extends Activity {  
  17.   
  18.     private ImageView iv;  
  19.     private float mScreenHeight;  
  20.   
  21.     @Override  
  22.     protected void onCreate(Bundle savedInstanceState) {  
  23.         super.onCreate(savedInstanceState);  
  24.         setContentView(R.layout.main_activity);  
  25.         iv = (ImageView) findViewById(R.id.iv);  
  26.         DisplayMetrics outMetrics = new DisplayMetrics();  
  27.         getWindowManager().getDefaultDisplay().getMetrics(outMetrics);  
  28.         mScreenHeight = outMetrics.heightPixels;  
  29.     }  
  30.   
  31.     public void click(View view) {  
  32.   
  33.         // 平移动画  
  34.         ObjectAnimator transX = ObjectAnimator.ofFloat(iv, "translationX", 0f,  
  35.                 300f);  
  36.         transX.setDuration(2000);  
  37.         transX.setRepeatCount(5);  
  38.         transX.setRepeatMode(ObjectAnimator.RESTART);  
  39.         transX.start();  
  40.   
  41.         // 平移动画  
  42.         ObjectAnimator transY = ObjectAnimator.ofFloat(iv, "translationY", 0f,  
  43.                 300f);  
  44.         transY.setDuration(1000);  
  45.         transY.setRepeatCount(3);  
  46.         transY.setRepeatMode(ObjectAnimator.RESTART);  
  47.         transY.start();  
  48.   
  49.         // 旋转动画  
  50.         ObjectAnimator rotation = ObjectAnimator.ofFloat(iv, "rotation", 0f,  
  51.                 360f);  
  52.         rotation.setDuration(1000);  
  53.         rotation.setRepeatCount(7);  
  54.         rotation.setRepeatMode(ObjectAnimator.RESTART);  
  55.         rotation.start();  
  56.   
  57.         // 缩放  
  58.         ObjectAnimator scaleX = ObjectAnimator.ofFloat(iv, "scaleX", 0f, 1.6f);  
  59.         scaleX.setDuration(1000);  
  60.         scaleX.setRepeatCount(5);  
  61.         scaleX.setRepeatMode(ObjectAnimator.RESTART);  
  62.         scaleX.start();  
  63.   
  64.         // 缩放动画  
  65.         ObjectAnimator scaleY = ObjectAnimator.ofFloat(iv, "scaleY", 0f, 1.6f);  
  66.         scaleY.setDuration(1000);  
  67.         scaleY.setRepeatCount(5);  
  68.         scaleY.setRepeatMode(ObjectAnimator.RESTART);  
  69.         scaleY.start();  
  70.   
  71.         // 缩放动画  
  72.         ObjectAnimator alpha = ObjectAnimator.ofFloat(iv, "alpha", 0f, 1f);  
  73.         alpha.setDuration(1000);  
  74.         alpha.setRepeatCount(5);  
  75.         alpha.setRepeatMode(ObjectAnimator.RESTART);  
  76.         alpha.start();  
  77.   
  78.         transX.addListener(new AnimatorListener() {  
  79.   
  80.             @Override  
  81.             public void onAnimationStart(Animator animation) {  
  82.   
  83.             }  
  84.   
  85.             @Override  
  86.             public void onAnimationRepeat(Animator animation) {  
  87.   
  88.             }  
  89.   
  90.             @Override  
  91.             public void onAnimationEnd(Animator animation) {  
  92.                 Toast.makeText(MainActivity.this"终于结束了...", Toast.LENGTH_LONG)  
  93.                         .show();  
  94.             }  
  95.   
  96.             @Override  
  97.             public void onAnimationCancel(Animator animation) {  
  98.   
  99.             }  
  100.         });  
  101.     }  
  102.   
  103.     // 属性动画  
  104.     public void click1(View view) {  
  105.         PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("alpha", 1f,  
  106.                 0f, 1f);  
  107.         // 属性动画Y值  
  108.         PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("y"0,  
  109.                 mScreenHeight / 20);  
  110.         // 属性动画X值  
  111.         PropertyValuesHolder pvhXx = PropertyValuesHolder.ofFloat("x"0,  
  112.                 mScreenHeight / 20);  
  113.         ObjectAnimator.ofPropertyValuesHolder(iv, pvhX, pvhY, pvhXx)  
  114.                 .setDuration(1000).start();  
  115.     }  
  116.   
  117.     @Override  
  118.     public boolean onCreateOptionsMenu(Menu menu) {  
  119.         // Inflate the menu; this adds items to the action bar if it is present.  
  120.         getMenuInflater().inflate(R.menu.main, menu);  
  121.         return true;  
  122.     }  
  123.   
  124.     @Override  
  125.     public boolean onOptionsItemSelected(MenuItem item) {  
  126.         int id = item.getItemId();  
  127.         if (id == R.id.action_settings) {  
  128.             return true;  
  129.         }  
  130.         return super.onOptionsItemSelected(item);  
  131.     }  
  132.   
  133. }  

界面代码:

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/RelativeLayout1"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <ImageView  
  9.         android:id="@+id/iv"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:layout_alignParentLeft="true"  
  13.         android:layout_alignParentTop="true"  
  14.         android:src="@drawable/ic_launcher" />  
  15.   
  16.     <LinearLayout  
  17.         android:layout_width="wrap_content"  
  18.         android:layout_height="wrap_content"  
  19.         android:layout_alignParentBottom="true"  
  20.         android:layout_centerInParent="true" >  
  21.   
  22.         <Button  
  23.             android:id="@+id/but"  
  24.             android:layout_width="wrap_content"  
  25.             android:layout_height="wrap_content"  
  26.             android:onClick="click"  
  27.             android:text="动画" />  
  28.   
  29.         <Button  
  30.             android:id="@+id/but1"  
  31.             android:layout_width="wrap_content"  
  32.             android:layout_height="wrap_content"  
  33.             android:onClick="click1"  
  34.             android:text="动画2" />  
  35.     </LinearLayout>  
  36.   
  37. </RelativeLayout>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值