亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android動(dòng)畫之逐幀動(dòng)畫(Frame Animation)基礎(chǔ)學(xué)習(xí)

 更新時(shí)間:2016年09月27日 09:47:38   作者:思格斯  
大家都知道逐幀動(dòng)畫是一種常見的動(dòng)畫形式,其原理是在“連續(xù)的關(guān)鍵幀”中分解動(dòng)畫動(dòng)作,也就是在時(shí)間軸的每幀上逐幀繪制不同的內(nèi)容,使其連續(xù)播放而成動(dòng)畫。下面我們就來學(xué)習(xí)下Android中逐幀動(dòng)畫的基礎(chǔ)知識(shí),有需要的可以參考借鑒。

前言

在Android中,動(dòng)畫Animation的實(shí)現(xiàn)有兩種方式:Tween Animation(補(bǔ)間動(dòng)畫)和Frame Animation(幀動(dòng)畫)。漸變動(dòng)畫是通過對場景里的對象不斷做圖像變換(平移、縮放、旋轉(zhuǎn)等)產(chǎn)生動(dòng)畫效果。幀動(dòng)畫則是通過順序播放事先準(zhǔn)備好的圖像來產(chǎn)生動(dòng)畫效果,和電影類似。

下面我們就來學(xué)習(xí)下Android中逐幀動(dòng)畫的基礎(chǔ)知識(shí)。

原理 : 人眼的"視覺暫留"

方式 :

     1.在java代碼中 ( new AnimationDrawable().addFrame(getDrawable(R.drawable.a),200);)

         sdk好像要求最低版本必須>=21

     2.在XML文件中定義動(dòng)畫資源

效果圖如下

代碼

1.準(zhǔn)備圖片資源

將圖片資源放在drawable-hdpi目錄下

2.在drawable目錄下新建animation-list類型文件

anim_frame.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/a" android:duration="100"/>
 <item android:drawable="@drawable/b" android:duration="100"/>
 <item android:drawable="@drawable/c" android:duration="100"/>
</animation-list>

3.在布局文件中,添加ImageView,將其background屬性設(shè)置為動(dòng)畫資源xml

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="match_parent"
 android:layout_height="match_parent">

 <Button
 android:id="@+id/btn_start"
 android:text="開始跳舞"
 android:textSize="25sp"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"/>
 <Button
 android:id="@+id/btn_stop"
 android:text="結(jié)束跳舞"
 android:textSize="25sp"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"/>

 <ImageView
 android:id="@+id/image"
 android:background="@drawable/anim_frame"
 android:layout_width="match_parent"
 android:layout_height="match_parent"/>
</LinearLayout>

4.在java中,獲取動(dòng)畫資源,調(diào)用start( )開啟動(dòng)畫,stop( )停止動(dòng)畫

package com.lyp.frameanim;

import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
 private Button mBtnStart;
 private Button mBtnStop;
 private ImageView mImage;
 private AnimationDrawable mAnim;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);

// new AnimationDrawable().addFrame(getDrawable(R.drawable.a),200);

 initView();
 mAnim = (AnimationDrawable) mImage.getBackground();
 }

 private void initView() {
 mBtnStart= (Button) findViewById(R.id.btn_start);
 mBtnStop= (Button) findViewById(R.id.btn_stop);
 mImage= (ImageView) findViewById(R.id.image);

 mBtnStart.setOnClickListener(this);
 mBtnStop.setOnClickListener(this);
 }

 @Override
 public void onClick(View v) {
 switch (v.getId()){
  case R.id.btn_start:
  mAnim.start();
  break;
  case R.id.btn_stop:
  mAnim.stop();
  break;
 }
 }
}

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望能對各位Android開發(fā)者們有所幫助,如果有疑問大家可以留言交流。

相關(guān)文章

最新評(píng)論