在android中使用GIF图像的方法和类

时间:2016-08-31 08:38:44

标签: android gif

我想在GIF中使用android张图片。 我找不到合适的教程来定义在android中使用GIF图像时使用哪些类或方法 我想点击一个按钮在Android中播放相应的GIF图像?

我是新来的android请帮忙。

2 个答案:

答案 0 :(得分:0)

最简单的解决方案是使用Glide库。 还存在支持GIF的WebView解决方案,但我不推荐它。

答案 1 :(得分:0)

  

您可以在不使用任何库的情况下播放GIF图像。请参阅   以下代码:

的xml:

  <com.jaldihealthy.SampleView
        android:id="@+id/gif_view"
        android:layout_width="match_parent"
        android:layout_centerInParent="true"
        android:layout_height="300dp"/>

SampleView类:

public class SampleView extends View {

    private Movie mMovie;
    private long mMovieStart;
    private ProgressDialog mProgressDialog;
    Context context;

    public SampleView(Context context) {
        super(context);
        setFocusable(true);

    }

    public void setmMovie(String url,Context context) {
        this.context=context;
        new RetrieveFeedTask().execute(url);
    }

    public SampleView(Context context, AttributeSet attrSet) {
        super(context, attrSet);
        setFocusable(true);

      /*  java.io.InputStream is;
        is = context.getResources().openRawResource(R.drawable.girl_dances);
        mMovie = Movie.decodeStream(is);*/
    }

    public SampleView(Context context, AttributeSet attrSet, int defStyle) {
        super(context, attrSet, defStyle);
        setFocusable(true);

       /* java.io.InputStream is;
        is = context.getResources().openRawResource(R.drawable.girl_dances);
        mMovie = Movie.decodeStream(is);*/
    }
    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawColor(0x00000000);

        Paint p = new Paint();
        p.setAntiAlias(true);

        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, getWidth() / 2 - mMovie.width() / 2,
                    getHeight() / 2 - mMovie.height() / 2);
            invalidate();
        }
    }


    public void showDialog() {
        mProgressDialog = new ProgressDialog(context, R.style.MyTheme);
        mProgressDialog.setMessage("Loading...");
        mProgressDialog.setProgressStyle(android.R.style.Widget_ProgressBar_Small);
        mProgressDialog.show();
        mProgressDialog.setContentView(R.layout.progress_layout);
        mProgressDialog.setCancelable(false);
    }

    public void dismissDialog() {
        if (mProgressDialog != null && mProgressDialog.isShowing()) {
            mProgressDialog.dismiss();
        }
    }


    class RetrieveFeedTask extends AsyncTask<String, Void, InputStream> {

        private Exception exception;
        InputStream is;

        @Override
        protected void onPreExecute() {
            showDialog();
            super.onPreExecute();
        }

        protected InputStream doInBackground(String... urls) {
            try {
                String url = urls[0];
                SAXParserFactory factory = SAXParserFactory.newInstance();
                SAXParser parser = factory.newSAXParser();
                XMLReader xmlreader = parser.getXMLReader();

                is =  new URL(url).openStream();
                mMovie = Movie.decodeStream(is);
            } catch (Exception e) {
                this.exception = e;

                return null;
            }
            return is;
        }

        protected void onPostExecute(InputStream is) {
            // TODO: check this.exception
            // TODO: do something with the feed
            dismissDialog();
            invalidate();
        }
    }
    }

传递你的gif网址:

 gif_view.setmMovie(url,mContext);