Android supports imagview and androidimagview of Gif dynamic images.

Source: Internet
Author: User

Android supports imagview and androidimagview of Gif dynamic images.

Android supports gif components and can control the lower-case and location. Suo Luo's online method is simply implemented.


Method 1. Use Movie to parse gif streams

This is also the method used in this article. There are a lot of blog posts about how to do this. The process is not difficult. There are several issues to consider: 1. How to control the size of the new component; 2. How to control the gif animation size; 3. How to control the position of the gif in the component.

This article will introduce the method here first post source code: https://github.com/HandsomeL/android_gif_imageview

Method 2. parse gif to obtain the bitmap

Using a separate thread management switch, this method has also been implemented, and it feels that the memory consumption is still relatively large, and the playback is not as smooth as imagined.


1. First, GifImageView inherits from ImageView and is compatible with all methods and attributes of ImageView.

Use Movie to read the InputStream of gif and determine whether it is a static image or a dynamic image in Ondraw (). Different pasters are called.

If it is a static graph, it is thrown to super; otherwise, the animation Rendering Method playMovie (canvas) is called );

This article adds a control mechanism to play the video only when selected. The first one is displayed in the unselected status.

Code:

protected void onDraw(Canvas canvas) {if (mMovie == null) {// src is classical imageView;super.onDraw(canvas);} else {int saveCount = canvas.getSaveCount();if (mDrawMatrix != null && !mDrawMatrix.isIdentity()) {canvas.concat(mDrawMatrix);}if (isSelected()) {playMovie(canvas);invalidate();} else {mMovie.setTime(0);mMovie.draw(canvas, 0, 0);}canvas.restoreToCount(saveCount);}}

PlayMovie (canvas); method code:

Comment in the Code:

Private boolean playMovie (Canvas canvas) {long now = SystemClock. uptimeMillis (); // obtain the relatively good time if (mStartTime = 0) {mStartTime = now;} int duration = mMovie. duration (); if (duration = 0) {duration = 1000;} int relTime = (int) (now-mStartTime) % duration ); // calculate the time relative to the start playback time to determine the position of the image to be drawn. setTime (relTime); // sets the relative position of mMovie. draw (canvas, 0, 0); // draw if (now-mStartTime)> = duration) {// after a playback is completed, true is returned. The program is used, ignore mStartTime = 0; return true;} return false ;}


Ii. gif size and position

This document uses the matrix for processing. The code is simple and easy to understand.

Not familiar with matrix operations please stamp here: http://www.apkbus.com/forum.php? Mod = viewthread & tid = 58458.


Private void calculateMatrix () {if (mMovie = null) {return;} int movieWidth = mMovie. width (); // The actual pixel width and height int movieHeight = mMovie. height (); int vWidth = getMeasuredWidth ()-getPaddingLeft ()-getPaddingRight (); int vHeight = getMeasuredHeight ()-getPaddingTop ()-getPaddingBottom (); mDrawMatrix = new Matrix (); float scale; scale = Math. min (float) vWidth/(float) movieWidth, (float) vHeight/(float) movieHeight); float dx = (int) (vWidth-movieWidth * scale) * 0.5f + 0.5f); float dy = (int) (vHeight-movieHeight * scale) * 0.5f + 0.5f); mDrawMatrix. setScale (scale, scale); // sets the mDrawMatrix scale. postTranslate (dx, dy); // sets the translation}

Your program does not use custom attributes and is directly set through setGif. If necessary, you can refer to the implementation method in the source code.


I hope to point out the incorrect expression ~



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.