canvas教程

Android自定义GifView显示gif动画

字号+ 作者:H5之家 来源:H5之家 2015-11-01 08:00 我要评论( )

Android自定义GifView显示gif动画 ,程序员之家论坛

北风网AD


com.ncs.msg.ui.widget; android.content.Context; android.content.res.TypedArray; android.graphics.Canvas; android.graphics.Movie; android.util.AttributeSet; android.view.View; com.ncs.msg.R; java.io.ByteArrayOutputStream; java.io.InputStream; GifView View { Movie ; ; = ; ; ; ; ; GifView(Context context) { (context); init(context, ); } GifView(Context context, AttributeSet set) { (context, set); init(context, set); } init(Context context, AttributeSet set) { TypedArray a = context.obtainStyledAttributes(set, R.styleable.); = a.getResourceId(R.styleable., -); = a.getBoolean(R.styleable., ); = a.getInteger( R.styleable., ); a.recycle(); setFocusable(); java.io.InputStream is; is = context.getResources().openRawResource(); () { = Movie.(is); } { [] array = (is); = Movie.(array, , array.); } } onDraw(Canvas canvas) { (!= ) { (!) { updateAnimationTime(); drawMovieFrame(canvas); invalidate(); } { drawMovieFrame(canvas); } } } updateAnimationTime() { now = android.os.SystemClock.(); (== ) { = now; } dur = .duration(); (dur == ) { dur = ; } = () ((now - ) % dur); } drawMovieFrame(Canvas canvas) { .setTime(); canvas.save(Canvas.); .draw(canvas, , ); canvas.restore(); } setPaused(paused) { .= paused; (!paused) { = android.os.SystemClock.() - ; } invalidate(); } isPaused() { .; } onMeasure(widthMeasureSpec, heightMeasureSpec) { widthMode = MeasureSpec.(widthMeasureSpec); widthSize = MeasureSpec.(widthMeasureSpec); heightMode = MeasureSpec.(heightMeasureSpec); heightSize = MeasureSpec.(heightMeasureSpec); width; height; (widthMode == MeasureSpec.) { width = widthSize; } { desired = () (getPaddingLeft() + .width() + getPaddingRight()); width = Math.(desired, widthSize); } (heightMode == MeasureSpec.) { height = heightSize; } { desired = () (getPaddingTop() + .height() + getPaddingBottom()); height = Math.(desired, heightSize); } setMeasuredDimension(width, height); } [] streamToBytes(InputStream is) { ByteArrayOutputStream os = ByteArrayOutputStream(); [] buffer = []; len; { ((len = is.read(buffer)) >= ) { os.write(buffer, , len); } } (java.io.IOException e) { } os.toByteArray(); } } [/code]


 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
网友点评
"