android---動畫入門(一)

来源:https://www.cnblogs.com/xkd-/archive/2019/02/21/10413855.html
-Advertisement-
Play Games

android 動畫分為兩類,View Animation(視圖動畫)和property Animation(屬性動畫),View Animation(視圖動畫)包含了Tween Animation和Frame Animation, property Animation包含Value Animati ...


android 動畫分為兩類,View Animation(視圖動畫)和property Animation(屬性動畫),View Animation(視圖動畫)包含了Tween Animation和Frame Animation, property Animation包含Value Animation和ObjectAnimation.

  1. View Animation(視圖動畫)
    1. Tween Animation
    2. Frame Animation
  2. property Animation(屬性動畫)
    1. Value Animation
    2. ObjectAnimation

Animation類是所有動畫(scale,alpha,translate,rotate)的基類,所有派生自Animation的類都具有它的一些公用屬性

  1. android:duration : 一次動畫的時間
  2. android:fillBefore:動畫結束是否恢復到開始前的狀態,true 是
  3. android:fillAftre:動畫結束是否保持結束時的狀態,true 是
  4. android:fillEnable: 同 android:fillBefore
  5. android:repeatCount: 動畫執行次數,(在set標簽下無效,要設置到具體動畫中)
  6. android:repeatMode:動畫重覆執行的模式:reverse 倒序回放,restart重新播放
  7. android:interpolator: 設置插值器
  8. android:startOffset: 延時多少毫秒開始動畫

Tween Animation(補間動畫)

  1. alpha   透明度漸變
  2. scale   放縮
  3. translate   移動
  4. rotate   旋轉
  5. set   自定義組合動畫

動畫的調用方式有兩種,xml標簽實現和代碼實現

(一):alpha   透明度漸變

  alpha 動畫特有的兩個屬性:

  1. android:fromAlpha="1"  動畫開始時候的透明度,0~1:0表示完全透明,1表示完全不透明
  2. android:toAlpha="0.1"  動畫結束時候的透明度

舉個慄子:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <alpha xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:fromAlpha="1"
 4     android:toAlpha="0.1"
 5     android:duration="2000" 
 6     android:repeatCount="5"
 7     android:repeatMode="reverse"
 8     >
11 </alpha>

(二):scale   放縮漸變

  scale   動畫特有的屬性:

  1. android:fromXScale="1"  動畫開始時,控制項在X軸方向上的比例,1表示自身比例,0.5表示自身比例的一半,2表示自身的兩倍
  2. android:toXScale="1.4"  動畫結束時,控制項在X軸方向上的比例,值同上
  3. android:fromYScale="0.4"
  4. android:toYScale="1.4"
  5. android:pivotX="50%"  縮放起始點X軸坐標,值有三種格式,數值,百分比,百分數p,具體含義在註里解釋
  6. android:pivotY="50%"  縮放起始點Y軸坐標

:縮放起始點:預設是控制項左上角的點為起始點,  數值 表示 左上角坐標點+這個數值 為起始點,百分比  表示 左上角坐標點+ 自身寬度或高度的值的百分比,百分數p 表示左上角坐標點+這個控制項的父控制項的寬高乘以這個百分比

舉個慄子:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <scale xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:fromXScale="0.4"
 4     android:toXScale="1.4"
 5     android:fromYScale="0.4"
 6     android:toYScale="1.4"
 7     android:pivotX="50%"
 8     android:pivotY="50%"
 9     android:repeatCount="3"
10     android:repeatMode="reverse"
11     android:duration="3000"
12     android:fillAfter="true"
13 
14     >
15 
16 </scale>

 

(二):translate   移動

  translate 動畫特有的屬性:

  1. android:fromXDelta="0" X軸開始坐標
  2. android:toXDelta="80%p"  X軸結束坐標, 值可以是數值,百分比,百分比p
  3. android:fromYDelta="0"
  4. android;toYDelta="80%"

舉個慄子:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <translate xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:fromXDelta="0"
 4     android:toXDelta="80%p"
 5     android:fromYDelta="0"
 6     android:toYDelta="80%p"
 7     android:duration="2000"
 8     android:repeatCount="3"
 9     android:repeatMode="reverse"
10     android:startOffset="1000"
11     >
12 
13 </translate>

 

(二):rotate 旋轉漸變

  rotate 動畫特有的屬性:

  1. android:fromDegrees="0" 動畫開始旋轉的角度,三點鐘方向為0°,6點鐘方向為90°
  2. android:toDegrees="180" 動畫結束旋轉的角度
  3. android:pivotX="50%"   旋轉的圓心坐標
  4. android:pivotY="50%"  旋轉的圓心坐標

舉個慄子:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <rotate xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:fromDegrees="0"
 4     android:toDegrees="180"
 5     android:visible="true"
 6     android:pivotX="50%"
 7     android:pivotY="50%"
 8     android:duration="2000"
 9     android:repeatCount="3"
10     android:repeatMode="reverse"
11     >
12 
13 </rotate>

 

(二):set 自定義動畫組合

  set 沒有自己的特有屬性,repeatCount屬性不能直接再set標簽下設置,設置無效,要設置在裡面的具體動畫中

舉個慄子:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <set xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:duration="2000"
 4     android:repeatMode="reverse"
 5     android:fillAfter="true"
 6 
 7     >
 8 
 9     <alpha
10         android:fromAlpha="0.1"
11         android:toAlpha="1"
12         android:duration="5000"
13         android:repeatCount="5"
14         android:startOffset="1000"
15         />
16     <translate
17         android:fromXDelta="0"
18         android:toXDelta="10%"
19         android:fromYDelta="0"
20         android:toYDelta="0"
21         android:repeatCount="3"
22         android:repeatMode="reverse"/>
23 
24     <rotate
25         android:fromDegrees="0"
26         android:toDegrees="180"
27         android:visible="true"
28         android:pivotX="50%"
29         android:pivotY="50%"
30         android:duration="2000"
31         android:repeatCount="3"
32         android:repeatMode="reverse"/>
33     <scale
34         android:fromXScale="0.4"
35         android:toXScale="1.4"
36         android:fromYScale="0.4"
37         android:toYScale="1.4"
38         android:pivotX="50%"
39         android:pivotY="50%"
40         android:repeatCount="3"
41         android:repeatMode="reverse"
42         android:duration="3000"
43         android:fillAfter="true"/>
44 
45 </set>

屬性介紹完了我們來實現一下:

1.在android studio中創建 xml標簽文件

 

 

 

結果:

該文件也可以放在drawable目錄下

2.以set舉慄子:

  1. 準備xml標簽文件set.xml
  2. <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="3000"
        android:fillAfter="true">
    
        <alpha android:fromAlpha="1.0"
            android:toAlpha="0.1"/>
        <scale android:fromXScale="0"
            android:fromYScale="0"
            android:toYScale="1.4"
            android:toXScale="1.4"/>
        <translate android:fromYDelta="0"
            android:fromXDelta="0"
            android:toYDelta="0"
            android:toXDelta="50%"/>
        <rotate android:fromDegrees="0"
            android:toDegrees="90"
            />
    </set>
    

      

  3. 在佈局中放一個TextView
  4. <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.learning.animationapplication.MainActivity">
    
        <TextView
            android:id="@+id/tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:background="@android:color/holo_blue_bright"/>
    
    </android.support.constraint.ConstraintLayout>
    

      

  5. 在Activity中載入動畫

    

package com.learning.animationapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private TextView tv;
    private Animation animation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = findViewById(R.id.tv);
        animation = AnimationUtils.loadAnimation(this, R.anim.set);//載入動畫
        tv.startAnimation(animation);//啟動動畫,並且是立即啟動
    }
}

  

 

代碼實現,不用xml標簽

舉例:

 

 

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 表結構為: 表數據為: 表中數據可以看到name列中有兩行數據是一致的,去重的SQL語句為: 去重後的數據為: 總結 在進行去重時,資料庫無法進行邊查詢邊刪除,所以刪除的時候必須是第三張臨時表作為匹配刪除,這樣的操作可以省略創建臨時表直接刪除原表中數據,推薦這種方式進行去重操作。 ...
  • 在視窗1開通一個名為 redis 的通道: 從視窗2傳入信息: 此時視窗1會收到這條信息: 以上, 就是通過 SUBSCRIBE 和 PUBLISH 實現了一個簡單的消息傳遞的過程. 目前我們是有一個視窗開通 redis 通道, 另一個視窗向這個通道傳遞消息, 大家可以試下再多開一個視窗, 也開通 ...
  • ordered set 是根據 score值有序排列的數據集合. 首先還是清空數據, 並清屏, 此步驟省略~~~~ 新建一條 ordered set 數據 myset1, 並存入4個字元串, score 的排列順序為1-4: 查看這個數據: 給 myset1 的值里新加一個字元e, score=10 ...
  • INSERT INTO table SELECT * FROM OPENDATASOURCE ('SQLOLEDB', 'Data Source=172.168.44.146;User ID=sa;password=123' ).tableexec sp_configure 'show advanc ...
  • unordered collection of unique strings.set值是唯一的字元串的無序集合, 把握住兩個特點: 唯一, 無序. 清空所有的數據, 並清理顯示界面: 保存一條 set 數據, 鍵是 myset1, 值是 1, 2, 3, 4 四個數字: 查看鍵myset1 的值: ...
  • select * from dba_jobs ;select * from dba_scheduler_job_run_details t; >這個語句通過制定job名,來查看job的運行的詳細信息 select * from dba_scheduler_jobs; >這個語句可以查看所有job,及 ...
  • 正如前面所講的, redis 的數據結構就是一系列的鍵值對鍵 -> printable ASCII (可列印的 ASCII 碼, 最大值是 512MB)值 -> Primitives (基本的) string 字元串 (最大值是 512MB) Containers (of string) (以其他形 ...
  • 屏幕尺寸 #define kScreenWidth [UIScreen mainScreen].bounds.size.width #define kScreenHeight [UIScreen mainScreen].bounds.size.height 手機型號 #define kISiPhon ...
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...