1、跟上一遍部落格一樣,請參考Android動畫效果Animation之Tween實現簡單動畫;
接下來直接java代碼實現如何?動畫:
定義
private Animation animation_alpha,animation_scale,animation_translate,animation_rotate;private AnimationSet animationSet;
分別是透明度動畫、旋轉動畫、尺寸伸縮動畫、移動動畫
具體實現代碼如下:
private void initAnimation() {//透明度控制動畫效果 alphaanimation_alpha=new AlphaAnimation(0.1f,1.0f);//第一個參數fromAlpha為 動畫開始時候透明度//第二個參數toAlpha為 動畫結束時候透明度animation_alpha.setRepeatCount(-1);//設定迴圈animation_alpha.setDuration(5000);//設定時間期間為 5000毫秒// 旋轉效果rotateanimation_rotate = new RotateAnimation(0, -720,RotateAnimation.RELATIVE_TO_SELF, 0.5f,RotateAnimation.RELATIVE_TO_SELF, 0.5f); //第一個參數fromDegrees為動畫起始時的旋轉角度 //第二個參數toDegrees為動畫旋轉到的角度 //第三個參數pivotXType為動畫在X軸相對於物件位置類型 //第四個參數pivotXValue為動畫相對於物件的X座標的開始位置 //第五個參數pivotXType為動畫在Y軸相對於物件位置類型 //第六個參數pivotYValue為動畫相對於物件的Y座標的開始位置animation_rotate.setRepeatCount(-1);animation_rotate.setDuration(5000);//設定時間期間為 5000毫秒//尺寸伸縮動畫效果 scaleanimation_scale=new ScaleAnimation(0.1f,3.0f,0.1f,3.0f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);//第一個參數fromX為動畫起始時 X座標上的伸縮尺寸 //第二個參數toX為動畫結束時 X座標上的伸縮尺寸 //第三個參數fromY為動畫起始時Y座標上的伸縮尺寸 //第四個參數toY為動畫結束時Y座標上的伸縮尺寸 /*說明: 以上四種屬性值 0.0表示收縮到沒有 1.0表示正常無伸縮 值小於1.0表示收縮 值大於1.0表示放大*///第五個參數pivotXType為動畫在X軸相對於物件位置類型 //第六個參數pivotXValue為動畫相對於物件的X座標的開始位置//第七個參數pivotXType為動畫在Y軸相對於物件位置類型 //第八個參數pivotYValue為動畫相對於物件的Y座標的開始位置animation_scale.setRepeatCount(-1);animation_scale.setDuration(5000);//設定時間期間為 5000毫秒//移動動畫效果translateanimation_translate=new TranslateAnimation(-20f,300f,-20f,300f);//第一個參數fromXDelta為動畫起始時 X座標上的移動位置 //第二個參數toXDelta為動畫結束時 X座標上的移動位置 //第三個參數fromYDelta為動畫起始時Y座標上的移動位置 //第三個參數toYDelta為動畫結束時Y座標上的移動位置 animation_translate.setRepeatCount(-1);//設定動畫執行多少次,如果是-1的話就是一直重複animation_translate.setDuration(5000);//設定時間期間為 5000毫秒animationSet=new AnimationSet(true);animationSet.addAnimation(animation_alpha);//透明度animationSet.addAnimation(animation_rotate);//旋轉animationSet.addAnimation(animation_scale);//尺寸伸縮animationSet.addAnimation(animation_translate);//移動image.startAnimation(animationSet);//開始播放}AnimationSet這個類是:
Represents a group of Animations that should be played together. The transformation of each individual animation are composed together into a single transform. If AnimationSet sets any properties that its children also set (for example, duration or fillBefore),
the values of AnimationSet override the child values.
意思是:代表一群動畫應該一同玩耍。轉變是由每個個體的動畫在一起,成為一個單一的變換。如果AnimationSet設定任何屬性,其子女也設定(例如,期間或fillBefore)的值AnimationSet推翻這孩子的價值觀。