已不再推薦補間動畫,請使用屬性動畫;
http://blog.csdn.net/guolin_blog/article/details/43536355
http://blog.csdn.net/guolin_blog/article/details/43816093
onCreate()中:
點擊Imageview啟動動畫:
添加動畫監(jiān)聽,在AnimationEnd的時候切換動畫:
布局,讓ImageView一開始的時候在屏幕右邊:
本實例只是實現(xiàn)連續(xù)動畫的一種簡單辦法,針對本例子應該還有更簡單的方法.
///////////////////////////////////////////////////////////////////
// 下面是anroid Api demo自帶的一個shake(搖頭)效果
///////////////////////////////////////////////////////////////////
anim/shake.xml
anim/cycle_7.xml
Android混合型動畫AnimationSet
附:android中所有的interpolator
android平臺提供了如下的interpolater
AccelerateDecelerateInterpolator
AccelerateInterpolator
CycleInterpolator
DecelerateInterpolator
LinearInterpolator
AnticipateInterpolator
AnticipateOvershootInterpolator
BounceInterpolator
OvershootInterpolator
這些interpolater實現(xiàn)的是
android.view.animation.Interpolator接口
該接口只有一個方法
getInterpolation(float num)
也就是不同的interpolater計算出的值不一樣,比如:
public float getInterpolation(float input)
{
if (mFactor == 1.0f)
{
return (float)(input * input);
}
else
{
return (float)Math.pow(input, 2 * mFactor);
}
}
當輸入的值為1時候,那么返回值是平方,否則返回立方的值。
其實interpolator是按照這種方式計算的
This interpolator’s goal is to provide a multiplication factor given a time interval
based on a hyperbolic curve。
基于雙曲線計算時間的間隔,進行相關的動畫處理。
比如常用的accelerator_interpolator其xml定義的結構為:
<accelerator xmlns:android="http://schemas.android.com/apk/res/android"
factor="1" /> 其乘數(shù)為1;基于此進行計算。
在大家學習的過程中建議大家多看英文的東西,這樣會事半功倍的。
http://blog.csdn.net/guolin_blog/article/details/43536355
http://blog.csdn.net/guolin_blog/article/details/43816093

onCreate()中:
iv=(ImageView)this.findViewById(R.id.iv); iv.setTag("toRight"); iv.setOnClickListener(listener); ani_0 = new TranslateAnimation( Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, -1.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f); ani_1= new TranslateAnimation( Animation.RELATIVE_TO_PARENT, -1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f); ani_0.setInterpolator(new AccelerateDecelerateInterpolator()); ani_0.setDuration(1000); ani_0.setFillEnabled(true); ani_0.setFillAfter(true); ani_0.setAnimationListener(animationListener); ani_1.setInterpolator(new AccelerateDecelerateInterpolator()); ani_1.setDuration(1000); ani_1.setFillEnabled(true); ani_1.setFillAfter(true); ani_1.setAnimationListener(animationListener);
點擊Imageview啟動動畫:
OnClickListener listener=new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.iv: iv.startAnimation(ani_0); break; } }
添加動畫監(jiān)聽,在AnimationEnd的時候切換動畫:
final AnimationListener animationListener = new AnimationListener() { @Override public void onAnimationEnd(Animation animation) { if (animation == ani_0) { iv.startAnimation(ani_1); } if (animation == ani_1) { iv.startAnimation(ani_0); } } @Override public void onAnimationRepeat(Animation animation) { // TODO Auto-generated method stub } @Override public void onAnimationStart(Animation animation) { // TODO Auto-generated method stub } };
布局,讓ImageView一開始的時候在屏幕右邊:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ImageView android:id="@+id/iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/search2" android:scaleType="fitCenter" android:layout_gravity="right" /> </LinearLayout>
本實例只是實現(xiàn)連續(xù)動畫的一種簡單辦法,針對本例子應該還有更簡單的方法.
///////////////////////////////////////////////////////////////////
// 下面是anroid Api demo自帶的一個shake(搖頭)效果
///////////////////////////////////////////////////////////////////

import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.animation.Animation; import android.view.animation.AnimationUtils; public class Animation1 extends Activity implements View.OnClickListener { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.animation_1); View loginButton = findViewById(R.id.login); loginButton.setOnClickListener(this); } public void onClick(View v) { Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake); findViewById(R.id.pw).startAnimation(shake); } }
anim/shake.xml
<?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:fromXDelta="0" android:toXDelta="10" android:duration="1000" android:interpolator="@anim/cycle_7" />
anim/cycle_7.xml
<?xml version="1.0" encoding="utf-8"?> <cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android" android:cycles="7" />
Android混合型動畫AnimationSet
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 要使用findViewById, 一定要使用layout / *.xml 做為使用者介面 setContentView( R.layout.main ); // 取得UI 介面中的View 物件 // 取得View 物件后,再透過轉(zhuǎn)換成實際的物件 ImageView ivPic = (ImageView)this.findViewById(R.id.widget10); //底圖 ImageView iv = (ImageView)this.findViewById(R.id.widget28); // 設定ImageView 的圖片來源 ivPic.setImageResource( R.drawable.a2 ); iv.setImageResource( R.drawable.icon ); // 透明度動畫設定(startAlpha, endAlpha) Animation am1 = new AlphaAnimation ( 1, 0 ); // 動畫開始到結束的執(zhí)行時間(1000 = 1 秒) am1. setDuration ( 2000 ); // 動畫重復次數(shù)(-1 表示一直重復) am1. setRepeatCount ( -1 ); // 旋轉(zhuǎn)動畫設定(startAngle, endAngle, rotateX, rotateY) Animation am2 = new RotateAnimation ( 0, 360, 30, 30 ); // 動畫開始到結束的執(zhí)行時間(1000 = 1 秒) am2. setDuration ( 2000 ); // 動畫重復次數(shù)(-1 表示一直重復) am2. setRepeatCount ( -1 ); // 動畫集合 AnimationSet am = new AnimationSet ( false ); am. addAnimation ( am1 ); am. addAnimation ( am2 ); // 圖片配置動畫 iv. setAnimation (am); // 動畫開始 am. startNow (); }
附:android中所有的interpolator
android平臺提供了如下的interpolater
AccelerateDecelerateInterpolator
AccelerateInterpolator
CycleInterpolator
DecelerateInterpolator
LinearInterpolator
AnticipateInterpolator
AnticipateOvershootInterpolator
BounceInterpolator
OvershootInterpolator
這些interpolater實現(xiàn)的是
android.view.animation.Interpolator接口
該接口只有一個方法
getInterpolation(float num)
也就是不同的interpolater計算出的值不一樣,比如:
public float getInterpolation(float input)
{
if (mFactor == 1.0f)
{
return (float)(input * input);
}
else
{
return (float)Math.pow(input, 2 * mFactor);
}
}
當輸入的值為1時候,那么返回值是平方,否則返回立方的值。
其實interpolator是按照這種方式計算的
This interpolator’s goal is to provide a multiplication factor given a time interval
based on a hyperbolic curve。
基于雙曲線計算時間的間隔,進行相關的動畫處理。
比如常用的accelerator_interpolator其xml定義的結構為:
<accelerator xmlns:android="http://schemas.android.com/apk/res/android"
factor="1" /> 其乘數(shù)為1;基于此進行計算。
在大家學習的過程中建議大家多看英文的東西,這樣會事半功倍的。
更多文章、技術交流、商務合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
