📄 animationthread.java
字号:
/*
* LumaQQ - Java QQ Client
*
* Copyright (C) 2004 luma <stubma@163.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package edu.tsinghua.lumaqq.widgets.rich;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.ImageLoader;
/**
* 动画线程
*
* @author luma
*/
public class AnimationThread extends Thread {
private RichBox richbox;
private ImageResolver resolver;
public static final int MAX = 5;
private Integer[] keys;
private int[] frames;
private int[] nextFrameTime;
private GC[] gc;
private int time;
private int pos;
// 停止标志
private volatile boolean stop;
public AnimationThread(RichBox richbox, ImageResolver resolver) {
this.richbox = richbox;
this.resolver = resolver;
keys = new Integer[MAX];
frames = new int[MAX];
nextFrameTime = new int[MAX];
gc = new GC[MAX];
pos = 0;
stop = false;
}
/*
* (non-Javadoc)
*
* @see java.lang.Thread#run()
*/
public void run() {
while(!isStop()) {
try {
// wait frame
synchronized(this) {
wait(200);
}
} catch (InterruptedException e) {
if(isStop())
break;
}
time += 200;
// 检查richbox是否已经dispose
if(richbox == null || richbox.isDisposed())
break;
richbox.getDisplay().asyncExec(new Runnable() {
public void run() {
try {
for(int i = 0; i < MAX; i++) {
if(keys[i] == null)
continue;
// 如果下一帧的时间没到,跳过
if(nextFrameTime[i] > time)
continue;
// 得到image data
int tag = keys[i].intValue() >>> 16;
int code = keys[i].intValue() & 0xFFFF;
ImageLoader loader = resolver.getImageLoader(tag, code);
ImageData frameData = loader.data[frames[i]];
ImageData previous = (frames[i] > 0) ? loader.data[frames[i] - 1] : loader.data[loader.data.length - 1];
Image frame = new Image(richbox.getDisplay(), frameData);
if(previous.disposalMethod == SWT.DM_FILL_BACKGROUND)
gc[i].fillRectangle(previous.x, previous.y, previous.width, previous.height);
else if(previous.disposalMethod == SWT.DM_FILL_PREVIOUS) {
Image previousImage = new Image(richbox.getDisplay(), previous);
gc[i].drawImage(previousImage, 0, 0, previous.width, previous.height, previous.x, previous.y, previous.width, previous.height);
previousImage.dispose();
}
if(frames[i] == 0)
gc[i].fillRectangle(frame.getBounds());
gc[i].drawImage(frame, 0, 0, frameData.width, frameData.height, frameData.x, frameData.y, frameData.width, frameData.height);
frame.dispose();
frames[i] = (frames[i] + 1) % loader.data.length;
nextFrameTime[i] = time + frameData.delayTime * 10;
}
richbox.redraw();
} catch (Throwable e) {
}
}
});
}
}
/**
* 释放资源
*/
public synchronized void dispose() {
for(int i = 0; i < MAX; i++) {
if(gc[i] != null)
gc[i].dispose();
}
stop = true;
notify();
}
/**
* @return Returns the stop.
*/
public synchronized boolean isStop() {
return stop;
}
/**
* 增加需要动画的表情key
*
* @param key
* @param gc
*/
public synchronized void putImage(Integer key, GC context) {
keys[pos] = key;
frames[pos] = 0;
nextFrameTime[pos] = 0;
if(gc[pos] != null) {
gc[pos].dispose();
gc[pos] = null;
}
gc[pos] = context;
pos++;
if(pos == MAX)
pos = 0;
}
/**
* @param key
*/
public synchronized void remove(Integer key) {
for(int i = 0; i < MAX; i++) {
if(key.equals(keys[i])) {
keys[i] = null;
if(gc[i] != null) {
gc[i].dispose();
gc[i] = null;
}
return;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -