<pre name="code" class="java"> /**
* 思路:使用screen[byte_pos]=0xff,一次设定一整个字节。起点和终点剩余部分的位,使用掩码设定。
* @param screen
* @param width
* @param x1
* @param x2
* @param y
*/
public static void drawHorizontalLine(byte[] screen,int width,int x1,int x2,int y){
int startOffset=x1%8;
int firstFullByte=x1/8;
if(startOffset!=0)
firstFullByte++;
int endOffset=x2%8;
int lastFullByte=x2/8;
if(endOffset!=7)
lastFullByte--;
//设定完整的字节
for(int i=firstFullByte;i<=lastFullByte;i++){
screen[(width/8)*y+i]=(byte)0xFF;
}
//创建用于线条起点和终点的掩码
byte startMask=(byte) (1>>startOffset);
byte endMask=(byte) ~(1>>(endOffset+1));
//设定线条的起点和终点
if((x1/8)==(x2/8)){
byte mask=(byte) (startMask&endMask);
screen[(width/8)*y+(x1/8)]|=mask;
}else{
if(startOffset!=0){
int byteNumber=(width/8)*y+firstFullByte-1;
screen[byteNumber]|=startMask;
}
if(endOffset!=0){
int byteNumber=(width/8)*y+lastFullByte+1;
screen[byteNumber]|=endMask;
}
}
}版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/shangqing1123/article/details/47337359