日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

8.4.1 Android動(dòng)畫合集之幀動(dòng)畫

 小飛苑 2017-01-04

本節(jié)引言:

從本節(jié)開始我們來(lái)探究Android中的動(dòng)畫,畢竟在APP中添加上一些動(dòng)畫,會(huì)讓我們的應(yīng)用變得 很炫,比如最簡(jiǎn)單的關(guān)開Activity,當(dāng)然自定義控件動(dòng)畫肯定必不可少啦~而Android中的動(dòng)畫 分為三大類,逐幀動(dòng)畫(Frame)以及補(bǔ)間動(dòng)畫(Tween),還有Android 3.0以后引入的屬性動(dòng)畫 (Property),而本節(jié)給大家?guī)?lái)的是第一種動(dòng)畫——逐幀動(dòng)畫的一個(gè)基本使用~


1.幀動(dòng)畫概念以及用法

幀動(dòng)畫非常容易理解,其實(shí)就是簡(jiǎn)單的由N張靜態(tài)圖片收集起來(lái),然后我們通過(guò)控制依次顯示 這些圖片,因?yàn)槿搜?視覺殘留"的原因,會(huì)讓我們?cè)斐蓜?dòng)畫的"錯(cuò)覺",跟放電影的原理一樣!

而Android中實(shí)現(xiàn)幀動(dòng)畫,一般我們會(huì)用到前面講解到的一個(gè)Drawable:AnimationDrawable 先編寫好Drawable,然后代碼中調(diào)用start()以及stop()開始或停止播放動(dòng)畫~

當(dāng)然我們也可以在Java代碼中創(chuàng)建逐幀動(dòng)畫,創(chuàng)建AnimationDrawable對(duì)象,然后調(diào)用 addFrame(Drawable frame,int duration)向動(dòng)畫中添加幀,接著調(diào)用start()和stop()而已~

下面我們來(lái)寫兩個(gè)例子體會(huì)下幀動(dòng)畫的效果以及熟悉下用法


2.使用示例:

示例一:最簡(jiǎn)單的例子

運(yùn)行效果圖

代碼實(shí)現(xiàn)

首先編寫我們的動(dòng)畫文件,非常簡(jiǎn)單,先在res下創(chuàng)建一個(gè)anim目錄,接著開始擼我們的 動(dòng)畫文件:miao_gif.xml: 這里的android:oneshot是設(shè)置動(dòng)畫是否只是播放一次,true只播放一次,false循環(huán)播放!

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas./apk/res/android"
    android:oneshot="false">
    <item
        android:drawable="@mipmap/img_miao1"
        android:duration="80" />
    <item
        android:drawable="@mipmap/img_miao2"
        android:duration="80" />
    <item
        android:drawable="@mipmap/img_miao3"
        android:duration="80" />
    <!--限于篇幅,省略其他item,自己補(bǔ)上-->
    ...
</animation-list>

動(dòng)畫文件有了,接著到我們的布局文件:activity_main.xml

<LinearLayout xmlns:android="http://schemas./apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/btn_start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="開始" />

    <Button
        android:id="@+id/btn_stop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="停止" />

    <ImageView
        android:id="@+id/img_show"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:layout_gravity="center"
        android:background="@anim/miao_gif" />
    
</LinearLayout>

最后是我們的MainActivity.java,這里在這里控制動(dòng)畫的開始以及暫停:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btn_start;
    private Button btn_stop;
    private ImageView img_show;
    private AnimationDrawable anim;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bindViews();
        anim = (AnimationDrawable) img_show.getBackground();
    }

    private void bindViews() {
        btn_start = (Button) findViewById(R.id.btn_start);
        btn_stop = (Button) findViewById(R.id.btn_stop);
        img_show = (ImageView) findViewById(R.id.img_show);
        btn_start.setOnClickListener(this);
        btn_stop.setOnClickListener(this);
    }
    
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_start:
                anim.start();
                break;
            case R.id.btn_stop:
                anim.stop();
                break;
        }
    }
}

好的,非常的簡(jiǎn)單哈~


示例二:在指定地方播放幀動(dòng)畫

運(yùn)行效果圖

代碼實(shí)現(xiàn)

依舊是先上我們的動(dòng)畫文件:anim_zhuan.xml

<animation-list xmlns:android="http://schemas./apk/res/android"
    android:oneshot="true">
    <item
        android:drawable="@mipmap/img_zhuan1"
        android:duration="80" />
    <item
        android:drawable="@mipmap/img_zhuan2"
        android:duration="80" />
    <item
        android:drawable="@mipmap/img_zhuan3"
        android:duration="80" />
     <!--限于篇幅,省略其他item,自己補(bǔ)上-->
    ...
</animation-list> 

接著我們來(lái)寫一個(gè)自定義的ImageView:FrameView.java,這里通過(guò)反射獲得當(dāng)前播放的幀, 然后是否為最后一幀,是的話隱藏控件!

/**
 * Created by Jay on 2015/11/15 0015.
 */
public class FrameView extends ImageView {

    private AnimationDrawable anim;

    public FrameView(Context context) {
        super(context);
    }

    public FrameView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public FrameView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setAnim(AnimationDrawable anim){
        this.anim = anim;
    }

    public void setLocation(int top,int left){
        this.setFrame(left,top,left + 200,top + 200);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        try{
            //反射調(diào)用AnimationDrawable里的mCurFrame值
            Field field = AnimationDrawable.class
                    .getDeclaredField("mCurFrame");
            field.setAccessible(true);
            int curFrame = field.getInt(anim);// 獲取anim動(dòng)畫的當(dāng)前幀
            if (curFrame == anim.getNumberOfFrames() - 1)// 如果已經(jīng)到了最后一幀
            {
                //讓該View隱藏
                setVisibility(View.INVISIBLE);
            }
        }catch (Exception e){e.printStackTrace();}
        super.onDraw(canvas);
    }
}

最后是我們的MainActivity.java,創(chuàng)建一個(gè)FrameLayout,添加View,對(duì)觸摸事件中按下的 事件做處理,顯示控件以及開啟動(dòng)畫~

public class MainActivity extends AppCompatActivity {

    private FrameView fView;
    private AnimationDrawable anim = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FrameLayout fly = new FrameLayout(this);
        setContentView(fly);
        fView = new FrameView(this);
        fView.setBackgroundResource(R.anim.anim_zhuan);
        fView.setVisibility(View.INVISIBLE);
        anim = (AnimationDrawable) fView.getBackground();
        fView.setAnim(anim);
        fly.addView(fView);
        fly.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                //設(shè)置按下時(shí)才產(chǎn)生動(dòng)畫效果
                if(event.getAction() == MotionEvent.ACTION_DOWN){
                    anim.stop();
                    float x = event.getX();
                    float y = event.getY();
                    fView.setLocation((int) y - 40,(int)x-20);  //View顯示的位置
                    fView.setVisibility(View.VISIBLE);
                    anim.start();    //開啟動(dòng)畫
                }
                return false;
            }
        });
    }
}

好的,同樣很簡(jiǎn)單哈~


3.本節(jié)示例代碼和Gif幀提取工具下載

AnimationDemo1.zip

AnimationDemo2.zip

Gif幀提取工具


本節(jié)小結(jié):

好的,本節(jié)給大家介紹了一下Android三種動(dòng)畫中最簡(jiǎn)單的幀動(dòng)畫,實(shí)際開發(fā)用的并不多 不過(guò)學(xué)多點(diǎn)東西,以后也多個(gè)思路嘛~好的,本節(jié)就到這里,謝謝~

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多