如何在安卓设备上启动和停止gif图片

3

在我的应用程序中,我想播放gif图片。我能够播放gif图片,但我想像处理其他图片一样处理gif图片:如果我点击开始按钮,动画应该开始,如果我点击停止按钮,动画应该停止。

     strtbtn.setOnClickListener(this);

      public void onClick(View v) {
        // TODO Auto-generated method stub
        Canvas canvas=new Canvas(mBitmap);
        long now = android.os.SystemClock.uptimeMillis();
        if (mMovieStart == 0) {   // first time
            mMovieStart = now;
        }
        if (mMovie != null) {
            int dur = mMovie.duration();
            if (dur == 0) {
                dur = 1000;
            }
            int relTime = (int)((now - mMovieStart) % dur);
            mMovie.setTime(relTime);
            mMovie.draw(canvas, mBitmap2.getWidth() - mMovie.width(),
                        mBitmap2.getHeight() - mMovie.height());
      //                invalidate();
            Log.i("movie",""+mMovieStart);
            Log.i("jjhjhhnjhj",""+mMovie);
        }
    }

请帮我学习如何启动和停止GIF动画图片。提前感谢。


http://www.bogotobogo.com/Android/android19Animation.php - Android
1个回答

0

步骤1

找到你想要用于动画的图片。它们应该是按顺序排列的,这样我们才能将其连续翻转。图片文件类型如GIF、PNG等。找到后,通过复制和粘贴将它们放置在

"你的项目" -> res -> drawable-mdpi -> 粘贴图片

Drawable有三个类别

hdpi-> high dots per inch
ldpi->large dots per inch
mdpi->medium dots per inch

第二步

执行此动画有两种方法:1)通过创建“BitmapDrawable”对象;2)通过创建“animation” xml文件。我们将逐一查看这两种方法。

使用“BitmapDrawable”进行操作

Syntax:

BitmapDrawable object=(BitmapDrawable) getResources().getDrawable(R.drawable.<image_name>);

Example

Suppose I have image named "sample.png" in "res.drawable" directory, then



 BitmapDrawable frame1=(BitmapDrawable)getResources().getDrawable(R.drawable.sample);


 AnimationDrawable animation = new AnimationDrawable();

Now, we can add frames which we have created above as like you want.

Syntax:



 AnimationDrawable.addFrame(BitmapDrawable,duration);

Example



 animation.addFrame(frame1,1000);

In this animation, while you start this animation, it will stop after last added frame. If you like to continue this animation, following method must be implemented.

animation.setOneShot(false);

Now, bind "ImageView" object of layout



  ImageView img=(ImageView)findViewById(R.id.imageView1);
    img.setBackgroundDrawable(animation);   



    animation.start();
    animation.stop();

Description:
    Duration is in milliseconds.
    setBackgroundDrawable(animation) method will set background with animation and give control over animation.
    start() method will start your animation and stop() will stop this animation.

  <?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/sample1" android:duration="50"/>
      <item android:drawable="@drawable/sample2" android:duration="50"/>
      <item android:drawable="@drawable/sample3" android:duration="50"/>
    </animation-list>

In above code, I have added 3 frames with "android:drawable" attribute and duration in milliseconds.

Now, bind "ImageView" object with layout and implement following methods.



  ImageView img=(ImageView)findViewById(R.id.imageView1);
    img.setImageBitmap(null);
    img.setBackgroundResource(R.anim.animation);

Next, create object of "AnimatinDrawable" and give background to "AnimationDrawable" so that it can handle it.



  AnimationDrawable animation = (AnimationDrawable) img.getBackground();
    animation.start();
    animation.stop();

使用 "BitmapDrawable"

Main.java

package com.animationapp;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class AnimationAppActvity extends Activity {
    /** Called when the activity is first created. */

    ImageView img;
    Button btnStart,btnStop;
    AnimationDrawable animation;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        int duration = 150;
        img = (ImageView)findViewById(R.id.imageView1);

        BitmapDrawable frame1 =
            (BitmapDrawable)getResources().getDrawable(R.drawable.d1);
        BitmapDrawable frame2 =
            (BitmapDrawable)getResources().getDrawable(R.drawable.d2);
        BitmapDrawable frame3 =
            (BitmapDrawable)getResources().getDrawable(R.drawable.d3);       

        animation = new AnimationDrawable();       
        animation.addFrame(frame1, duration);
        animation.addFrame(frame2, duration);
        animation.addFrame(frame3, duration);

        animation.setOneShot(false);

        img.setBackgroundDrawable(animation);

        btnStart = (Button)findViewById(R.id.btnStart);
        btnStop = (Button)findViewById(R.id.btnStop);

        btnStart.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                  // TODO Auto-generated method stub
                  animation.start();
            }
        });

        btnStop.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
             // TODO Auto-generated method stub
             animation.stop();
             }
        });
    }
} 

布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
<ImageView
      android:layout_height="wrap_content"
      android:id="@+id/imageView1"
      android:layout_width="wrap_content"
      android:minHeight="191px" android:minWidth="285px">
</ImageView>
<Button
      android:text="Start"
      android:id="@+id/btnStart"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content">
</Button>
<Button
      android:text="Stop"
      android:id="@+id/btnStop"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content">
</Button>
</LinearLayout> 

使用 "animation.xml" 文件。
package com.AnimApp;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView; 

public class AnimAppActivity extends Activity {
    /** Called when the activity is first created. */

      Button btnStart,btnStop;
      ImageView img;
      AnimationDrawable animation;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        btnStart=(Button)findViewById(R.id.button1);
        btnStop=(Button)findViewById(R.id.button2);

        img=(ImageView)findViewById(R.id.imageView1);       
        img.setBackgroundResource(R.anim.animation);
        animation=(AnimationDrawable)img.getBackground();

        btnStart.setOnClickListener(new View.OnClickListener() {   
              @Override
              public void onClick(View v) {
                   // TODO Auto-generated method stub
                   animation.start();
              }
        });

        btnStop.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
            // TODO Auto-generated method stub
            animation.stop();
            }
        });
    }
}

animation.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/d1" android:duration="50"/>
  <item android:drawable="@drawable/d2" android:duration="50"/>
  <item android:drawable="@drawable/d3" android:duration="50"/>
</animation-list>

1
感谢您的回复,但我使用的是.gif格式的图像,我想启动和停止GIF图像而不是.png。 - user1105975

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接