亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

讓一個ImageView在屏幕中來回運動

系統(tǒng) 2031 0
已不再推薦補間動畫,請使用屬性動畫;
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(搖頭)效果
///////////////////////////////////////////////////////////////////

讓一個ImageView在屏幕中來回運動

    
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;基于此進行計算。
在大家學習的過程中建議大家多看英文的東西,這樣會事半功倍的。

讓一個ImageView在屏幕中來回運動


更多文章、技術交流、商務合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

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

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 91在线网| 天天碰天天操 | 亚洲精品午夜久久久伊人 | 91成人爽a毛片一区二区 | 精品在线免费视频 | 国产精品免费福利 | 成人a毛片久久免费播放 | 91成人免费福利网站在线 | 欧美一二三区 | 毛片免费永久不卡视频观看 | 乱人伦99久久 | 美女狠狠干| 亚洲国产成人久久精品hezyo | 免费一区二区三区久久 | 四虎成人免费网址在线 | 一级肉体毛片视频免费看看 | 日韩成a人片在线观看日本 日韩成人 | 伊在人亚洲香蕉精品区麻豆 | 日韩欧美一区二区在线 | 97中文字幕在线观看 | 天堂精品视频 | 毛片免费永久不卡视频观看 | 欧美综合色另类图片区 | 国产精品久久久久久亚洲伦理 | 我不卡老子影院午夜伦我不卡四虎 | 四虎免费在线播放 | 中文字幕日本一区久久 | 五月国产综合视频在线观看 | 黄片毛片 | 国产日韩一区二区三区 | 国产精品久久久久久一区二区三区 | 在线免费看a | 久久久久久岛国免费网站 | 久久婷婷五夜综合色频 | 成人在免费观看视频国产 | 欧美成人国产一区二区 | 中文在线播放 | 中文有码第一页 | 国产精品不卡 | 日本aaaa | 日韩欧美一区二区三区四区 |