自己定义控件 播放GIF动画
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center">
代码例如以下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//debug 引用
using System.Diagnostics; namespace CYSoft.TS.UI.StudentInfo
{
public partial class PicboxPlayGif : UserControl
{
private Image m_imgImage = null;
private EventHandler m_evthdlAnimator = null;
public PicboxPlayGif()
{
InitializeComponent();
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.DoubleBuffer, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
//为托付关联一个处理方法
m_evthdlAnimator = new EventHandler(OnImageAnimate);
Debug.Assert(m_evthdlAnimator != null); } private bool isPicboxFit = true;
[Description("pic 的宽和高和 真实gif的宽和高 是否要一致 ture=一致 false=不一致 ")]
public bool _isPicboxFit {
get { return isPicboxFit; }
set { this.isPicboxFit = value;
this.Invalidate();
}
} private int picWidth = 0;
[Description("图片宽度(假设isPicboxFit=true 这个參数无意义)")]
public int _picWidth {
get { return picWidth; }
set {
if (!isPicboxFit) {
if(value!=0)
{
this.picWidth = value;
this.Invalidate();
}
}
}
} private int picHeight = 0;
[Description("图片高度(假设isPicboxFit=true 这个參数无意义)")]
public int _picHeight {
get { return picHeight; }
set {
if (!isPicboxFit)
{
if (value != 0)
{
this.picHeight = value;
this.Invalidate();
}
}
}
} private string imagePath = "C:\\Users\\Thinkpad\\Desktop\\素材\\WaitLoading.gif";
[Description("图片路径")]
public string _imagePath {
get { return imagePath; }
set { this.imagePath = value;
this.Invalidate();
}
} private AA imageLayout = AA.Stretch;
[Description("图片在picbox中的显示方式")]
public AA _imageLayout {
get { return imageLayout; }
set {
this.imageLayout = value;
this.Invalidate();
}
} public enum AA {
None,
Title,
Center,
Stretch,
Zoom
} protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (m_imgImage != null)
{
UpdateImage();
//不用picbox 直接输出
// e.Graphics.DrawImage(m_imgImage, new Rectangle(100, 100, m_imgImage.Width, m_imgImage.Height)); //image 格式
if(imageLayout==AA.None)
{
pic.BackgroundImageLayout = ImageLayout.None;
}else if(imageLayout==AA.Title)
{
pic.BackgroundImageLayout = ImageLayout.Tile;
}
else if (imageLayout == AA.Stretch)
{
pic.BackgroundImageLayout = ImageLayout.Stretch;
}
else if (imageLayout == AA.Zoom)
{
pic.BackgroundImageLayout = ImageLayout.Zoom;
}
else {
pic.BackgroundImageLayout = ImageLayout.Center;
} pic.BackgroundImage = m_imgImage;
}
} protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
//载入图片
m_imgImage = Image.FromFile(imagePath);
if (isPicboxFit)
{
this.Width = m_imgImage.Width;
this.Height = m_imgImage.Height;
}
else
{
this.Width = picWidth;
this.Height = picHeight;
} //pic设置
pic.BackColor = Color.Transparent;
pic.Dock = DockStyle.Fill; //BeginAnimate();
} /// <summary>
/// 播放或停止 动画
/// </summary>
/// <param name="isPlay">true=播放 false=停止</param>
public void _PlayOrEndGif(bool isPlay) {
if (isPlay)
{
BeginAnimate();
}
else {
if (m_imgImage != null)
{
StopAnimate();
//m_imgImage = null;
}
}
} //開始动画
private void BeginAnimate()
{
if (m_imgImage == null)
return; if (ImageAnimator.CanAnimate(m_imgImage))
{
//当gif动画每隔一定时间后,都会变换一帧。那么就会触发一事件,
//该方法就是将当前image每变换一帧时,都会调用当前这个托付所关联的方法。
ImageAnimator.Animate(m_imgImage,m_evthdlAnimator);
}
} //结束动画
private void StopAnimate()
{
if (m_imgImage == null)
return; if (ImageAnimator.CanAnimate(m_imgImage))
{
ImageAnimator.StopAnimate(m_imgImage,m_evthdlAnimator);
}
} //切换图片(帧图片)
private void UpdateImage()
{
if (m_imgImage == null)
return; if (ImageAnimator.CanAnimate(m_imgImage))
{
//获得当前gif动画的下一步须要渲染的帧。当下一步不论什么对当前gif动画的操作都是对该帧进行操作)
ImageAnimator.UpdateFrames(m_imgImage);
}
} private void OnImageAnimate(Object sender,EventArgs e)
{
//使得当前这个winfor重绘,然后去调用该winform的OnPaint()方法进行重绘
this.Invalidate();
} private void PicboxPlayGif_Load(object sender, EventArgs e)
{ }
}
}
调用:
上图右边是属性设置:
播放gif:
picboxPlayGif2._PlayOrEndGif(true);
停止gif:
picboxPlayGif2._PlayOrEndGif(true);
自己定义控件 播放GIF动画的更多相关文章
- 自己定义控件三部曲之动画篇(七)——ObjectAnimator基本使用
前言: 假如生活欺骗了你, 不要悲伤,不要心急! 忧郁的日子里须要镇静: 相信吧,快乐的日子终将会来临! 心儿永远向往着未来: 如今却常是忧郁. 一切都是瞬息,一切都将会过去: 而那过去了的,就会成为 ...
- 自己定义控件三部曲之动画篇(十三)——实现ListView Item进入动画
前言:宝剑锋从磨砺出,梅花香自苦寒来 相关文章: <Android自己定义控件三部曲文章索引>: http://blog.csdn.net/harvic880925/article/det ...
- BillBoardView自己定义控件广告板轮播
BillBoardView自己定义控件广告板轮播 GitHub地址 compile 'com.march.billboardview:billboardview:2.0.6-beta4' BillBo ...
- Android自己定义控件之应用程序首页轮播图
如今基本上大多数的Android应用程序的首页都有轮播图.就是像下图这种(此图为转载的一篇博文中的图.拿来直接用了): 像这种组件我相信大多数的应用程序都会使用到,本文就是自己定义一个这种组件,能够动 ...
- Android自己定义控件:进度条的四种实现方式
前三种实现方式代码出自: http://stormzhang.com/openandroid/2013/11/15/android-custom-loading/ (源代码下载)http://down ...
- Android自己定义控件
今天我们来讲一下 Android中自己定义控件的介绍,在Android中, 我们一般写xml都是用的是单个的控件来完毕的 ,但是.往往在一些项目中.单个控件有时是满足不了的.故此我们能够自己定义控件 ...
- 自己定义控件事实上非常easy1/6
尊重原创转载请注明:From AigeStudio(http://blog.csdn.net/aigestudio)Power by Aige 侵权必究! 炮兵镇楼 上一节我们粗略地讲了下怎样去实现我 ...
- android 自己定义控件
Android自己定义View实现非常easy 继承View,重写构造函数.onDraw.(onMeasure)等函数. 假设自己定义的View须要有自己定义的属性.须要在values下建立attrs ...
- Android 实现形态各异的双向側滑菜单 自己定义控件来袭
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/39670935.本文出自:[张鸿洋的博客] 1.概述 关于自己定义控件側滑已经写了 ...
随机推荐
- SoapUI对于Json数据进行属性值获取与传递
SoapUI的Property Transfer功能可以很好地对接口请求返回的数据进行参数属性获取与传递,但对于Json数据,SoapUI会把数据格式先转换成XML格式,但实际情况却是,转换后的XML ...
- Android N requires the IDE to be running with Java 1.8 or later
Android Studio需要两个JDK: ide jdk和project jdk: 前者是IDE本身运行使用的JDK. 后者用于编译Java代码 Project JDK 可以通过file-&g ...
- 最短路 || POJ 1847 Tram
POJ 1847 最短路 每个点都有初始指向,问从起点到终点最少要改变多少次点的指向 *初始指向的那条边长度为0,其他的长度为1,表示要改变一次指向,然后最短路 =========高亮!!!===== ...
- swift详解之十-------------异常处理、类型转换 ( Any and AnyObject )
异常处理.类型转换 ( Any and AnyObject ) 1.错误处理 (异常处理) swift 提供第一类错误支持 ,包括在运行时抛出 ,捕获 , 传送和控制可回收错误.在swift中 ,错误 ...
- luogu 1608 路径统计--最短路计数
https://www.luogu.org/problemnew/show/P1608 题意https://www.cnblogs.com/rmy020718/p/9440588.html相似,建议还 ...
- POJ-3190-分配畜栏
这个题首先,我们需要注意的是它的时间是一秒,其中还包括了你读入数据的时间,因为cin我写的时候没有解除绑定,所以直接超时,我们直接用scanf函数读入50000组数据好了. 然后就是poj交的时候,如 ...
- phpstorm使用Database关联数据库
1.如图选择mysql 配置数据库连接及shh 点击Test Connection,如果成功,即可点击ok连接mysql数据库 2.如图选择所有的表 3.更新数据
- React深入 - 手写redux api
简介: 手写实现redux基础api createStore( )和store相关方法 api回顾: createStore(reducer, [preloadedState], enhancer) ...
- day22 01 初识面向对象----简单的人狗大战小游戏
day22 01 初识面向对象----简单的人狗大战小游戏 假设有一个简单的小游戏:人狗大战 怎样用代码去实现呢? 首先得有任何狗这两个角色,并且每个角色都有他们自己的一些属性,比如任务名字nam ...
- 大数据学习——实现多agent的串联,收集数据到HDFS中
采集需求:比如业务系统使用log4j生成的日志,日志内容不断增加,需要把追加到日志文件中的数据实时采集到hdfs,使用agent串联 根据需求,首先定义以下3大要素 第一台flume agent l ...