Android补间动画笔记
生活随笔
收集整理的這篇文章主要介紹了
Android补间动画笔记
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
布局文件:
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_my_anim"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.jogger.propertiesanim.MyAnim">
<ImageView
android:id="@+id/iv_clock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/t_clock"/>
<ImageView
android:id="@+id/iv_bettery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/t_bettery"/>
<ImageView
android:id="@+id/iv_low_bettery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/iv_clock"
android:layout_centerHorizontal="true"
android:src="@drawable/t_low_bettery"/>
<ImageView
android:id="@+id/iv_next"
android:layout_centerHorizontal="true"
android:src="@drawable/t_next"
android:layout_below="@id/iv_clock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageView
android:id="@+id/iv_msg"
android:layout_centerHorizontal="true"
android:src="@drawable/t_first_msg"
android:layout_below="@id/iv_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
方法一:動(dòng)態(tài)注冊(cè) public class MyAnim extends AppCompatActivity {
private ImageView iv_low_bettery, iv_next, iv_bettery;
/**
* 補(bǔ)間動(dòng)畫案例(縮放,平移,旋轉(zhuǎn))
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_anim);
iv_low_bettery = (ImageView) findViewById(R.id.iv_low_bettery);
iv_next = (ImageView) findViewById(R.id.iv_next);
iv_bettery = (ImageView) findViewById(R.id.iv_bettery);
//縮放動(dòng)畫
/**
* fromX:開始縮放的X軸倍數(shù)。如1.0f:本身大小;如2.0f:從自己兩倍開始
toX:結(jié)束縮放的X軸倍數(shù),體現(xiàn)在結(jié)束時(shí)的寬度上
fromY:始縮放的Y軸倍數(shù)。
toY:結(jié)束縮放的Y軸倍數(shù)。
pivotXType:X軸縮放中心點(diǎn)類型;可選值有:
Animation.RELATIVE_TO_SELF相對(duì)自己--常用
Animation.RELATIVE_TO_PARENT相對(duì)父窗體
Animation.ABSOLUTE 絕對(duì)的---不常用
pivotXValue:在pivotXType的基礎(chǔ)上,X軸縮放中心的位置。如:0.5f:縮放中心就在控件的一半的位置。如果是0.0f,則會(huì)在控件本身的左邊位置
pivotYType:X軸縮放中心點(diǎn)類型;同上 ...
pivotYValue:在pivotYType的基礎(chǔ)上,Y軸縮放中心的位置。
*/
ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 1.5f, 1.0f, 1.5f, Animation
.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
//平移動(dòng)畫
/**
* fromXType:開始平移的X軸參照位置,一般使用Animation.RELATIVE_TO_SELF
fromXValue:X軸平移的開始倍數(shù)。在fromXType的基礎(chǔ)上。
toXType:結(jié)束平移的X軸參照位置
toXValue:X軸平移的結(jié)束倍數(shù)。
fromYType:開始平移的Y軸參照位置
fromYValue:Y軸平移的開始倍數(shù)。
toYType:結(jié)束平移的Y軸參照位置
toYValue:Y軸平移的結(jié)束倍數(shù)。
*/
TranslateAnimation translateAnimation = new TranslateAnimation(Animation
.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF,
0, Animation.RELATIVE_TO_SELF, 1f);
scaleAnimation.setRepeatCount(Animation.INFINITE);//設(shè)置重復(fù)次數(shù),INFINITE為設(shè)置無限重復(fù)
translateAnimation.setRepeatCount(Animation.INFINITE);
AnimationSet set = new AnimationSet(false);
set.addAnimation(scaleAnimation);
set.addAnimation(translateAnimation);
set.setDuration(2000);//播放一輪動(dòng)畫的時(shí)間
set.setRepeatMode(Animation.REVERSE);//用于設(shè)置動(dòng)畫的重復(fù)方式,可選擇為reverse(反向)和restart(重新開始)
//set.setRepeatCount(Animation.INFINITE);//set不能直接設(shè)置無限重復(fù)
iv_low_bettery.startAnimation(set);
TranslateAnimation translateAnimationNext = new TranslateAnimation(Animation
.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF,
0, Animation.RELATIVE_TO_SELF, 1.1f);
translateAnimationNext.setDuration(2000);
translateAnimationNext.setRepeatCount(Animation.INFINITE);
translateAnimationNext.setRepeatMode(Animation.REVERSE);
iv_next.startAnimation(translateAnimationNext);
/**
* fromDegrees:開始旋轉(zhuǎn)的角度;三點(diǎn)方向?yàn)?度,六點(diǎn)方向?yàn)?0度。
toDegrees:結(jié)束旋轉(zhuǎn)的角度
pivotXType:參照縮放動(dòng)畫
pivotXValue:參照縮放動(dòng)畫
pivotYType:參照縮放動(dòng)畫
pivotYValue:參照縮放動(dòng)畫
*/
RotateAnimation rotateAnimation = new RotateAnimation(0, 360,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
rotateAnimation.setRepeatMode(Animation.REVERSE);
rotateAnimation.setRepeatCount(Animation.INFINITE);
rotateAnimation.setDuration(2000);
iv_bettery.startAnimation(rotateAnimation);
}
}
方法二:靜態(tài)注冊(cè)
在res目錄下創(chuàng)建anim文件夾,創(chuàng)建如下3個(gè)資源文件:
low_bettery_anim.xml: <set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2000"
android:repeatMode="reverse"
>
<translate
android:fromXDelta="0"
android:fromYDelta="0"
android:repeatCount="infinite"
android:toXDelta="0"
android:toYDelta="100%"></translate>
<scale
android:fromXScale="100%"
android:fromYScale="100%"
android:pivotX="50%"
android:pivotY="50"
android:repeatCount="infinite"
android:toXScale="150%"
android:toYScale="150%"></scale>
</set>
next_anim.xml <set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2000"
android:repeatMode="reverse">
<translate
android:fromXDelta="0"
android:fromYDelta="0"
android:repeatCount="infinite"
android:toXDelta="0"
android:toYDelta="100%"></translate>
</set> bettery_anim.xml: <set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2000"
android:repeatMode="reverse">
<!--pivotX 50%表示相對(duì)自身-->
<rotate
android:fromDegrees="0.5"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="infinite"
android:toDegrees="360"></rotate>
</set> 代碼界面: public class MyAnim extends AppCompatActivity {
private ImageView iv_low_bettery, iv_next, iv_bettery;
/**
* 補(bǔ)間動(dòng)畫案例(縮放,平移,旋轉(zhuǎn))
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_anim);
iv_low_bettery = (ImageView) findViewById(R.id.iv_low_bettery);
iv_next = (ImageView) findViewById(R.id.iv_next);
iv_bettery = (ImageView) findViewById(R.id.iv_bettery);
Animation low_bettery_anim = AnimationUtils.loadAnimation(this, R.anim.low_bettery_anim);
Animation next_anim = AnimationUtils.loadAnimation(this, R.anim.next_anim);
Animation bettery_anim = AnimationUtils.loadAnimation(this, R.anim.bettery_anim);
iv_next.startAnimation(next_anim);
iv_low_bettery.startAnimation(low_bettery_anim);
iv_bettery.startAnimation(bettery_anim);
}
}
效果圖:
轉(zhuǎn)載于:https://www.cnblogs.com/epilogue/p/6135593.html
總結(jié)
以上是生活随笔為你收集整理的Android补间动画笔记的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mvn filter autoconfi
- 下一篇: Android 之UID and PID