📄 chatcontrol.java
字号:
package com.tianxia.qipai.view.util;
import java.io.IOException;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.game.TiledLayer;
public class ChatControl {
private Sprite upsprite,downsprite; //聊天内容上下滚动控制键 图层
private TiledLayer contentlayer; //显示聊天内容 图层
private TiledLayer contentrect; //整个控件的图层框
private LayerManager lm; //图层管理器
private Font font; //字体
private int recordnum = 20; //保留的记录数
private final int distance = 1; //聊天内容和框的距离
private int maxrow; //最多显示行数
private int firi; //聊天框第一行显示的单元格编号
private int currdraw; //当前画的聊天内容的单元格编号
private int mostnum; //每行显示的最多字数
private int orignX = 0; //控件的位置
private int orignY = 0;
private final int updownwidth = 16;
private final int updownheight = 13;
private int upwidth = 0; //上翻图标宽度
private int upheight = 0; //上翻图标高度
private int downwidth = 0;//下翻图标宽度
private int downheight = 0;//下翻图标高度
private int rectwidth = 0; //控件的宽度
private int rectheight = 0; //控件的高度
private int contentwidth = 0;
private int contentheight = 0;
private boolean chatwordflag[]; //对应1。。40单元格的聊天内容
private boolean currflag; //是否已设置当前显示的内容
private Runtime rt = Runtime.getRuntime();
public ChatControl(LayerManager lm,Font font,int rectwidth,int rectheight) {
super();
this.lm = lm;
this.font = font;
this.rectwidth = rectwidth;
this.rectheight = rectheight;
Image upimage = null;
Image downimage = null;
Image updownimage = null;
Image tempimage = null;
//将聊天内容标志全部设为false
try {
updownimage = Image.createImage("/png/updownimage.png");
tempimage = Image.createImage(updownwidth,updownheight);
Graphics usergra = tempimage.getGraphics();
usergra.drawImage(updownimage,0,0,Graphics.TOP|Graphics.LEFT);
downimage=this.setImageAlph(tempimage);
tempimage = null;
usergra = null;
tempimage = Image.createImage(updownwidth,updownheight);
usergra = tempimage.getGraphics();
usergra.drawImage(updownimage,-1*updownwidth,0,Graphics.TOP|Graphics.LEFT);
upimage=this.setImageAlph(tempimage);
tempimage = null;
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
upwidth = upimage.getWidth();
upheight = upimage.getHeight();
downwidth = downimage.getWidth();
downheight = downimage.getHeight();
Image rectimage = Image.createImage(rectwidth,rectheight);
Graphics rectgra = rectimage.getGraphics();
rectgra.drawRect(0,0,rectwidth-1,rectheight-1);
rectimage = setImageAlph(rectimage);
contentrect = new TiledLayer(1,1,rectimage,rectwidth,rectheight);
contentrect.setCell(0,0,1);
lm.insert(contentrect,0); //插入整个控件的图层框
contentrect.setVisible(false);
//上翻图层
upsprite = new Sprite(upimage,upwidth,upheight);
lm.insert(upsprite,0);
upsprite.setVisible(false);
//上翻图层
downsprite = new Sprite(downimage,downwidth,downheight);
lm.insert(downsprite,0);
downsprite.setVisible(false);
upimage = null;
downimage = null;
lm = null;
rt.gc();
System.out.println("chatin: "+String.valueOf(rt.freeMemory()));
// TODO 自动生成构造函数存根
}
public void init(String chatcontent,int maxrow,int mostnum,int recordnum){
this.mostnum = mostnum;
this.maxrow = maxrow;
this.recordnum = 2*recordnum;
currflag =false;
chatwordflag = new boolean[this.recordnum+1];
for(int i=0;i<this.recordnum+1;i++){
chatwordflag[i] = false;
}
contentwidth = rectwidth-2*distance;
contentheight = font.getHeight()*this.recordnum;
Image contentimage = Image.createImage(contentwidth,contentheight);
Graphics gra = contentimage.getGraphics();
gra.setFont(font);
currdraw = 0;
contentdraw(gra,chatcontent);
currflag =false;
Image tempimage= null;
//tempimage = setImageAlph(contentimage);
contentlayer = new TiledLayer(1,maxrow,contentimage,contentimage.getWidth(),font.getHeight());
lm.insert(contentlayer,2);
tempimage = null;
contentimage = null;
DisplayDeal();
rt.gc();
System.out.println("chatinit: "+String.valueOf(rt.freeMemory()));
}
private void updatePosition(){
contentrect.setPosition(orignX,orignY);
upsprite.setPosition(orignX+rectwidth-upwidth,orignY);
downsprite.setPosition(orignX+rectwidth-upwidth,orignY+rectheight-downheight);
contentlayer.setPosition(orignX+1,orignY+1);
setVisible(true);
contentrect = null;
upsprite = null;
downsprite = null;
rt.gc();
System.out.println("chatupdate: "+String.valueOf(rt.freeMemory()));
}
public void setPosition(int x,int y){
this.orignX = x;
this.orignY = y;
updatePosition();
}
private void setVisible(boolean flag){
contentrect.setVisible(flag);
upsprite.setVisible(flag);
downsprite.setVisible(flag);
contentlayer.setVisible(flag);
}
public void setOperate(int direction){ //0: 表示向下翻 1:向上翻
if(direction == 0){
if(firi+maxrow<=recordnum)
firi++;
}else if(direction == 1){
if(chatwordflag[firi-1]){
firi--;
}
}
DisplayDeal();
}
public void append(String [] content,int appendi){
int i = appendi;
String drawstr;
Image contentimage = Image.createImage(contentwidth,contentheight);
Graphics gra = contentimage.getGraphics();
gra.setFont(font);
currdraw = 0;
for(int j=0;j<this.recordnum+1;j++){ //清空聊天内容的所有标志
chatwordflag[j] = false;
}
while(true){
drawstr = content[i];
if(drawstr.equals(""))
break;
contentdraw(gra,drawstr);
if((i == appendi-1)||(appendi == 0 && i == recordnum/2 - 1)){
break;
}
if(i == 0){
i = recordnum/2 - 1;
}else{
i--;
}
}
currflag =false;
Image tempimage = null;
// tempimage = setImageAlph(contentimage); //将图片变透明
//contentlayer.setStaticTileSet(tempimage,contentwidth,font.getHeight());
contentlayer.setStaticTileSet(contentimage,contentwidth,font.getHeight());
contentimage = null;
tempimage = null;
gra = null;
rt.gc();
System.out.println("chatappend: "+String.valueOf(rt.freeMemory()));
DisplayDeal();
}
private void DisplayDeal(){
for(int i = 0; i < maxrow; i++){
contentlayer.setCell(0,i,firi+i);
}
}
private void contentdraw(Graphics gra,String drawstr){
int rowcount = (drawstr.length()+mostnum-1)/mostnum;
currdraw = currdraw + rowcount;
int currtemp = recordnum - currdraw;
if(currtemp>=0){
if(!currflag){
int curri = recordnum - rowcount+1; //由于单元格图片编号从1开始,所以加1
if(maxrow <= rowcount){
firi = curri;
}
else{
firi =curri-( maxrow - rowcount);
}
currflag = true;
}
String tempstr1=drawstr;
String tempstr2;
int j =0;
while(tempstr1.length()>mostnum){
tempstr2 = tempstr1.substring(0,mostnum);
tempstr1 = tempstr1.substring(mostnum);
gra.drawString(tempstr2,0,(currtemp+j)*font.getHeight(),Graphics.TOP|Graphics.LEFT);
chatwordflag[currtemp+j+1] = true;
j++;
}
if(tempstr1.length()>0){
gra.drawString(tempstr1,0,(currtemp+j)*font.getHeight(),Graphics.TOP|Graphics.LEFT);
chatwordflag[currtemp+j+1] = true;
}
}
System.out.println("chatdraw: "+String.valueOf(rt.freeMemory()));
}
// 将图片变透明
private Image setImageAlph(Image orignImage){
Image tempimage = null;
int tempwidth= 0;
int tempheight = 0;
tempwidth = orignImage.getWidth();
tempheight = orignImage.getHeight();
int temprect[]= new int[tempwidth*tempheight];
orignImage.getRGB(temprect,0,tempwidth,0,0,tempwidth,tempheight);
orignImage = null;
for(int i = 0;i<tempwidth*tempheight;i++){
if(temprect[i]==0xffffffff) //将白底的图片变透明
temprect[i]=temprect[i]&0x00ffffff;
}
tempimage = Image.createRGBImage(temprect,tempwidth,tempheight,true);
temprect = null;
rt.gc();
return tempimage;
}
public void clear(){
upsprite = null;
downsprite = null; //聊天内容上下滚动控制键 图层
contentrect = null; //整个控件的图层框
}
public void setNull(){
upsprite = null;
downsprite = null; //聊天内容上下滚动控制键 图层
contentlayer = null; //显示聊天内容 图层
contentrect = null; //整个控件的图层框
lm = null; //图层管理器
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -