java逐帧动画_android的逐帧动画
逐幀動(dòng)畫是指按照給定的順序輪流顯示一定數(shù)目的圖像而產(chǎn)生的動(dòng)畫效果。一般應(yīng)用于比較簡單的場景,比如一個(gè)監(jiān)測手機(jī)wifi信號(hào)強(qiáng)度的應(yīng)用中,不斷的顯示wifi信號(hào)的強(qiáng)弱程度就比較適合用逐幀動(dòng)畫。
逐幀動(dòng)畫的使用例子:
使用Animation-list定義圖像元素,注意順序。圖片素材:
文件名稱:icon1.pngicon1.pngicon1.pngicon1.pngicon1.pngicon1.png
利用上面的圖片素材,我們定義了兩個(gè)Animation-list,文件存放在res/drawable目錄下
順序顯示動(dòng)畫文件:animation1.xml<?xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true"
>
倒序顯示動(dòng)畫文件:animation2.xml<?xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true"
>
布局文件, 文件名main.xml:<?xml version="1.0" encoding="utf-8"?>
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5px"
android:src="@drawable/animation1"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5px"
android:text="順序顯示" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5px"
android:text="停止" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5px"
android:text="倒序顯示" />
Activity文件,文件名:MainActivity.javapackage org.shuxiang.test;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
public class Activity10 extends Activity
{
private ImageView animationIV;
private Button buttonA, buttonB, buttonC;
private AnimationDrawable animationDrawable;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.test10);
animationIV = (ImageView) findViewById(R.id.animationIV);
buttonA = (Button) findViewById(R.id.buttonA);
buttonB = (Button) findViewById(R.id.buttonB);
buttonC = (Button) findViewById(R.id.buttonC);
buttonA.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
animationIV.setImageResource(R.drawable.animation1);
animationDrawable = (AnimationDrawable) animationIV.getDrawable();
animationDrawable.start();
}
});
buttonB.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
animationDrawable = (AnimationDrawable) animationIV.getDrawable();
animationDrawable.stop();
}
});
buttonC.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
animationIV.setImageResource(R.drawable.animation2);
animationDrawable = (AnimationDrawable) animationIV.getDrawable();
animationDrawable.start();
}
});
}
}
總結(jié):Animation-list是被當(dāng)作一個(gè)drawable對象的,當(dāng)需要開始播放動(dòng)畫的時(shí)候,將drawable取出,然后調(diào)用drawable的start方法。
所以這個(gè)動(dòng)畫的作用對象是drawable。而drawable既可以作為ImageView的ImageResource,也可以作為任意一個(gè)View的background,如果是作為ImageResource,我們用戶getDrawable()取出
如果是作為background,用 getBackground()取出。
總結(jié)
以上是生活随笔為你收集整理的java逐帧动画_android的逐帧动画的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python播放音乐同步歌词_linux
- 下一篇: 手机上的电子词典:PPC版本金山词霸