簡體   English   中英

android中的Play.gif圖像而不使用webview

[英]Play.gif image in android without using webview

我這樣嘗試過,我在網上找到了播放gif圖像的方法:

private class MYGIFView extends View {

    Movie movie;
    InputStream is = null;
    long moviestart;

    public MYGIFView(Context context) {
        super(context);

        // Provide your own gif animation file

        is = context.getResources().openRawResource(R.drawable.mygif);
        movie = Movie.decodeStream(is);

    }

    @Override
    protected void onDraw(Canvas canvas) {

        canvas.drawColor(Color.WHITE);
        super.onDraw(canvas);
        long now = android.os.SystemClock.uptimeMillis();
        System.out.println("now=" + now);
        if (moviestart == 0) { // first time
            moviestart = now;

        }
        System.out.println("\tmoviestart=" + moviestart);
        int relTime = (int) ((now - moviestart) % movie.duration());
        System.out.println("time=" + relTime + "\treltime="
                + movie.duration());
        movie.setTime(relTime);
        movie.draw(canvas, this.getWidth() / 2 - 20,
                this.getHeight() / 2 - 40);
        this.invalidate();
    }

盡管許多人說他們取得了理想的結果。

我把gif圖像放在了drawable中。 我將movie.duration()設為null。

請提出我在哪里錯或是否有任何辦法。

沒有WebView Android無法播放GIF文件。 您必須將其分解為多個幀並自己制作動畫。

我在另一個StackOverflow上發現了XoyoSoft的這一軟件 GifSplitter,它可以將GIF分割成多個幀。 然后,您將需要使用AnimationDrawable來組合它們。

它看起來像這樣:

// Your files:
res\drawable-xxxx\frame1.jpg
res\drawable-xxxx\frame2.jpg
res\drawable-xxxx\frame3.jpg
// ...
res\drawable-xxxx\frame99.jpg

然后,上面的AnimationDrawable文檔中有一個示例,該示例將顯示如何顯示這些圖像。

最后,您必須加載並播放動畫。 這在AnimationDrawable文檔中也有詳細介紹。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM