一浪感、Animation 動(dòng)畫屬性
動(dòng)畫相關(guān)的屬性:SET屬性
名稱 | 屬性 | 備注 |
---|---|---|
android:shareInterpolator | 是否共享插入器 | 共享時(shí),四個(gè)子節(jié)點(diǎn)都用一個(gè)插入器 |
android:interpolator | 指定一個(gè)動(dòng)畫的插入器 | 使用系統(tǒng)資源 |
android:fillEnabled | 當(dāng)設(shè)置為true時(shí)等孵,fillAfter和fillBefroe將會(huì)都為true,此時(shí)會(huì)忽略fillBefore 和fillAfter兩種屬性 | |
android:fillAfter | 該動(dòng)畫轉(zhuǎn)化是否在動(dòng)畫結(jié)束后被應(yīng)用 | boolean |
android:fillBefore | 該動(dòng)畫轉(zhuǎn)化是否在動(dòng)畫開始前被應(yīng)用 | boolean |
android:repeatMode | 重復(fù)模式 | "restart" =從頭開始 或者 "reverse"=從末尾開始 |
android:repeatCount | 重復(fù)次數(shù) | integer -1為無限循環(huán) |
android:duration | 動(dòng)畫持續(xù)時(shí)間 | integer |
android:startOffset | 動(dòng)畫時(shí)間間隔(動(dòng)畫執(zhí)行前停留時(shí)間) | long |
android:zAdjustment | 定義動(dòng)畫z order的變換 | [normal] or [top] or [bottom] |
android:detachWallpaper | 未知 | boolean |
二蹂空、Animation 動(dòng)畫類型
Android的animation由四種類型組成:
XML中
名稱 | 作用 |
---|---|
alph | 漸變透明度動(dòng)畫效果 |
scale | 漸變尺寸伸縮動(dòng)畫效果 |
translate | 畫面轉(zhuǎn)換位置移動(dòng)動(dòng)畫效果 |
rotate | 畫面轉(zhuǎn)移旋轉(zhuǎn)動(dòng)畫效果 |
JavaCode中
名稱 | 作用 |
---|---|
AlphaAnimation | 漸變透明度動(dòng)畫效果 |
ScaleAnimation | 漸變尺寸伸縮動(dòng)畫效果 |
TranslateAnimation | 畫面轉(zhuǎn)換位置移動(dòng)動(dòng)畫效果 |
RotateAnimation | 畫面轉(zhuǎn)移旋轉(zhuǎn)動(dòng)畫效果 |
三俯萌、Android動(dòng)畫模式
Animation主要有兩種動(dòng)畫模式:
一種是tweened animation(漸變動(dòng)畫)
XML中 | JavaCode |
---|---|
alpha | AlphaAnimation |
scale | ScaleAnimation |
一種是frame by frame(畫面轉(zhuǎn)換動(dòng)畫)
XML中 | JavaCode |
---|---|
translate | TranslateAnimation |
rotate | RotateAnimation |
四、如何在XML文件中定義動(dòng)畫
步驟如下:
- 新建 Android 項(xiàng)目
- 在res目錄中新建anim文件夾
- 在anim目錄中新建一個(gè)my_anim.xml(注意文件名小寫)
- 在 my_anim.xml 加入動(dòng)畫代碼
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<alpha />
<scale />
<translate />
<rotate />
</set>
五上枕、Android XML動(dòng)畫解析
- Alpha
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<alpha
android:fromAlpha="0.1"
android:toAlpha="1.0"
android:duration="3000"
/>
<!-- 透明度控制動(dòng)畫效果 alpha
浮點(diǎn)型值:
fromAlpha 屬性為動(dòng)畫起始時(shí)透明度
toAlpha 屬性為動(dòng)畫結(jié)束時(shí)透明度
說明:
0.0表示完全透明
1.0表示完全不透明
以上值取0.0-1.0之間的float數(shù)據(jù)類型的數(shù)字
長整型值:
duration 屬性為動(dòng)畫持續(xù)時(shí)間
說明:
時(shí)間以毫秒為單位
-->
</set>
- Scale
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:interpolator=
"@android:anim/accelerate_decelerate_interpolator"
android:fromXScale="0.0"
android:toXScale="1.4"
android:fromYScale="0.0"
android:toYScale="1.4"
android:pivotX="50%"
android:pivotY="50%"
android:fillAfter="false"
android:duration="700" />
</set>
<!-- 尺寸伸縮動(dòng)畫效果 scale
屬性:interpolator 指定一個(gè)動(dòng)畫的插入器
在我試驗(yàn)過程中绳瘟,使用android.res.anim中的資源時(shí)候發(fā)現(xiàn)
有三種動(dòng)畫插入器:
accelerate_decelerate_interpolator 加速-減速 動(dòng)畫插入器
accelerate_interpolator 加速-動(dòng)畫插入器
decelerate_interpolator 減速- 動(dòng)畫插入器
其他的屬于特定的動(dòng)畫效果
浮點(diǎn)型值:
fromXScale 屬性為動(dòng)畫起始時(shí) X坐標(biāo)上的伸縮尺寸
toXScale 屬性為動(dòng)畫結(jié)束時(shí) X坐標(biāo)上的伸縮尺寸
fromYScale 屬性為動(dòng)畫起始時(shí)Y坐標(biāo)上的伸縮尺寸
toYScale 屬性為動(dòng)畫結(jié)束時(shí)Y坐標(biāo)上的伸縮尺寸
說明:
以上四種屬性值
0.0表示收縮到?jīng)]有
1.0表示正常無伸縮
值小于1.0表示收縮
值大于1.0表示放大
pivotX 屬性為動(dòng)畫相對(duì)于物件的X坐標(biāo)的開始位置
pivotY 屬性為動(dòng)畫相對(duì)于物件的Y坐標(biāo)的開始位置
說明:
以上兩個(gè)屬性值 從0%-100%中取值
50%為物件的X或Y方向坐標(biāo)上的中點(diǎn)位置
長整型值:
duration 屬性為動(dòng)畫持續(xù)時(shí)間
說明: 時(shí)間以毫秒為單位
布爾型值:
fillAfter 屬性 當(dāng)設(shè)置為true ,該動(dòng)畫轉(zhuǎn)化在動(dòng)畫結(jié)束后被應(yīng)用
-->
- Translate
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="30"
android:toXDelta="-80"
android:fromYDelta="30"
android:toYDelta="300"
android:duration="2000"
/>
<!-- translate 位置轉(zhuǎn)移動(dòng)畫效果
整型值:
fromXDelta 屬性為動(dòng)畫起始時(shí) X坐標(biāo)上的位置
toXDelta 屬性為動(dòng)畫結(jié)束時(shí) X坐標(biāo)上的位置
fromYDelta 屬性為動(dòng)畫起始時(shí) Y坐標(biāo)上的位置
toYDelta 屬性為動(dòng)畫結(jié)束時(shí) Y坐標(biāo)上的位置
注意:
沒有指定fromXType toXType fromYType toYType 時(shí)候姿骏,
默認(rèn)是以自己為相對(duì)參照物
長整型值:
duration 屬性為動(dòng)畫持續(xù)時(shí)間
說明: 時(shí)間以毫秒為單位
-->
</set>
- Rotate
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromDegrees="0"
android:toDegrees="+350"
android:pivotX="50%"
android:pivotY="50%"
android:duration="3000" />
<!-- rotate 旋轉(zhuǎn)動(dòng)畫效果
屬性:interpolator 指定一個(gè)動(dòng)畫的插入器
在我試驗(yàn)過程中,使用android.res.anim中的資源時(shí)候發(fā)現(xiàn)
有三種動(dòng)畫插入器:
accelerate_decelerate_interpolator 加速-減速 動(dòng)畫插入器
accelerate_interpolator 加速-動(dòng)畫插入器
decelerate_interpolator 減速- 動(dòng)畫插入器
其他的屬于特定的動(dòng)畫效果
浮點(diǎn)數(shù)型值:
fromDegrees 屬性為動(dòng)畫起始時(shí)物件的角度
toDegrees 屬性為動(dòng)畫結(jié)束時(shí)物件旋轉(zhuǎn)的角度 可以大于360度
說明:
當(dāng)角度為負(fù)數(shù)——表示逆時(shí)針旋轉(zhuǎn)
當(dāng)角度為正數(shù)——表示順時(shí)針旋轉(zhuǎn)
(負(fù)數(shù)from——to正數(shù):順時(shí)針旋轉(zhuǎn))
(負(fù)數(shù)from——to負(fù)數(shù):逆時(shí)針旋轉(zhuǎn))
(正數(shù)from——to正數(shù):順時(shí)針旋轉(zhuǎn))
(正數(shù)from——to負(fù)數(shù):逆時(shí)針旋轉(zhuǎn))
pivotX 屬性為動(dòng)畫相對(duì)于物件的X坐標(biāo)的開始位置
pivotY 屬性為動(dòng)畫相對(duì)于物件的Y坐標(biāo)的開始位置
說明: 以上兩個(gè)屬性值 從0%-100%中取值
50%為物件的X或Y方向坐標(biāo)上的中點(diǎn)位置
長整型值:
duration 屬性為動(dòng)畫持續(xù)時(shí)間
說明: 時(shí)間以毫秒為單位
-->
</set>
XML中使用動(dòng)畫效果
public static Animation loadAnimation (Context context, int id)
//第一個(gè)參數(shù)Context為程序的上下文
//第二個(gè)參數(shù)id為動(dòng)畫XML文件的引用
//例子:
myAnimation= AnimationUtils.loadAnimation(this, R.anim.my_action);
//使用AnimationUtils類的靜態(tài)方法loadAnimation()來加載XML中的動(dòng)畫XML文件
六斤彼、Java代碼實(shí)現(xiàn)上述動(dòng)畫
xml代碼
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
style="@style/MatchMatch"
android:orientation="vertical"
tools:context="com.example.administrator.foundationdemo.animation.AnimationActivity">
<LinearLayout
android:orientation="horizontal"
style="@style/MatchWrap">
<Button
android:id="@+id/tweened_rotate_btn"
style="@style/WrapWrap"
android:text="旋轉(zhuǎn)" />
<Button
android:id="@+id/tweened_scale_btn"
style="@style/WrapWrap"
android:text="縮放" />
<Button
android:id="@+id/tweened_alpha_btn"
style="@style/WrapWrap"
android:text="淡入淡出" />
<Button
android:id="@+id/tweened_translate_btn"
style="@style/WrapWrap"
android:text="移動(dòng)" />
</LinearLayout>
<ImageView
android:id="@+id/tweened_img"
android:layout_width="200dp"
android:layout_height="300dp"
android:src="@drawable/mz"/>
<Button
android:id="@+id/Tween_Animations"
style="@style/WrapWrap"
android:text="幀動(dòng)畫"/>
</LinearLayout>
AnimationActivity代碼
package com.example.administrator.foundationdemo.animation;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import com.example.administrator.foundationdemo.R;
public class AnimationActivity extends AppCompatActivity {
private Button tweened_rotate_btn;
private Button tweened_scale_btn;
private Button tweened_alpha_btn;
private Button tweened_translate_btn;
private ImageView tweened_img;
private Button tween_animation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_animation);
init();
viewListener();
}
private void init() {
tweened_rotate_btn = (Button) findViewById(R.id.tweened_rotate_btn);
tweened_scale_btn = (Button) findViewById(R.id.tweened_scale_btn);
tweened_alpha_btn = (Button) findViewById(R.id.tweened_alpha_btn);
tweened_translate_btn = (Button) findViewById(R.id.tweened_translate_btn);
tweened_img = (ImageView) findViewById(R.id.tweened_img);
tween_animation = (Button) findViewById(R.id.tween_animation);
}
private void viewListener() {
tweened_rotate_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//創(chuàng)建一個(gè)AnimationSet對(duì)象 參數(shù)為boolean型
//true表示使用Animation的interpolation分瘦,false則是使用自己的
AnimationSet animationSet = new AnimationSet(true);
//參數(shù)1:從哪個(gè)旋轉(zhuǎn)角度開始
//參數(shù)2:轉(zhuǎn)到什么角度
//后4個(gè)參數(shù)用于設(shè)置圍繞著旋轉(zhuǎn)的圓的圓心在哪里
//參數(shù)3:確定x軸坐標(biāo)的類型蘸泻,有ABSOLUT絕對(duì)坐標(biāo)、RELATIVE_TO_SELF相對(duì)于自身坐標(biāo)嘲玫、RELATIVE_TO_PARENT相對(duì)于父控件的坐標(biāo)
//參數(shù)4:x軸的值悦施,0.5f表明是以自身這個(gè)控件的一半長度為x軸
//參數(shù)5:確定y軸坐標(biāo)的類型
//參數(shù)6:y軸的值,0.5f表明是以自身這個(gè)控件的一半長度為x軸
RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
//設(shè)置執(zhí)行時(shí)間,單位ms
rotateAnimation.setDuration(1000);
//將動(dòng)畫對(duì)象添加到序列中
animationSet.addAnimation(rotateAnimation);
tweened_img.startAnimation(animationSet);
}
});
tweened_scale_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AnimationSet animationSet = new AnimationSet(true);
//參數(shù)1:x軸的初始值
//參數(shù)2:x軸收縮后的值
//參數(shù)3:y軸的初始值
//參數(shù)4:y軸收縮后的值
//參數(shù)5:確定x軸坐標(biāo)的類型
//參數(shù)6:x軸的值去团,0.5f表明是以自身這個(gè)控件的一半長度為x軸
//參數(shù)7:確定y軸坐標(biāo)的類型
//參數(shù)8:y軸的值抡诞,0.5f表明是以自身這個(gè)控件的一半長度為x軸
ScaleAnimation scaleAnimation = new ScaleAnimation(
0, 0.1f,0,0.1f,
Animation.RELATIVE_TO_SELF,0.5f,
Animation.RELATIVE_TO_SELF,0.5f);
scaleAnimation.setDuration(1000);
animationSet.addAnimation(scaleAnimation);
tweened_img.startAnimation(animationSet);
}
});
tweened_alpha_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//創(chuàng)建一個(gè)AnimationSet對(duì)象 參數(shù)為boolean型
//true表示使用Animation的interpolation,false則是使用自己的
AnimationSet animationSet = new AnimationSet(true);
//創(chuàng)建一個(gè)AlphaAnimation對(duì)象土陪,參數(shù)透明度昼汗,1完全透明,0不透明
AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
//設(shè)置執(zhí)行時(shí)間,單位ms
alphaAnimation.setDuration(1000);
//將動(dòng)畫對(duì)象添加到序列中
animationSet.addAnimation(alphaAnimation);
tweened_img.startAnimation(animationSet);
}
});
tweened_translate_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AnimationSet animationSet = new AnimationSet(true);
//參數(shù)1~2:x軸的開始位置
//參數(shù)3~4:y軸的開始位置
//參數(shù)5~6:x軸的結(jié)束位置
//參數(shù)7~8:x軸的結(jié)束位置
TranslateAnimation translateAnimation =
new TranslateAnimation(
Animation.RELATIVE_TO_SELF,0f,
Animation.RELATIVE_TO_SELF,0.5f,
Animation.RELATIVE_TO_SELF,0f,
Animation.RELATIVE_TO_SELF,0.5f);
translateAnimation.setDuration(1000);
animationSet.addAnimation(translateAnimation);
tweened_img.startAnimation(animationSet);
}
});
tween_animation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent mIntent = new Intent(AnimationActivity.this,TweenAnimationActivity.class);
startActivity(mIntent);
}
});
}
}
七鬼雀、幀動(dòng)畫
xml代碼
res下新建anim文件夾animation_list XML代碼
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false">
<item android:drawable="@drawable/a1" android:duration="100"/>
<item android:drawable="@drawable/a2" android:duration="100"/>
<item android:drawable="@drawable/a3" android:duration="100"/>
<item android:drawable="@drawable/a1" android:duration="100"/>
</animation-list>
activity_frame_animation XML代碼
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="播放動(dòng)畫" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止動(dòng)畫" />
</LinearLayout>
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="單次播放" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="循環(huán)播放" />
</RadioGroup>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="拖動(dòng)進(jìn)度條修改透明度(0 - 255)之間" />
<SeekBar
android:id="@+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="200dip"
android:layout_height="200dip"
android:background="@anim/animation_list" />
</LinearLayout>
FrameAnimationActivity代碼
package com.example.administrator.foundationdemo.animation;
import android.graphics.drawable.AnimationDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import com.example.administrator.foundationdemo.R;
public class FrameAnimationActivity extends AppCompatActivity {
/** Called when the activity is first created. */
private Button button1,button2;
private RadioGroup radioGroup;
private RadioButton radioButton1,radioButton2;
private SeekBar seekBar;
private ImageView imageView1;
private AnimationDrawable animationDrawable;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_frame_animation);
button1=(Button) this.findViewById(R.id.button1);
button2=(Button) this.findViewById(R.id.button2);
radioGroup=(RadioGroup) this.findViewById(R.id.radioGroup1);
radioButton1=(RadioButton) this.findViewById(R.id.radioButton1);
radioButton2=(RadioButton) this.findViewById(R.id.radioButton2);
seekBar=(SeekBar) this.findViewById(R.id.seekBar1);
imageView1=(ImageView) this.findViewById(R.id.imageView1);
//通過ImageView對(duì)象拿到背景顯示的AnimationDrawable
animationDrawable=(AnimationDrawable) imageView1.getBackground();
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!animationDrawable.isRunning()){
animationDrawable.start();
}
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(animationDrawable.isRunning()){
animationDrawable.stop();
}
}
});
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
if(checkedId==radioButton1.getId()){
//設(shè)置單次播放
animationDrawable.setOneShot(true);
}else if(checkedId==radioButton2.getId()){
//設(shè)置循環(huán)播放
animationDrawable.setOneShot(false);
}
//設(shè)置播放后重新啟動(dòng)
animationDrawable.stop();
animationDrawable.start();
}
});
//監(jiān)聽的進(jìn)度條修改透明度
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
//設(shè)置動(dòng)畫Alpha值
animationDrawable.setAlpha(progress);
//通知imageView 刷新屏幕
imageView1.postInvalidate();
}
});
}
}
上述就是顷窒,屬性動(dòng)畫,補(bǔ)間動(dòng)畫源哩,幀動(dòng)畫的全部類容鞋吉,希望對(duì)大家有幫助