📄 dropshadowtexteffect.java
字号:
//#condition polish.usePolishGui && polish.midp2 && polish.cldc1.1/* * Created on 10.07.2006 at 12:22:13. * * Copyright (c) 2005 Robert Virkus / Enough Software * * This file is part of J2ME Polish. * * J2ME Polish 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. * * J2ME Polish 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 J2ME Polish; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * Commercial licenses are also available, please * refer to the accompanying LICENSE.txt or visit * http://www.j2mepolish.org for details. */package de.enough.polish.ui.texteffects;import javax.microedition.lcdui.Font;import javax.microedition.lcdui.Graphics;import javax.microedition.lcdui.Image;import de.enough.polish.ui.Color;import de.enough.polish.ui.Style;import de.enough.polish.ui.TextEffect;import de.enough.polish.util.DrawUtil;/** * <p>Paints a dropshadow behind a text, whereas you are able to specify * the shadows inner and outer color.</p> * <p>Activate the shadow text effect by specifying <code>text-effect: drop-shadow;</code> in your polish.css file. * You can finetune the effect with following attributes: * </p> * <ul> * <li><b>text-drop-shadow-inner-color</b>: the inner color of the shadow, which should be less opaque than the text. </li> * <li><b>text-drop-shadow-outer-color</b>: the outer color of the shadow, which should be less than opaque the inner color. </li> * <li><b>text-drop-shadow-offsetx:</b>: use this for finetuning the shadow's horizontal position. Negative values move the shadow to the left.</li> * <li><b>text-drop-shadow-offsety:</b>: use this for finetuning the shadow's vertical position. Negative values move the shadow to the top.</li> * <li><b>text-drop-shadow-size:</b>: use this for finetuning the shadows radius.</li> * </ul> * <p>Choosing the same inner and outer color and varying the transparency is recommended. Dropshadow just works, if the Text is opaque.</p> * <p>Copyright Enough Software 2006</p> * <pre> * history * 11-Jul-2006 * </pre> * @author Simon Schmitt * */public class DropShadowTextEffect extends TextEffect { private final static int CLEAR_COLOR = 0xFF000123; private int clearColor; private String lastText; private int lastTextColor; int[] localRgbBuffer; private int innerColor = 0xA0909090; private int outerColor = 0x20909090; private int size=6; private int xOffset=1, yOffset=2; /* (non-Javadoc) * @see de.enough.polish.ui.TextEffect#drawString(java.lang.String, int, int, int, int, javax.microedition.lcdui.Graphics) */ public void drawString(String text, int textColor, int x, int y, int orientation, Graphics g) { // calculate imagesize Font font = g.getFont(); int fHeight = font.getHeight(); int fWidth = font.stringWidth( text ); int newWidth=fWidth + this.size*2; int newHeight=fHeight+ this.size*2; int startX = getLeftX( x, orientation, fWidth ); int startY = getTopY( y, orientation, fHeight, font.getBaselinePosition() ); // additional Margin for the image because of the shadow int iLeft = this.size-this.xOffset<0 ? 0 : this.size-this.xOffset; int iTop = this.size-this.yOffset<0 ? 0 : this.size-this.yOffset; // offset of an invisble area caused by negative (x,y) int invX=Math.max(0, -(startX-iLeft)); int invY=Math.max(0, -(startY-iTop)); // check whether the string has to be rerendered if (this.lastText!=text || this.lastTextColor != textColor) { this.lastText=text; this.lastTextColor=textColor; // create Image, Graphics, ARGB-buffer Graphics bufferG; Image midp2ImageBuffer = Image.createImage( newWidth, newHeight); // iLeft+iRight=2*size?? bufferG = midp2ImageBuffer.getGraphics(); this.localRgbBuffer = new int[ (newWidth) * (newHeight) ]; // draw pseudo transparent Background bufferG.setColor( CLEAR_COLOR ); bufferG.fillRect(0,0,newWidth, newHeight); // draw String on Graphics bufferG.setFont(font); bufferG.setColor( textColor ); bufferG.drawString(text,iLeft,iTop, Graphics.LEFT | Graphics.TOP); // get RGB-Data from Image midp2ImageBuffer.getRGB(this.localRgbBuffer,0,newWidth, 0, 0, newWidth, newHeight); // check clearColor int[] clearColorArray = new int[1]; midp2ImageBuffer.getRGB(clearColorArray, 0, 1, 0, 0, 1, 1 ); this.clearColor = clearColorArray[0]; // transform RGB-Data for (int i=0; i<this.localRgbBuffer.length;i++){ // perform Transparency if (this.localRgbBuffer[i] == this.clearColor){ this.localRgbBuffer[i] = 0x00000000; } } DrawUtil.dropShadow(this.localRgbBuffer,newWidth,newHeight,this.xOffset, this.yOffset, this.size,this.innerColor, this.outerColor); } // draw RGB-Data if (newHeight-invY<=0 || newWidth-invX<=0){ // bugfix: exit if there is no part of text visible return; } g.drawRGB(this.localRgbBuffer,invY*(newWidth)+invX,newWidth, ( startX-iLeft+invX<=0 ? 0 :startX-iLeft+invX), ( startY-iTop+invY<=0 ? 0 :startY-iTop+invY) , newWidth-invX, newHeight-invY, true); } /* (non-Javadoc) * @see de.enough.polish.ui.TextEffect#setStyle(de.enough.polish.ui.Style) */ public void setStyle(Style style) { super.setStyle(style); //#if polish.css.text-drop-shadow-inner-color Color sShadowColorObj = style.getColorProperty( "text-drop-shadow-inner-color" ); if (sShadowColorObj != null) { this.innerColor = sShadowColorObj.getColor(); } //#endif //#if polish.css.text-drop-shadow-outer-color Color eShadowColorObj = style.getColorProperty( "text-drop-shadow-outer-color" ); if (eShadowColorObj != null) { this.outerColor = eShadowColorObj.getColor(); } //#endif //#if polish.css.text-drop-shadow-size Integer sizeInt = style.getIntProperty( "text-drop-shadow-size" ); if (sizeInt != null) { this.size = sizeInt.intValue(); } //#endif //#if polish.css.text-drop-shadow-offsetx Integer oXInt = style.getIntProperty( "text-drop-shadow-offsetx" ); if (oXInt != null) { this.xOffset = oXInt.intValue(); } //#endif //#if polish.css.text-drop-shadow-offsety Integer oYInt = style.getIntProperty( "text-drop-shadow-offsety" ); if (oYInt != null) { this.yOffset = oYInt.intValue(); } //#endif } /* (non-Javadoc) * @see de.enough.polish.ui.TextEffect#releaseResources() */ public void releaseResources() { super.releaseResources(); this.lastText = null; this.localRgbBuffer = null; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -