Android開發之動畫效果淺析(一)
程式執行效果圖:
Android動畫主要包含補間動畫(Tween)View Animation、幀動畫(Frame)Drawable Animation、以及屬性動畫Property Animation。下面依次介紹一下各個動畫。
1. 補間動畫(Tween)
Tween動畫,通過對View的內容進行一系列的圖形變換 (包括平移、縮放、旋轉、改變透明度)來實現動畫效果。動畫效果的定義可以採用XML來做也可以採用編碼來做。Tween動畫有4種類型:
動畫的型別 | Xml定義動畫使用的配置節點 | 編碼定義動畫使用的類 |
漸變透明度動畫效果 | AlphaAnimation | |
漸變尺寸縮放動畫效果 | ScaleAnimation | |
畫面位置移動動畫效果 | TranslateAnimation | |
畫面旋轉動畫效果 | RotateAnimation |
我們可以為每一個動畫設定動畫插入器,Android自帶的幾種動畫插入器:
AccelerateInterpolator | 加速,開始時慢中間加速 |
DecelerateInterpolator | 減速,開始時快然後減速 |
AccelerateDecelerateInterolator | 先加速後減速,開始結束時慢,中間加速 |
AnticipateInterpolator | 反向,先向相反方向改變一段再加速播放 |
AnticipateOvershootInterpolator | 反向加超越,先向相反方向改變,再加速播放,會超出目的值然後緩慢移動至目的值 |
BounceInterpolator | 跳躍,快到目的值時值會跳躍,如目的值100,後面的值可能依次為85,77,70,80,90,100 |
CycleIinterpolator | 迴圈,動畫迴圈一定次數,值的改變為一正弦函式:Math.sin(2* mCycles* Math.PI* input) |
LinearInterpolator | 線性,線性均勻改變 |
OvershottInterpolator | 超越,最後超出目的值然後緩慢改變到目的值 |
1.1預備知識:
抽象類Animation是一個實現androidUI介面動畫效果的API,Animation是補間動畫的基類,它的直接子類AlphaAnimation, RotateAnimation, ScaleAnimation, TranslateAnimation,AnimationSet,提供了一系列的動畫效果,可以進行淡入淡出、旋轉、縮放、動畫集等,這些效果可以應用在絕大多數的控制元件中。
1.2AlphaAnimation實現淡入淡出的動畫效果
//方式一通過程式碼的方式定義透明度動畫
AnimationalphaAnimation=new AlphaAnimation(1, (float) 0.1);
alphaAnimation.setDuration(3000);//設定動畫持續時間為3秒
alphaAnimation.setFillAfter(true);//設定動畫結束後保持當前的位置(即不返回到動畫開始前的位置)
imgShow.startAnimation(alphaAnimation);
//方式二通過在xml中定義透明度動畫
第一步:定義xml動畫檔案:alpha.xml
"1.0"encoding="utf-8"?>
<alphaxmlns:android="https://schemas.android.com/apk/res/android"
android:fromAlpha="1.0"
android:toAlpha="0.1"
android:duration="3000"
android:fillAfter="true"
android:repeatCount="2">
第二步:載入xml動畫檔案並將其設定到指定的View上
AnimationalphaAnimation2=AnimationUtils.loadAnimation(this, R.anim.alpha);//載入Xml檔案中的動畫
imgShow.startAnimation(alphaAnimation2);
程式執行效果圖:
1.3.RotateAnimation實現旋轉的動畫效果
主要屬性及說明:
repeatCount 重複次數
fromDegrees為動畫起始時物件的角度:
當角度為負數——表示逆時針旋轉
當角度為正數——表示順時針旋轉
(負數fromDegrees——toDegrees正數:順時針旋轉)
(負數fromDegrees——toDegrees負數:逆時針旋轉)
(正數fromDegrees——toDegrees正數:順時針旋轉)
(正數fromDegrees——toDegrees負數:逆時針旋轉)
toDegrees屬性為動畫結束時物件旋轉的角度可以大於360度
pivotX,pivotY 為動畫相對於物件的X、Y座標的開始位.說明:以上兩個屬性值從0%-100%中取值,50%為物件的X或Y方向座標上的中點位置。
例項:
//方式一通過程式碼的方式定義旋轉動畫
AnimationrotateAnimation=new RotateAnimation(0, 45);
rotateAnimation.setDuration(3000);//設定動畫持續時間為3秒
rotateAnimation.setFillAfter(true);//設定動畫結束後保持當前的位置(即不返回到動畫開始前的位置)
imgShow.startAnimation(rotateAnimation);
//方式二通過在xml中定義旋轉動畫
第一步:定義xml動畫檔案:rotate.xml
"1.0"encoding="utf-8"?>
<rotatexmlns:android="https://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="45"
android:duration="300"
android:fillAfter="true">
第二步:載入xml動畫檔案並將其設定到指定的View上
Animation rotateAnimation2=AnimationUtils.loadAnimation(this, R.anim.rotate);//載入Xml檔案中的動畫
imgShow.startAnimation(rotateAnimation2);
程式執行效果圖:
1.4.ScaleAnimation實現縮放動畫效果
主要屬性及說明:
fromXScale(浮點型)屬性為動畫起始時X座標上的縮放尺寸
fromYScale(浮點型)屬性為動畫起始時Y座標上的縮放尺寸
toXScale(浮點型) 屬性為動畫結束時X座標上的縮放尺寸
toYScale(浮點型) 屬性為動畫結束時Y座標上的縮放尺寸
說明: 以上四種屬性值
0.0表示收縮到沒有
1.0表示正常無縮放
值小於1.0表示收縮
值大於1.0表示放大
pivotX(浮點型) 屬性為動畫相對於物件的X座標的開始位置
pivotY(浮點型) 屬性為動畫相對於物件的Y座標的開始位置
說明:
以上兩個屬性值從0%-100%中取值
50%為物件的X或Y方向座標上的中點位置
duration(長整型)屬性為動畫持續時間。說明: 時間以毫秒為單位
fillAfter(布林型)屬性當設定為true,該動畫轉化在動畫結束後被應用
例項:
//方式一通過程式碼的方式定義縮放動畫
AnimationscaleAnimation=new ScaleAnimation(0.5f, 1.0f,1.0f, 1.0f);
scaleAnimation.setDuration(2000);//設定動畫持續時間為3秒
scaleAnimation.setFillAfter(true);//設定動畫結束後保持當前的位置(即不返回到動畫開始前的位置)
scaleAnimation.setRepeatCount(3);
imgShow.startAnimation(scaleAnimation);
//方式二通過在xml中定義縮放動畫
第一步:定義xml動畫檔案:scale.xml
"1.0"encoding="utf-8"?>
<scalexmlns:android="https://schemas.android.com/apk/res/android"
android:fromXScale="0.5"
android:toXScale="1.0"
android:fromYScale="1.0"
android:toYScale="1.0"
android:duration="3000"
android:fillAfter="true">
第二步:載入xml動畫檔案並將其設定到指定的View上
AnimationscaleAnimation2=AnimationUtils.loadAnimation(this, R.anim.scale);//載入Xml檔案中的動畫imgShow.startAnimation(scaleAnimation2);
程式執行效果圖:
1.5. TranslateAnimation實現位移動畫效果
//方式一通過程式碼的方式定義位移動畫
AnimationtranslateAnimation=new TranslateAnimation(0, 100, 0, 0);
translateAnimation.setDuration(3000);//設定動畫持續時間為3秒
translateAnimation.setInterpolator(this, android.R.anim.cycle_interpolator);//設定動畫插入器
translateAnimation.setFillAfter(true);//設定動畫結束後保持當前的位置(即不返回到動畫開始前的位置)
imgShow.startAnimation(translateAnimation);
//方式二通過在xml中定義位移動畫
第一步:定義xml動畫檔案:translate.xml
"1.0"encoding="utf-8"?>
"https://schemas.android.com/apk/res/android"
android:fromXDelta="0"
android:toXDelta="260"
android:fromYDelta="0"
android:toYDelta="600"
android:duration="3600"
android:fillAfter="true"
android:interpolator="@android:anim/accelerate_decelerate_interpolator">
第二步:載入xml動畫檔案並將其設定到指定的View上
AnimationtranslateAnimation2=AnimationUtils.loadAnimation(this, R.anim.translate);//載入Xml檔案中的動畫
imgShow.startAnimation(translateAnimation2);
程式執行效果圖:
1.6.AnimationSet實現多種動畫混合效果
定義動畫集主要用到了AnimationSet類,該類可以新增多個補間動畫啊。
//方式一通過程式碼的方式定義動畫集
AnimationSetanimationSet=new AnimationSet(true);//定義一個動畫集,並設定所有的動畫使用一個動畫插入其
Animation translateAnimation2=AnimationUtils.loadAnimation(this, R.anim.translate);//載入Xml檔案中的動畫
AnimationalphaAnimation2=AnimationUtils.loadAnimation(this, R.anim.alpha);//載入Xml檔案中的動畫
Animation rotateAnimation2=AnimationUtils.loadAnimation(this, R.anim.rotate);//載入Xml檔案中的動畫
Animation scaleAnimation2=AnimationUtils.loadAnimation(this, R.anim.scale);//載入Xml檔案中的動畫
animationSet.addAnimation(translateAnimation2);
animationSet.addAnimation(alphaAnimation2);
animationSet.addAnimation(rotateAnimation2);
animationSet.addAnimation(scaleAnimation2);
animationSet.setInterpolator(this, android.R.anim.anticipate_interpolator);
imgShow.startAnimation(animationSet);
//方式二在xml檔案中設定動畫集合
第一步:定義xml動畫檔案:animset.xml
"1.0"encoding="utf-8"?>
<setxmlns:android="https://schemas.android.com/apk/res/android">
android:fromAlpha="1.0"
android:toAlpha="0.1"
android:duration="3000"
android:fillAfter="true"
android:repeatCount="2">
android:fromDegrees="0"
android:toDegrees="45"
android:duration="300"
android:fillAfter="true">
android:fromXScale="0.5"
android:toXScale="1.0"
android:fromYScale="1.0"
android:toYScale="1.0"
android:duration="3000"
android:fillAfter="true">
android:fromXDelta="0"
android:toXDelta="260"
android:fromYDelta="0"
android:toYDelta="600"
android:duration="3600"
android:fillAfter="true"
android:interpolator="@android:anim/accelerate_decelerate_interpolator">
第二步:載入xml動畫檔案並將其設定到指定的View上
AnimationSetanimationSet2=(AnimationSet) AnimationUtils.loadAnimation(this, R.anim.animset);
程式執行效果圖:
提示:雖然可以通過程式碼的方式定義動畫,但是Android官方還是建議在xml中定義動畫效果,這樣可做到最大程度上的解耦,方便專案的後期維護。
2. 幀動畫(Frame)
Frame動畫,即順序播放事先做好的影象,跟放膠片電影類似。
開發步驟:
(1)把準備好的圖片放進專案res/ drawable下。
(2)在專案的drawable資料夾下面定義動畫XML檔案,檔名稱可以自定義。當然也可以採用編碼方式定義動畫效果(使用AnimationDrawable類)。
(3)為View控制元件繫結動畫效果。呼叫代表動畫的AnimationDrawable的start()方法開始動畫。
例項:
framelist.xml
"1.0"encoding="utf-8"?>
<animation-listxmlns:android="https://schemas.android.com/apk/res/android"
android:oneshot="false">
<itemandroid:drawable="@drawable/zzlx1"android:duration="200"/>
<itemandroid:drawable="@drawable/zzlx2"android:duration="200"/>
<itemandroid:drawable="@drawable/zzlx3"android:duration="200"/>
<itemandroid:drawable="@drawable/zzlx4"android:duration="200"/>
<itemandroid:drawable="@drawable/zzlx5"android:duration="200"/>
<itemandroid:drawable="@drawable/zzlx6"android:duration="200"/>
<itemandroid:drawable="@drawable/zzlx7"android:duration="200"/>
<itemandroid:drawable="@drawable/zzlx8"android:duration="200"/>
程式碼分析:
上面的XML就定義了一個Frame動畫,其包含8幀動畫,8幀動畫中分別應用了drawable中的8張圖片:zzlx1、zzlx2、zzlx3....,每幀動畫持續200毫秒。android:oneshot屬性如果為true,表示動畫只播放一次停止在最後一幀上,如果設定為false表示動畫迴圈播放。
在Xml中定義好幀動畫之後就可以將其設定到View上了,請看下面程式碼:
//第一步將animation-list設定為ImageView的背景
imgShow.setBackgroundResource(R.drawable.framelist);
//第二步獲取ImagView的背景並將其轉換成AnimationDrawable
AnimationDrawableanimationDrawable=(AnimationDrawable)imgShow.getBackground();
//第三步開始播放動畫
animationDrawable.start();
提示:
有一點需要強調的是:啟動Frame動畫的程式碼animationDrawable.start();不能應用在OnCreate()方法中,因為在OnCreate()中AnimationDrawable還沒有完全的與ImageView繫結。在OnCreate()中啟動動畫,只能看到第一張圖片。這裡在觸控事件中實現的
程式執行效果圖:
3. 屬性動畫(Property Animation)
使用屬性動畫注意事項:
1). object必須要提供setXxx方法,如果動畫的時候沒有傳遞初始值,那麼還要提供getXxx方法,因為系統要去拿xxx屬性的初始值(如果這條不滿足,程式直接Crash)
2). object的setXxx對屬性xxx所做的改變必須能夠通過某種方法反映出來,比如會帶來ui的改變啥的(如果這條不滿足,動畫無效果但不會Crash)
以上條件缺一不可。
3).對應沒有getXxx和setXxx方法或有getXxx和setXxx方法但和getXxx和setXxx方法設定的屬性不是我們想要的效果如,我們對Button的width屬性做動畫沒有效果?這是因為Button內部雖然提供了getWidth和setWidth方法,但是這個setWidth方法並不是改變檢視的大小,它是TextView新新增的方法,View是沒有這個setWidth方法的,由於Button繼承了TextView,所以Button也就有了setWidth方法.getWidth的確是獲取View的寬度的,而setWidth是TextView和其子類的專屬方法,它的作用不是設定View的寬度,而是設定TextView的最大寬度和最小寬度的,這個和TextView的寬度不是一個東西,具體來說,TextView的寬度對應Xml中的android :layout_width屬性,而TextView還有一個屬性android :width,這個android:width屬性就對應了TextView的setWidth方法。這裡給出一種解決方法及使用包裝類:用一個類來包裝原始物件,間接為其提供get和set方法。如下:
/**
*包裝類用於封裝View的with、height,
*你可以通過getXxx以及setXxx方法設定View的寬和高
*@author jph
*/
class WrapView{
private Viewview;
privateintwidth;
privateintheight;
public WrapView(View view){
this.view=view;
}
publicint getWidth() {
returnview.getLayoutParams().width;
}
publicvoid setWidth(int width) {
this.width = width;
view.getLayoutParams().width=width;//修改View的高度
view.requestLayout();//重新整理View的佈局
}
publicint getHeight() {
returnview.getLayoutParams().height;
}
publicvoid setHeight(int height) {
this.height=height;
view.getLayoutParams().height = height;
view.requestLayout();
}
}
例項:
?123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 | package com.jph.anim; import android.animation.ObjectAnimator; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.ImageView; /** * 屬性動畫例項 * @author jph * */ public class PropertyActivity extends Activity { private ImageView imgShow; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.property); imgShow=(ImageView)findViewById(R.id.imgShow); } public void play(View view) { switch (view.getId()) { case R.id.btnStart: ObjectAnimator animator=ObjectAnimator.ofInt( new WrapView(imgShow), "width" , 10 ); animator.setDuration( 2000 ); //設定動畫持續的時間 animator.setRepeatCount( 2 ); //設定動畫重複的次數 animator.start(); //開啟動畫 default : break ; } } /** * 包裝類用於封裝View的with、height, * 你可以通過getXxx以及setXxx方法設定View的寬和高 * @author jph */ class WrapView{ private View view; private int width; private int height; public WrapView(View view){ this .view=view; } public int getWidth() { return view.getLayoutParams().width; } public void setWidth( int width) { this .width = width; view.getLayoutParams().width=width; //修改View的高度 view.requestLayout(); //重新整理View的佈局 } public int getHeight() { return view.getLayoutParams().height; } public void setHeight( int height) { this .height=height; view.getLayoutParams().height = height; view.requestLayout(); } } } |
程式執行效果圖:
相關推薦
Android開發之動畫效果淺析(一)
程式執行效果圖:Android動畫主要包含補間動畫(Tween)View Animation、幀動畫(Frame)Drawable Animation、以及屬性動畫Property Animation。下面依次介紹一下各個動畫。1. 補間動畫(Tween)Tween動畫,通過
Android學習之動畫總結(一)
寫在前面:本文是根據hencoder提供的教程寫的總結。HenCoder https://hencoder.com。 Android裡動畫可以分為兩類:Animation和Transition,其中Animation又可以分為View Animation和Pr
Android開發之藍芽(一)——基於SPP協議藍芽模組通訊
使用裝置 基本概念 基本流程 本文意在介紹藍芽開發的主要流程,學習使用藍芽開發一個星期了,寫寫一個星期以來遇到的一些小問題,還有介紹下流程。開發具有基本的通訊功能,本專案主要是用於與藍芽模組的串列埠讀寫功能。 下一篇文章還有Android開
Android開發之實現最簡單最酷炫的3D圖片瀏覽效果(一)
一、原理 整體實現是以手機螢幕的正中間位置為對稱軸,位於正中間的圖片顯示最大,也最亮,同時左右兩邊的圖片以最中間位置為對稱軸,分別旋轉對應的角度,同時亮度調整為適當的比例,已達到對稱的效果。具體的3D瀏覽圖片效果,我是通過自定義Gallery來實現的,建立一個類Galler
【Android】Android開發之常用的loading等待效果實現,仿微博等待動畫。兩種實現方式詳解
長期維護的Android專案,裡面包括常用功能實現,以及知識點詳解, 當然還有Java中的知識點。 具體請看github:https://github.com/QQ986945193/DavidAndroidProjectTools 首先大家都知道,當我
Android開發之仿手機衛士懸浮窗效果
wrap 使用 indexof handle post ani refresh stat gen 基本的實現原理,這種桌面懸浮窗的效果很類似與Widget,但是它比Widget要靈活的多。主要是通過WindowManager這個類來實現的,調用這個類的addView方法用於
Android開發 - 掌握ConstraintLayout(十一)複雜動畫!如此簡單!
介紹 本系列我們已經介紹了ConstraintLayout的基本用法。學習到這裡,相信你已經熟悉ConstraintLayout的基本使用了,如果你對它的用法還不瞭解,建議您先閱讀我之前的文章。 使用ConstraintLayout建立動畫的基本思想是我們建立兩個不同的佈局,每個佈局有其不同的約束,從而我
Android OpenGL ES濾鏡開發之美顏效果
前言 其實之前我就是已經把美顏效果啥的都做完了,但是就一直懶得記錄,今天來記錄一下,之前記錄的就是大眼睛還有貼紙的效果。以前的時候不愛寫部落格,總覺得很麻煩,現在發現寫部落格是用來總結複習很好的一個途徑,很多時候寫效果或者做些什麼,就基本就是做完就完事兒了,也不去總結一下或者拿來複習一下
Android酷炫動畫效果之進度載入動畫
package com.eftimoff.androipathview; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Bitmap; import android
Android開發之顯示篇(弄懂ppi、dpi、pt、px、dp、dip、sp之間的關係看這一篇就夠了)
版權申明】非商業目的註明出處可自由轉載 博文地址:https://blog.csdn.net/ShuSheng0007/article/details/85165773 出自:shusheng007 文章目錄 概述 要解決的疑問 概念篇
android開發之&使用ViewPager加gridView實現選單按鈕分頁滑動(類似QQ表情選擇翻頁效果)
剛做的專案中要用到選單分頁,以前沒做過,仔細想了想,既然是分頁,肯定就少不了ViewPager,大家都知道gridView可以實現九宮格,剛好滿足我們的需求,我做的是gridview單行顯示,大家如果需要向QQ表情一樣多行顯示,直接修改資料來源就可以。 好了,上程式碼 pu
Android開發之實現水平滾動效果—RecyclerView
ListView由於其強大的功能,在過去的Android開發中有著卓越的貢獻,然而使用ListView,如果不使用一些技巧來提升其效率的話,那麼ListView的效能會很差。 為此,Android提供了一個更加強大的滾動控制元件–RecyclerView。下面
Android開發之縱向跑馬燈效果實現
我們看淘寶、京東等app軟體,首頁就會看到那廣告詞向上走。。。今天我給大家帶來兩種實現方式。效果圖就不上啦,直接上程式碼: 一,自定義view 1,之前經常用findViewById(),今天用另一種方式來找控制元件。在app/build.gradle的閉包
Android開發之使一開啟activity等介面Edittext獲取焦點,彈出軟鍵盤java程式碼實現
// 獲取編輯框焦點 editText.setFocusable(true); //開啟軟鍵盤 IInputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVI
android開發之仿微信輸入框效果
<EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="50dp" android:layout_m
Android開發之手把手教你寫ButterKnife框架(一)
系列文章目錄導讀: 一、概述 JakeWharton我想在Android界無人不知,無人不曉的吧, ButterKnife這個框架就是出自他隻手。這個框架我相信很多人都用過,本系列部落格就是帶大家更加深入的認識這個框架,ButterKnife截至目前
Android開發之計算器(一)介面設計之activity_main佈局檔案
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schem
Android開發之計算器(一)介面設計
計算器開發主要涉及到LinearLayout佈局、EditText、Button的使用。為android入門基礎內容。 開啟android studio選擇建立一個新的工程,應用程式的名稱為CalculateApp,公司域可以設定,也可以不設定,我設定的如圖所示 點選“next”,選擇支援的最
Android開發之使用Tween動畫
Tween大概有四種動畫效果: 1.Alpha:漸變透明度效果 2.Scale:伸縮效果 3.Translate:位置移動效果 4.Rotate:旋轉效果 效果如圖: 一、程式碼實現 1.AlphaAnimation動畫 // AlphaAnimation(fl
Android開發之藍芽詳解(一)
一.概述 這篇文章是我學習Android開發官網以及網上一些其他文章總結而來,主要就是為了好好研究一下藍芽開發,看完這篇文章以後,我們就知道了怎樣使用藍芽API完成建立藍芽連線的必要四步:1.開啟藍芽;2.查詢附近已配對或可用的裝置;3.連線裝置;4.裝置間資