黄色网页视频 I 影音先锋日日狠狠久久 I 秋霞午夜毛片 I 秋霞一二三区 I 国产成人片无码视频 I 国产 精品 自在自线 I av免费观看网站 I 日本精品久久久久中文字幕5 I 91看视频 I 看全色黄大色黄女片18 I 精品不卡一区 I 亚洲最新精品 I 欧美 激情 在线 I 人妻少妇精品久久 I 国产99视频精品免费专区 I 欧美影院 I 欧美精品在欧美一区二区少妇 I av大片网站 I 国产精品黄色片 I 888久久 I 狠狠干最新 I 看看黄色一级片 I 黄色精品久久 I 三级av在线 I 69色综合 I 国产日韩欧美91 I 亚洲精品偷拍 I 激情小说亚洲图片 I 久久国产视频精品 I 国产综合精品一区二区三区 I 色婷婷国产 I 最新成人av在线 I 国产私拍精品 I 日韩成人影音 I 日日夜夜天天综合

讓一個(gè)ImageView在屏幕中來回運(yùn)動(dòng)

系統(tǒng) 2502 0
已不再推薦補(bǔ)間動(dòng)畫,請(qǐng)使用屬性動(dòng)畫;
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);

  

點(diǎn)擊Imageview啟動(dòng)動(dòng)畫:
    
 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;
}
}

  

添加動(dòng)畫監(jiān)聽,在AnimationEnd的時(shí)候切換動(dòng)畫:
    
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一開始的時(shí)候在屏幕右邊:
    
<?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>

  

本實(shí)例只是實(shí)現(xiàn)連續(xù)動(dòng)畫的一種簡單辦法,針對(duì)本例子應(yīng)該還有更簡單的方法.


///////////////////////////////////////////////////////////////////
// 下面是anroid Api demo自帶的一個(gè)shake(搖頭)效果
///////////////////////////////////////////////////////////////////

讓一個(gè)ImageView在屏幕中來回運(yùn)動(dòng)

    
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混合型動(dòng)畫AnimationSet
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
 
    // 要使用findViewById, 一定要使用layout / *.xml 做為使用者介面
    setContentView( R.layout.main );
 
    // 取得UI 介面中的View 物件
    // 取得View 物件后,再透過轉(zhuǎn)換成實(shí)際的物件
    ImageView ivPic = (ImageView)this.findViewById(R.id.widget10);  //底圖
    ImageView iv = (ImageView)this.findViewById(R.id.widget28);
 
    // 設(shè)定ImageView 的圖片來源
    ivPic.setImageResource( R.drawable.a2 );
    iv.setImageResource( R.drawable.icon );
 
    // 透明度動(dòng)畫設(shè)定(startAlpha, endAlpha)
    Animation am1 = new AlphaAnimation ( 1, 0 );
    // 動(dòng)畫開始到結(jié)束的執(zhí)行時(shí)間(1000 = 1 秒)
    am1. setDuration ( 2000 );
    // 動(dòng)畫重復(fù)次數(shù)(-1 表示一直重復(fù))
    am1. setRepeatCount ( -1 );
 
    // 旋轉(zhuǎn)動(dòng)畫設(shè)定(startAngle, endAngle, rotateX, rotateY)
    Animation am2 = new RotateAnimation ( 0, 360, 30, 30 );
    // 動(dòng)畫開始到結(jié)束的執(zhí)行時(shí)間(1000 = 1 秒)
    am2. setDuration ( 2000 );
    // 動(dòng)畫重復(fù)次數(shù)(-1 表示一直重復(fù))
    am2. setRepeatCount ( -1 );
 
    // 動(dòng)畫集合
    AnimationSet am = new AnimationSet ( false );
    am. addAnimation ( am1 );
    am. addAnimation ( am2 );
 
    // 圖片配置動(dòng)畫
    iv. setAnimation (am);
 
    // 動(dòng)畫開始
    am. startNow ();
}
  



附:android中所有的interpolator
android平臺(tái)提供了如下的interpolater
AccelerateDecelerateInterpolator
AccelerateInterpolator
CycleInterpolator
DecelerateInterpolator
LinearInterpolator
AnticipateInterpolator
AnticipateOvershootInterpolator
BounceInterpolator
OvershootInterpolator
這些interpolater實(shí)現(xiàn)的是
android.view.animation.Interpolator接口
該接口只有一個(gè)方法
getInterpolation(float num)
也就是不同的interpolater計(jì)算出的值不一樣,比如:
public float getInterpolation(float input)
{
if (mFactor == 1.0f)
{
return (float)(input * input);
}
else
{
return (float)Math.pow(input, 2 * mFactor);
}
}
當(dāng)輸入的值為1時(shí)候,那么返回值是平方,否則返回立方的值。
其實(shí)interpolator是按照這種方式計(jì)算的
This interpolator’s goal is to provide a multiplication factor given a time interval
based on a hyperbolic curve。
基于雙曲線計(jì)算時(shí)間的間隔,進(jìn)行相關(guān)的動(dòng)畫處理。
比如常用的accelerator_interpolator其xml定義的結(jié)構(gòu)為:
<accelerator xmlns:android="http://schemas.android.com/apk/res/android"
factor="1" /> 其乘數(shù)為1;基于此進(jìn)行計(jì)算。
在大家學(xué)習(xí)的過程中建議大家多看英文的東西,這樣會(huì)事半功倍的。

讓一個(gè)ImageView在屏幕中來回運(yùn)動(dòng)


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

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

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

【本文對(duì)您有幫助就好】

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

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論