欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

Android显示GIF图片实例代码

时间:2020-11-03 13:34:02|栏目:Android代码|点击:

最近.一个朋友跟我说想,我给她弄个闹钟APP软件...功能其实很简单...只需要弄个简单的闹钟.自己设计设计时间.然后时间到了的时候,闹铃放的声音是男朋友录制好的声音...于是就开始整了....

第一次做这种的时候.我想把首页界面做成一个GIF动画特效的.这样看起来可爱点...适合妹子用,于是就有了下面这个效果图

上代码

ndroid中的Android.graphics.Movie 这个类,这是android提供给我们的一个非常方便的工具。
首先,重写控件View,自定义一个展示gif图的GifView
Movie其实管理着GIF动画中的多个帧,只需要通过 setTime() 一下就可以让它在draw()的时候绘出相应的那帧图像。通过当前时间与duration之间的换算关系,是很容易实现GIF动起来的效果。

public class GifView extends View { 
 /** 
 * 默认为1秒 
 */ 
 private static final int DEFAULT_MOVIE_DURATION = 1000; 
 private int mMovieResourceId; 
 private Movie mMovie; 
 private long mMovieStart; 
 private int mCurrentAnimationTime = 0; 
 private float mLeft; 
 private float mTop; 
 private float mScale; 
 private int mMeasuredMovieWidth; 
 private int mMeasuredMovieHeight; 
 private boolean mVisible = true; 
 private volatile boolean mPaused = false; 
 public GifView(Context context) { 
 this(context, null); 
 } 
 public GifView(Context context, AttributeSet attrs) { 
 this(context, attrs, R.styleable.CustomTheme_gifViewStyle); 
 } 
 public GifView(Context context, AttributeSet attrs, int defStyle) { 
 super(context, attrs, defStyle); 
 setViewAttributes(context, attrs, defStyle); 
 } 
 @SuppressLint("NewApi") 
 private void setViewAttributes(Context context, AttributeSet attrs, 
  int defStyle) { 
 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 
  setLayerType(View.LAYER_TYPE_SOFTWARE, null); 
 } 
 // 从描述文件中读出gif的值,创建出Movie实例 
 final TypedArray array = context.obtainStyledAttributes(attrs, 
  R.styleable.GifView, defStyle, R.style.Widget_GifView); 
 mMovieResourceId = array.getResourceId(R.styleable.GifView_gif, -1); 
 mPaused = array.getBoolean(R.styleable.GifView_paused, false); 
 array.recycle(); 
 if (mMovieResourceId != -1) { 
  mMovie = Movie.decodeStream(getResources().openRawResource( 
   mMovieResourceId)); 
 } 
 } 
 /** 
 * 设置gif图资源 
 * 
 * @param movieResId 
 */ 
 public void setMovieResource(int movieResId) { 
 this.mMovieResourceId = movieResId; 
 mMovie = Movie.decodeStream(getResources().openRawResource( 
  mMovieResourceId)); 
 requestLayout(); 
 } 
 public void setMovie(Movie movie) { 
 this.mMovie = movie; 
 requestLayout(); 
 } 
 public Movie getMovie() { 
 return mMovie; 
 } 
 public void setMovieTime(int time) { 
 mCurrentAnimationTime = time; 
 invalidate(); 
 } 
 /** 
 * 设置暂停 
 * 
 * @param paused 
 */ 
 public void setPaused(boolean paused) { 
 this.mPaused = paused; 
 if (!paused) { 
  mMovieStart = android.os.SystemClock.uptimeMillis() 
   - mCurrentAnimationTime; 
 } 
 invalidate(); 
 } 
 /** 
 * 判断gif图是否停止了 
 * 
 * @return 
 */ 
 public boolean isPaused() { 
 return this.mPaused; 
 } 
 @Override 
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
 if (mMovie != null) { 
  int movieWidth = mMovie.width(); 
  int movieHeight = mMovie.height(); 
  int maximumWidth = MeasureSpec.getSize(widthMeasureSpec); 
  float scaleW = (float) movieWidth / (float) maximumWidth; 
  mScale = 1f / scaleW; 
  mMeasuredMovieWidth = maximumWidth; 
  mMeasuredMovieHeight = (int) (movieHeight * mScale); 
  setMeasuredDimension(mMeasuredMovieWidth, mMeasuredMovieHeight); 
 } else { 
  setMeasuredDimension(getSuggestedMinimumWidth(), 
   getSuggestedMinimumHeight()); 
 } 
 } 
 @Override 
 protected void onLayout(boolean changed, int l, int t, int r, int b) { 
 super.onLayout(changed, l, t, r, b); 
 mLeft = (getWidth() - mMeasuredMovieWidth) / 2f; 
 mTop = (getHeight() - mMeasuredMovieHeight) / 2f; 
 mVisible = getVisibility() == View.VISIBLE; 
 } 
 @Override 
 protected void onDraw(Canvas canvas) { 
 if (mMovie != null) { 
  if (!mPaused) { 
  updateAnimationTime(); 
  drawMovieFrame(canvas); 
  invalidateView(); 
  } else { 
  drawMovieFrame(canvas); 
  } 
 } 
 } 
 @SuppressLint("NewApi") 
 private void invalidateView() { 
 if (mVisible) { 
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { 
  postInvalidateOnAnimation(); 
  } else { 
  invalidate(); 
  } 
 } 
 } 
 private void updateAnimationTime() { 
 long now = android.os.SystemClock.uptimeMillis(); 
 // 如果第一帧,记录起始时间 
 if (mMovieStart == 0) { 
  mMovieStart = now; 
 } 
 // 取出动画的时长 
 int dur = mMovie.duration(); 
 if (dur == 0) { 
  dur = DEFAULT_MOVIE_DURATION; 
 } 
 // 算出需要显示第几帧 
 mCurrentAnimationTime = (int) ((now - mMovieStart) % dur); 
 } 
 private void drawMovieFrame(Canvas canvas) { 
 // 设置要显示的帧,绘制即可 
 mMovie.setTime(mCurrentAnimationTime); 
 canvas.save(Canvas.MATRIX_SAVE_FLAG); 
 canvas.scale(mScale, mScale); 
 mMovie.draw(canvas, mLeft / mScale, mTop / mScale); 
 canvas.restore(); 
 } 
 @SuppressLint("NewApi") 
 @Override 
 public void onScreenStateChanged(int screenState) { 
 super.onScreenStateChanged(screenState); 
 mVisible = screenState == SCREEN_STATE_ON; 
 invalidateView(); 
 } 
 @SuppressLint("NewApi") 
 @Override 
 protected void onVisibilityChanged(View changedView, int visibility) { 
 super.onVisibilityChanged(changedView, visibility); 
 mVisible = visibility == View.VISIBLE; 
 invalidateView(); 
 } 
 @Override 
 protected void onWindowVisibilityChanged(int visibility) { 
 super.onWindowVisibilityChanged(visibility); 
 mVisible = visibility == View.VISIBLE; 
 invalidateView(); 
 } 
 
} 

其次,在xml布局文件中,把这个view定义进去,代码如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:layout_width="fill_parent" 
  android:layout_height="fill_parent" 
  android:background="#ffffff" 
  android:gravity="center" 
  android:orientation="vertical" 
 > 
 
 <com.example.alarmtest.GifView 
 android:id="@+id/gif1" 
 android:layout_width="300dp" 
 android:layout_height="300dp" 
 android:layout_gravity="center_horizontal" 
 android:enabled="false"/> 
 <Button 
 android:id="@+id/bu" 
 android:text="进入" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:layout_gravity="center_horizontal"/> 
 
</LinearLayout> 

最后,在MainActivity中的使用,代码如下:

public class FirstActivity extends Activity { 
 private GifView gif; 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
 super.onCreate(savedInstanceState); 
 setContentView(R.layout.activity_first); 
 gif = (GifView) findViewById(R.id.gif1); 
 // 设置背景gif图片资源 
 gif.setMovieResource(R.raw.a123); 
 } 
} 

Styles.xml

<declare-styleable name="GifView"> 
 <attr name="gif" format="reference" /> 
 <attr name="paused" format="boolean" /> 
 </declare-styleable> 
 <declare-styleable name="CustomTheme"> 
 <attr name="gifViewStyle" format="reference" /> 
 </declare-styleable> 
 <style name="Widget_GifView"></style> 
 

raw是res下面的一个文件放的是GIF动画图噢

我这里放了一个动画图 跟一个 音乐文件,因为我要做的是一个有声音的闹铃

上一篇:Android Camera是否支持变焦的判断方法总结

栏    目:Android代码

下一篇:Android解析JSON数据的方法分析

本文标题:Android显示GIF图片实例代码

本文地址:http://www.codeinn.net/misctech/18728.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有