advancedroundeddrawer.java

来自「moblie syncml mail javame」· Java 代码 · 共 274 行

JAVA
274
字号
/*
 * Funambol is a mobile platform developed by Funambol, Inc.
 * Copyright (C) 2003 - 2007 Funambol, Inc.
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License version 3 as published by
 * the Free Software Foundation with the addition of the following permission
 * added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED
 * WORK IN WHICH THE COPYRIGHT IS OWNED BY FUNAMBOL, FUNAMBOL DISCLAIMS THE
 * WARRANTY OF NON INFRINGEMENT  OF THIRD PARTY RIGHTS.
 *
 * 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 Affero General Public License
 * along with this program; if not, see http://www.gnu.org/licenses or write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301 USA.
 *
 * You can contact Funambol, Inc. headquarters at 643 Bair Island Road, Suite
 * 305, Redwood City, CA 94063, USA, or at email address info@funambol.com.
 *
 * The interactive user interfaces in modified source and object code versions
 * of this program must display Appropriate Legal Notices, as required under
 * Section 5 of the GNU Affero General Public License version 3.
 *
 * In accordance with Section 7(b) of the GNU Affero General Public License
 * version 3, these Appropriate Legal Notices must retain the display of the
 * "Powered by Funambol" logo. If the display of the logo is not reasonably
 * feasible for technical reasons, the Appropriate Legal Notices must display
 * the words "Powered by Funambol".
 *
 *
 */


package com.funambol.mailclient.ui.view;

import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;

/**
 * a drawer with rounded corners
 */
public class AdvancedRoundedDrawer implements Drawer {

    private  GraphicalTheme graphicalTheme;

    private static final int ARC_WIDTH=1;
    private static final int ARC_HEIGHT=1;
    //private static final String CONTINUE_STRING="..";

    private Font font;
    private int[] colors;
    private int titleBarHeight;

    private int BKG_COLOR_START = 0x00dcee;

    private int BKG_COLOR_END = 0x004a7e;

    private int[] gradientBackground;

    private int gradientLastH = -1;

    private int highlightedMiddleColor = -1;

    /** Creates a new instance of Drawer */
    public AdvancedRoundedDrawer() {
        this.graphicalTheme=GraphicalThemeFactory.getGraphicalTheme();
    }
  
    private static int LEFT_EDGE_MARGIN   = 0;
    private static int RIGHT_EDGE_MARGIN  = 0;
    private static int TOP_EDGE_MARGIN    = 1;
    private static int BOTTOM_EDGE_MARGIN = 2;

    public void drawBackground(Graphics g, int w, int h, boolean selected) {

        drawBackground(g, 0, 0, w, h, selected);
    }

    public void drawBackground(Graphics g, int x, int y, int w, int h,
                               boolean selected) {

        if (selected) {
            
            // find the radius of the corner            
            int radius = ARC_HEIGHT/2;
            
            
           
            // bg color
            g.setColor(graphicalTheme.getHighlightedBackgroundColor());
            g.fillRoundRect(1,1,w-2,h-2,ARC_WIDTH,ARC_HEIGHT);
            //g.fillRect(1,1,w-2,h-2);
            // the middle color
            g.setColor(getHighlightedMiddleColor());
            g.fillRoundRect(1,h/2,w-2,h/2,ARC_WIDTH,ARC_HEIGHT);
            // g.fillRect(1,h/2,w-2,h/2);


            // fill the gradient and paint it
            getGradientBackground(h-2*radius);
            for (int i = 0; i<gradientBackground.length; i++) {
                g.setColor(gradientBackground[i]);
                g.drawLine(1,i+radius,w-2,i+radius);
            }
            
            // this fills the central line, 
            // that can be skipped in the previous loop due to some
            // "obscure" math rule (something about a division by 2)
            if (gradientBackground.length * 2 < h) {
                int i = gradientBackground.length;
                g.drawLine(1,i+radius,w-2,i+radius);
            }

            
            /*g.setColor(graphicalTheme.getHighlightedBorderColor());
            g.drawRoundRect(x + LEFT_EDGE_MARGIN-1,      y + TOP_EDGE_MARGIN-1,
                            w - RIGHT_EDGE_MARGIN - 1, h - BOTTOM_EDGE_MARGIN - 1,
                            ARC_WIDTH,ARC_HEIGHT);*/

        } else {
            g.setColor(graphicalTheme.getBackgroundColor());
            g.fillRoundRect(x + LEFT_EDGE_MARGIN,  y + TOP_EDGE_MARGIN,
                            w - RIGHT_EDGE_MARGIN, h - TOP_EDGE_MARGIN,
                            ARC_WIDTH,ARC_HEIGHT);
        
          /*  g.setColor(getHighlightedMiddleColor());
            g.drawLine(x,y+h, w, y+h);
           */
        }
    }

    public GraphicalTheme getGraphicalTheme(){
        return graphicalTheme;
    }

    public void drawString(String string, Graphics graphics, int x, int y, boolean selected) {
        drawString(string, graphics, x, y, Graphics.TOP|Graphics.LEFT,selected);
    }
    public void drawBoldString(String string, Graphics graphics, int x, int y, boolean selected) {
        Font oldfont = graphics.getFont();
        graphics.setFont(getGraphicalTheme().getBoldFont());
        //drawString(string, graphics, x, y, Graphics.TOP|Graphics.LEFT,selected);
        if (selected) {
            graphics.setColor(graphicalTheme.getHighlightedForegroundColor());
        } else {
            graphics.setColor(graphicalTheme.getForegroundColor());
        }
        graphics.drawString(string, x,y,Graphics.TOP|Graphics.LEFT);
        graphics.setFont(oldfont);
    }


    public void drawString(String string, Graphics graphics, int x, int y, int layout, boolean selected) {
        if (selected) {
            graphics.setColor(graphicalTheme.getHighlightedForegroundColor());
        } else {
            graphics.setColor(graphicalTheme.getForegroundColor());
        }
        graphics.setFont(graphicalTheme.getDefaultFont());
        graphics.drawString(string, x,y,layout);
    }

    public void drawCanvasBackground(Graphics g, int w, int h) {
        g.setColor(graphicalTheme.getBackgroundColor());
        g.fillRect(0,0,w,h);
    }

    public void drawTitleBackground(Graphics g, int w, int h) {
        /*
        g.setColor(graphicalTheme.getTitleBackgroundColor());
        g.fillRect(0,0,w,h);
        g.setColor(graphicalTheme.getTitleBorderColor());
        g.drawLine(0,h,w,h);
         **/

        if (this.titleBarHeight != h) {
            this.titleBarHeight = h;
            colors = getGradient(BKG_COLOR_START, BKG_COLOR_END, h);
        }
        for (int i = 0; i<colors.length; i++) {
            g.setColor(colors[i]);
            g.drawLine(0,i,w,i);
        }
        g.setColor(graphicalTheme.getTitleBorderColor());
        g.drawLine(0,h,w,h);

    }

    public 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;
    }

    private int[] getGradientBackground(int h) {
        if (gradientBackground == null || h != gradientLastH) {
            gradientLastH = h;
            gradientBackground = getGradient(
                    graphicalTheme.getHighlightedBackgroundColor(), getHighlightedMiddleColor(), h);
        }
        return gradientBackground;
    }

    private 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;
    }

    private int getHighlightedMiddleColor() {
       if (highlightedMiddleColor == -1) {
        
        highlightedMiddleColor = getMiddleColor(graphicalTheme.getHighlightedBackgroundColor(),
                    graphicalTheme.getHighlightedForegroundColor());
       }
       return highlightedMiddleColor;
       
    }

}

⌨️ 快捷键说明

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