⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uiutils.java

📁 moblie syncml mail javame
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        String [] ret;
        
        ChunkedString csLine = new ChunkedString(text);
        ChunkedString csWord;
        String[] lineSep = {"\r\n", "\n\r", "\r", "\n" };
        String[] wordSep = {" ", "\t"};
        
        
        //ChunkedString line;
        StringBuffer wordStringBuffer = new StringBuffer();
        String word;
        StringBuffer myLine=new StringBuffer();
        int widthLeft;
        while ( (csWord=csLine.getNextChunk(lineSep)) != null ) {
            //csWord = new ChunkedString(line);
            widthLeft = screenWidth;
            myLine.setLength(0);
            
            // get the words
            while ( (word = csWord.getNextString(wordSep)) != null ) {
                wordStringBuffer = new StringBuffer(word);
                
                if ( myLine.length() != 0 ) {
                    //if this is not the first word of the line we add a space
                    wordStringBuffer.insert(0," ");
                }
                int width = font.stringWidth(wordStringBuffer.toString());
                //if it fits inside the line
                if (width < widthLeft) {
                    //we append the word at the line and update widthleft
                    myLine.append(wordStringBuffer.toString());
                    widthLeft -=width;
                } else {
                    //if we've a space we can cut it out since this word
                    // will go to the next line
                    if (wordStringBuffer.charAt(0) == ' ') {
                        wordStringBuffer.deleteCharAt(0);
                    }
                    width = font.stringWidth(wordStringBuffer.toString());
                    //if can go on a new line
                    if ( width < screenWidth ) {
                        //we put it on a new line
                        lines.addElement(myLine.toString());
                        myLine.setLength(0);
                        myLine.append(wordStringBuffer.toString());
                        widthLeft = screenWidth - width;
                        continue;
                    } else {
                        int start = 0;
                        int end = 0;
                        int length = wordStringBuffer.length();
                        //String aString = myLine.toString();
                        while (start < length) {
                            // check length of the substring, adding a single char each time
                            for (int w = 0; w < widthLeft && end < length; end++) {
                                w = font.substringWidth( wordStringBuffer.toString(), start, (end - start) +1 );
                            }
                            myLine.append(wordStringBuffer.toString().substring(start,end));
                            if (end < length) {
                                lines.addElement(myLine.toString());
                                myLine.setLength(0);
                                widthLeft = screenWidth;
                            } else {
                                widthLeft = screenWidth - font.stringWidth(myLine.toString());
                            }
                            start = end;
                        }
                    }
                }
            }
            lines.addElement(myLine.toString());
        }
        ret = new String[lines.size()];
        //        Log.debug(this, "created vector, copying it");
        lines.copyInto(ret);
        lines=null;
        //       Log.debug(this, "returning string array");
        return ret;
    }
    
    
    private static Image resizeImage2(Image image, int width, int height) {
        int sourceWidth = image.getWidth();
        int sourceHeight = image.getHeight();
        
        Image thumb = Image.createImage(width, height);
        Graphics g = thumb.getGraphics();
        
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                g.setClip(x, y, 1, 1);
                int dx = x * sourceWidth / width;
                int dy = y * sourceHeight / height;
                g.drawImage(image, x - dx, y - dy, Graphics.LEFT | Graphics.TOP);
            }
        }
        
        Image immutableThumb = Image.createImage(thumb);
        
        return immutableThumb;
    }
    
    
    private static int[] rescaleArray(int[] ini, int x, int y, int x2, int y2) {
        int out[] = new int[x2*y2];
        for (int yy = 0; yy < y2; yy++) {
            int dy = yy * y / y2;
            for (int xx = 0; xx < x2; xx++) {
                int dx = xx * x / x2;
                out[(x2*yy)+xx]=ini[(x*dy)+dx];
            }
        }
        return out;
    }
    
    
    public static Image resizeImage(Image image, int width, int height) {
        /*int width = getWidth();
        int height = width * image.getHeight() / image.getWidth();
         */
        
        //Need an array (for RGB, with the size of original image)
        int rgb[] = new int[image.getWidth()*image.getHeight()];
        //Get the RGB array of image into "rgb"
        image.getRGB(rgb,0,image.getWidth(),0,0,image.getWidth(),image.getHeight());
        //Call to our function and obtain RGB2
        int rgb2[] = rescaleArray(rgb,image.getWidth(),image.getHeight(),width,height);
        //Create an image with that RGB array
        Image temp2 = Image.createRGBImage(rgb2,width,height,true);
        
        return temp2;
    }
    
    public static Image bestFit(Image image, int maxWidth, int maxHeight) {
        
        // getting image properties
        int w = image.getWidth();
        int h = image.getHeight();
        
        //  get the ratio
        int ratiow = 100 * maxWidth / w;
        int ratioh = 100 * maxHeight / h;
        
        // this is to find the best ratio to
        // resize the image without deformations
        int ratio = Math.min(ratiow, ratioh);
        
        // computing final desired dimensions
        int desiredWidth = w * ratio/100;
        int desiredHeight = h * ratio/100;
        
        //resizing
        return  resizeImage(image, desiredWidth, desiredHeight);
    }
    
    
    
    /**
     * get the middle color given two colors
     * 
     * @param aStartColor the starting color
     * @param aEndColor the ending color
     * @return the middle color (sort of, no mapping in hsb is performed, so this
     *  is not really the middle color...)
     */
     public static int getMiddleColor(int aStartColor, int aEndColor) {

        int r1 = aStartColor>>16;
        int g1 = aStartColor>>8;
        g1 = g1 & 0x0000FF;
        int b1 = aStartColor;
        b1 = b1 & 0x0000FF;

        int r2 = aEndColor>>16;
        int g2 = aEndColor>>8;
        g2 = g2 & 0x0000FF;
        int b2 = aEndColor;
        b2 = b2 & 0x0000FF;

        int r = r1 - (r1 - r2) /2 ;
        r = r << 16;
        int g = g1 - (g1 - g2) /2 ;
        g = g << 8;
        int b = b1 - (b1 - b2) /2 ;

        return r | g | b;
    }
     
     public static int[] getGradient(int aStartColor, int aEndColor, int aLength){
        int r1 = aStartColor>>16;
        int g1 = aStartColor>>8;
        g1 = g1 & 0x0000FF;
        int b1 = aStartColor;
        b1 = b1 & 0x0000FF;
        
        int r2 = aEndColor>>16;
        int g2 = aEndColor>>8;
        g2 = g2 & 0x0000FF;
        int b2 = aEndColor;
        b2 = b2 & 0x0000FF;
        
        int res_r = 0;
        int res_g = 0;
        int res_b = 0;
        
        int gradient[] = new int[aLength];
        
        int counter = 0;
        int dif;
        while(counter < aLength){
            dif = (aLength-counter);
            res_r = (r1 * dif + r2*counter)/aLength ;
            res_g = (g1 * dif + g2*counter)/aLength ;
            res_b = (b1 * dif + b2*counter)/aLength ;
            
            res_r = res_r <<16;
            res_g = res_g <<8;
            
            gradient[counter] = (res_r | res_g | res_b);
            
            counter++;
        }
        return gradient;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -