A. android Studio 怎麼創建動畫配置xml文件
在Android
Studio菜單上,依次選擇:
File
New
Xml
Layout
xml
File
Android
Studio
是一個Android開發環境,基於IntelliJ
IDEA.
類似
Eclipse
ADT,Android
Studio
提供了集成的
Android
開發工具用於開發和調試。
在IDEA的基礎上,Android
Stu。詳細的可以看看安卓巴士教程:http://www.apkbus.com/thread-318594-1-1.html
B. 求助一個android動畫怎麼寫,音樂播放狀態的動畫
在Android3.0(即API Level11)以前,Android僅支持2種動畫:分別是Frame Animation(逐幀動畫)和Tween Animation(補間動畫),在3.0之後Android支持了一種新的動畫系統,稱為:Property Animation(屬性動畫)。
一、Frame Animation:(逐幀動畫)
這個很好理解,一幀幀的播放圖片,利用人眼視覺殘留原理,給我們帶來動畫的感覺。它的原理的GIF圖片、電影播放原理一樣。
1.定義逐幀動畫比較簡單,只要在中使用子元素定義所有播放幀即可。
(1) android:oneshot 設置是否僅播放一次
(2) android:drawable 設置每一幀圖片
(3) android:ration 設置圖片間切換間隔
2.習慣上把AnimationDrawable設置為ImageView的背景
android:background=@anim/frame_anim
然後我們就可以在java代碼中獲取AnimationDrawable對象了
AnimationDrawable anim = (AnimationDrawable)imageView.getBackground();
(需要注意的是,AnimationDrawable默認是不播放的,調用其start()方法開始播放,stop停止播放)
3.上面的動畫文件是通過xml文件來配置的,如果你喜歡,也可以通過在java代碼中創建AnimationDrawable對象,然後通過addFrame(Drawable frame, int ration)方法向動畫添加幀,然後start()。。。
二、Tween Animation:(補間動畫)
補間動畫就是我們只需指定開始、結束的「關鍵幀「,而變化中的其他幀由系統來計算,不必自己一幀幀的去定義。
1. Android使用Animation代表抽象動畫,包括四種子類:AlphaAnimation(透明度動畫)、ScaleAnimation(縮放動畫)、TranslateAnimation(位移動畫)、RotateAnimation(透明度動畫)。Android裡面允許在java中創建Animation類對象,但是一般都會採用動畫資源文件來定義動畫,把界面與邏輯分離
<set android:interpolator="@android:anim/linear_interpolator" xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 定義透明度的變換 -->
<!-- 定義旋轉變換 -->
<rotate android:ration="3000/" android:fromdegrees="0" android:pivotx="50%" android:pivoty="50%" android:todegrees="1800">
</rotate></alpha></set>
(一個set可以同時定義多個動畫,一起執行。)
2. android:interpolator=@android:anim/linear_interpolator控制動畫期間需要補入多少幀,簡單來說就是控制動畫速度,有些地方翻譯為「插值「。Interpolator有幾種實現類:LinearInterpolator、AccelerateInterpolator、、CycleInterpolator、DecelerateInterpolator,具體使用可以參考官方API Demo。
3. 定義好anim文件後,我們可以通過AnimationUtils工具類來載入它們,載入成功後返回一個Animation。然後就可以通過View的startAnimation(anim)開始執行動畫了。
Animation anim = AnimationUtils.loadAnimation(this, R.anim.anim);
//設置動畫結束後保留結束狀態
anim.setFillAfter(true);
//設置插值效果
anim.setInterpolator(interpolator);
//對view執行動畫
view. startAnimation(anim);
三、Property Animation:(屬性動畫)
屬性動畫,這個是在Android 3.0中才引進的,它可以直接更改我們對象的屬性。在上面提到的Tween Animation中,只是更改View的繪畫效果而View的真實屬性是不改變的。假設你用Tween動畫將一個Button從左邊移到右邊,無論你怎麼點擊移動後的Button,他都沒有反應。而當你點擊移動前Button的位置時才有反應,因為Button的位置屬性木有改變。而Property Animation則可以直接改變View對象的屬性值,這樣可以讓我們少做一些處理工作,提高效率與代碼的可讀性。
(1)ValueAnimator:包含Property Animation動畫的所有核心功能,如動畫時間,開始、結束屬性值,相應時間屬性值計算方法等。應用ValueAnimator有兩個步驟
1計算屬性值。
2根據屬性值執行相應的動作,如改變對象的某一屬性。
我們的主是第二步,需要實現ValueAnimator.onUpdateListener介面,這個介面只有一個函數onAnimationUpdate(),將要改變View對象屬性的事情在該介面中do。
animation.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
//do your work
}
});
(2)ObjectAnimator:繼承自ValueAnimator,要指定一個對象及該對象的一個屬性,當屬性值計算完成時自動設置為該對象的相應屬性,即完成了Property Animation的全部兩步操作。實際應用中一般都會用ObjectAnimator來改變某一對象的某一屬性,但用ObjectAnimator有一定的限制,要想使用ObjectAnimator,應該滿足以下條件:
1.對象應該有一個setter函數:set(駝峰命名法)
2如下面的例子,像ofFloat之類的工場方法,第一個參數為對象名,第二個為屬性名,後面的參數為可變參數,如果values…參數只設置了一個值的話,那麼會假定為目的值,屬性值的變化范圍為當前值到目的值,為了獲得當前值,該對象要有相應屬性的getter方法:get
3如果有getter方法,其應返回值類型應與相應的setter方法的參數類型一致。
ObjectAnimator oa=ObjectAnimator.ofFloat(tv, alpha, 0f, 1f);
oa.setDuration(3000);
oa.start();
如果不滿足上面的條件,我們只能乖乖的使用ValueAnimator來創建動畫。
(3)Animator.AnimatorListener:可以為Animator設置動畫監聽,需要重寫下面四個方法。
onAnimationStart()
onAnimationEnd()
onAnimationRepeat()
onAnimationCancel()
這里我們也可以實現AnimatorListenerAdapter,他的好處是可以只用定義想監聽的事件而不用實現每個函數卻只定義一空函數體。如下:
anim.addListener(new AnimatorListenerAdapter() {
public void on AnimationEnd(Animator animation){
//do your work
}
});
(4)AnimationSet:可以組合多個動畫共同工作
AnimatorSet bouncer = new AnimatorSet();
bouncer.play(anim1).before(anim2);
bouncer.play(anim2).with(anim3);
bouncer.play(anim2).with(anim4)
bouncer.play(anim5).after(amin2);
animatorSet.start();
上面的代碼意思是: 首先播放anim1;同時播放anim2,anim3,anim4;最後播放anim5。
(5)TimeInterplator:與Tween中的interpolator類似。有以下幾種
AccelerateInterpolator 加速,開始時慢中間加速
DecelerateInterpolator 減速,開始時快然後減速
先加速後減速,開始結束時慢,中間加速
AnticipateInterpolator 反向 ,先向相反方向改變一段再加速播放
反向加回彈,先向相反方向改變,再加速播放,會超出目的值然後緩慢移動至目的值
BounceInterpolator 跳躍,快到目的值時值會跳躍,如目的值100,後面的值可能依次為85,77,70,80,90,100
CycleIinterpolator 循環,動畫循環一定次數,值的改變為一正弦函數:Math.sin(2 * mCycles * Math.PI * input)
LinearInterpolator 線性,線性均勻改變
OvershottInterpolator 回彈,最後超出目的值然後緩慢改變到目的值
TimeInterpolator 一個介面,允許你自定義interpolator,以上幾個都是實現了這個介面
(6)Keyframes:可以讓我們定義除了開始和結束以外的關鍵幀。KeyFrame是抽象類,要通過ofInt(),ofFloat(),ofObject()獲得適當的KeyFrame,然後通過PropertyValuesHolder.ofKeyframe獲得PropertyValuesHolder對象,如下:
Keyframe kf0 = Keyframe.ofInt(0, 400);
Keyframe kf1 = Keyframe.ofInt(0.25f, 200);
Keyframe kf2 = Keyframe.ofInt(0.5f, 400);
Keyframe kf4 = Keyframe.ofInt(0.75f, 100);
Keyframe kf3 = Keyframe.ofInt(1f, 500);
PropertyValuesHolder pvhRotation = PropertyValuesHolder.ofKeyframe(width, kf0, kf1, kf2, kf4, kf3);
ObjectAnimator rotationAnim = ObjectAnimator.ofPropertyValuesHolder(btn, pvhRotation);
上述代碼的意思是:設置btn對象的width屬性值使其:開始時 Width=400,動畫開始1/4時 Width=200,動畫開始1/2時 Width=400,動畫開始3/4時 Width=100,動畫結束時 Width=500。
(7)ViewPropertyAnimator:對一個View同時改變多種屬性,非常推薦用這種。該類對多屬性動畫進行了優化,會合並一些invalidate()來減少刷新視圖。而且使用起來非常簡便,但是要求API LEVEL 12,即Android 3.1以上。僅需要一行代碼即可完成水平、豎直移動
myView.animate().translationX(50f). translationY(100f);
(8)常需要改變的一些屬性:
translationX,translationY: View相對於原始位置的偏移量
rotation,rotationX,rotationY: 旋轉,rotation用於2D旋轉角度,3D中用到後兩個
scaleX,scaleY: 縮放比
x,y: View的最終坐標,是View的left,top位置加上translationX,translationY
alpha: 透明度
四、最後自己總結一下這三種動畫的優缺點:
(1)Frame Animation(幀動畫)主要用於播放一幀幀准備好的圖片,類似GIF圖片,優點是使用簡單方便、缺點是需要事先准備好每一幀圖片;
(2)Tween Animation(補間動畫)僅需定義開始與結束的關鍵幀,而變化的中間幀由系統補上,優點是不用准備每一幀,缺點是只改變了對象繪制,而沒有改變View本身屬性。因此如果改變了按鈕的位置,還是需要點擊原來按鈕所在位置才有效。
(3)Property Animation(屬性動畫)是3.0後推出的動畫,優點是使用簡單、降低實現的復雜度、直接更改對象的屬性、幾乎可適用於任何對象而僅非View類,缺點是需要3.0以上的API支持,限制較大!但是目前國外有個開源庫,可以提供低版本支持!
C. 如何通過android官方api查看xml文件屬性的相關說明
frameworks\base\core\res\res\values\attrs.xml這個可以看
另外就是xml的屬性一般都有對應的java類,到這個java 類api 裡面可以查看到屬性
D. 如何使用旋轉動畫和幀動畫實現自定義ProgressDialog
第一步:自定義progressDialog的樣式 progress_dialog_layout.xml
[html] view plain
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/refreshing_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:scaleType="fitCenter"
android:src="@drawable/icon_progress_dialog" />
</RelativeLayout>
第二步:在styles.xml文件中定義ProgressDialog的主題為:無邊框全透明背景
[html] view plain
<resources>
<!--自定義dialog背景全透明無邊框theme -->
<style name="MyDialog" parent="android:style/Theme.Dialog">
<!--背景顏色及和透明程度-->
<item name="android:windowBackground">@android:color/transparent</item>
<!--是否去除標題 -->
<item name="android:windowNoTitle">true</item>
<!--是否去除邊框-->
<item name="android:windowFrame">@null</item>
<!--是否浮現在activity之上-->
<item name="android:windowIsFloating">true</item>
<!--是否模糊-->
<item name="android:backgroundDimEnabled">false</item>
</style>
</resources>
第三部:在anim文件夾下,定義旋轉動畫的樣式,這里定義了動畫旋轉時間為0.7秒,從0到359度,若設置成360度在停止時會出現停頓現象。動畫的插值器為先加速後減速,旋轉的中心點為imageView的自身中心點。然後repeateCount為重復次數,這里設置為 -1 ,表示無限重復。動畫progress_rotate.xml 如下:
[html] view plain
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<!--設置動畫渲染器為先加速在減速(開始速度最快 逐漸減慢)-->
<rotate
android:ration="700"
android:fromDegrees="0"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="-1"
android:toDegrees="359" />
</set>
這里順便講解下動畫的屬性,加深下印象:
android:fromDegrees 起始的角度度數
android:toDegrees 結束的角度度數,負數表示逆時針,正數表示順時針
android:pivotX 旋轉中心的X坐標
浮點數或是百分比。浮點數表示相對於Object的左邊緣,如5; 百分比表示相對於Object的左邊緣,如5%; 另一種百分比表示相對於父容器的左邊緣,如5%p; 一般設置為50%表示在Object中心
android:pivotY 旋轉中心的Y坐標
浮點數或是百分比。浮點數表示相對於Object的上邊緣,如5; 百分比表示相對於Object的上邊緣,如5%; 另一種百分比表示相對於父容器的上邊緣,如5%p; 一般設置為50%表示在Object中心
android:ration 表示從android:fromDegrees轉動到android:toDegrees所花費的時間,單位為毫秒。可以用來計算速度。
android:interpolator表示變化率,但不是運行速度。一個插補屬性,可以將動畫效果設置為加速,減速,反復,反彈等。默認為開始和結束慢中間快,
android:startOffset 在調用start函數之後等待開始運行的時間,單位為毫秒,若為10,表示10ms後開始運行
android:repeatCount 重復的次數,默認為0,必須是int,可以為-1表示不停止
android:repeatMode 重復的模式,默認為restart,即重頭開始重新運行,可以為reverse即從結束開始向前重新運行。在android:repeatCount大於0或為infinite時生效
android:detachWallpaper 表示是否在壁紙上運行
android:zAdjustment 表示被animated的內容在運行時在z軸上的位置,默認為normal。
normal保持內容當前的z軸順序
top運行時在最頂層顯示
bottom運行時在最底層顯示
在操作開始之前調用
if (drawableAnim != null) {
imageView.startAnimation(drawableAnim);
}
操作完成時調用 drawableAnim.clearAnimation();
第四部:繼承AlertDialog實現自定義的ProgressDialog,如下:d
[java] view plain
package com.example.myfirst.refreshdialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
/**
* 自定義過場動畫,主要用戶數據載入時,顯示等待progress
* Created by 程果 on 2016/3/16.
*/
public class ProgressAlertDialog extends AlertDialog {
private ImageView progressImg;
//旋轉動畫
private Animation animation;
public ProgressAlertDialog(Context context) {
super(context, R.style.MyDialog);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.progress_dialog_layout);
//點擊imageview外側區域,動畫不會消失
setCanceledOnTouchOutside(false);
progressImg = (ImageView) findViewById(R.id.refreshing_img);
//載入動畫資源
animation = AnimationUtils.loadAnimation(getContext(), R.anim.progress_rotate);
//動畫完成後,是否保留動畫最後的狀態,設為true
animation.setFillAfter(true);
}
/**
* 在AlertDialog的 onStart() 生命周期裡面執行開始動畫
*/
@Override
protected void onStart() {
super.onStart();
if( animation != null){
progressImg.startAnimation(animation);
[java] view plain
}
}
/**
* 在AlertDialog的onStop()生命周期裡面執行停止動畫
*/
@Override
protected void onStop() {
super.onStop();
progressImg.clearAnimation();
}
}
效果gif圖如下:
第二種使用 幀動畫實現自定義ProgressDialog,
第一步:也是定義動畫布局progress_drawable_dialog_layout.xml文件,如下:
[html] view plain
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/refreshing_drawable_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:scaleType="fitCenter"
android:src="@drawable/drawable_anim" />
</RelativeLayout>
第二步:在drawable文件下面定義幀動畫,這就比旋轉動畫簡單點,如下只有兩個熟悉,單幀圖片和它顯示的時間:
[java] view plain
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@mipmap/img2"
android:ration="200" />
<item
android:drawable="@mipmap/img3"
android:ration="200" />
<item
android:drawable="@mipmap/img4"
android:ration="200" />
<item
android:drawable="@mipmap/img5"
android:ration="200" />
<item
android:drawable="@mipmap/img6"
android:ration="200" />
<item
android:drawable="@mipmap/img7"
android:ration="200" />
<item
android:drawable="@mipmap/img1"
android:ration="200" />
</animation-list>
第三部:也是使用旋轉動畫中的AlertDialog主題;
第四部:繼承AlertDialog實現自定義的幀動畫ProgressDialog:
[java] view plain
package com.example.myfirst.refreshdialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.widget.ImageView;
/**
* 自定義過場動畫,主要用戶數據載入時,顯示等待progress
* Created by 程果 on 2016/3/16.
*/
public class ProgressDrawableAlertDialog extends AlertDialog {
private ImageView progressImg;
//幀動畫
private AnimationDrawable animation;
public ProgressDrawableAlertDialog(Context context) {
super(context, R.style.MyDialog);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.progress_drawable_dialog_layout);
//點擊imageview外側區域,動畫不會消失
setCanceledOnTouchOutside(false);
progressImg = (ImageView) findViewById(R.id.refreshing_drawable_img);
//載入動畫資源
animation = (AnimationDrawable) progressImg.getDrawable();
}
/**
* 在AlertDialog的 onStart() 生命周期裡面執行開始動畫
*/
@Override
protected void onStart() {
super.onStart();
animation.start();
}
/**
* 在AlertDialog的onStop()生命周期裡面執行停止動畫
*/
@Override
protected void onStop() {
super.onStop();
animation.stop();
}
}
E. 如何查找一個Android控制項具有哪些XML屬性
通過設置公共style的方式對同一個屬性進行統一設置
1、res/values文件夾下新建一個xml文件
1
2
3
4
5
6
7
8
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!--定義一個叫et1的style-->
<style name="et1" parent="@android:style/Widget.EditText">
<!--設置控制項的背景色-->
<item name="android:background">#1A4EA4</item>
</style>
</resources>
2、組件中使用
1
2
<EditText android:layout_width="fill_parent" android:id="@+id/et2"
android:text="自定義樣式一" android:layout_height="wrap_content" style="@style/et1"></EditText> <!--設置style為et1,需要設置同樣屬性的其他組件可以都設置成同一個style-->
F. Android 中 怎麼動態修改布局xml文件中的根屬性值 比如如下圖的兩個屬性值 fitsSystemWindows 改為 false
給這個RelativeLayout設置一個id,比如 android:id="@+id/rl"
然後在class文件中獲取此控制項,再設置屬性即可
RelativeLayoutrelativeLayout=(RelativeLayout)findViewById(R.id.rl);
relativeLayout.setFitsSystemWindows(false);
G. Android 中的動畫有哪幾類,它們的特點和區別是什麼
Android包含三種動畫:
ViewAnimation、 Drawable Animation、Property Animation。
ViewAnimation(Tween Animation補間動畫):
動畫的對象除了傳統的View對象,還可以是Object對象,動畫之後,Object對象的屬性值被實實在在的改變了。Property animation能夠通過改變View對象的實際屬性來實現View動畫。任何時候View屬性的改變,View能自動調用invalidate()來刷新。
H. android 自定義view 怎麼獲取xml屬性
你說的是自定義view ,也就是你自己寫了個自定義的UI控制項,在xml中你只要將該控制項寫出來即可(如Button,TexView當作控制項來寫),例如:<view(你自定義的view的類名)/>,接下來你就懂了,在裡面聲明你要的屬性
I. android xml配置文件中一些標簽的作用
第一個xml是控制項動畫的xml,interpolator設置動畫播放的速度模型,這個設置的是播放速度逐漸變慢。第一個scale是縮放的動畫,ration是動畫時間,從0.9倍放大到1倍,pivot是指參照哪個點進行縮放,這個設置的50%是指中心。alpha是改變透明度的動畫,從0完全透明到1完全不透明。
第二個xml官方叫法是背景選擇器,就是改變按鈕之類的控制項在選中、獲得焦點及通常狀態時的背景的,可以是純色,也可以像你這個似的是drawable中的圖片。在選擇背景時從上到下找到第一個符合條件的為准,state_之類的是各種條件,pressed按下,focused獲得焦點,等等,最後一個item是表示默認條件即之前條件都不滿足時的背景。
J. android view動畫實現從邊緣滑出的效果怎麼做
添加layout布局文件,在xml設置動畫屬性即可,上下左右四個方向均可以實現 。animation in / off 例如: 1.slide_in_right <set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:ration="100" android:fromXDelta="-100.0%p" android:toXDelta="0.0" /> </set> 2.slide_in_right <set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:ration="100" android:fromXDelta="100.0%p" android:toXDelta="0.0" /> </set> 3.slide_out_left <set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:ration="100" android:fromXDelta="0.0" android:toXDelta="-100.0%p" /> </set> 4.slide_out_right <set xmlns:android="http://schemas.android.com/apk/res/android" >