<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>

<channel>
	<title>专注于blackberry &#187; 开发</title>
	<atom:link href="http://www.inblackberry.com/web/tag/development/feed" rel="self" type="application/rss+xml" />
	<link>http://www.inblackberry.com/web</link>
	<description>blackberry</description>
	<pubDate>Thu, 26 Aug 2010 14:19:35 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.1</generator>
	<language>en</language>
			<item>
		<title>在Blackberry编写邮件的窗体中加入控件:编写自定义控件</title>
		<link>http://www.inblackberry.com/web/development/implements-custome-image-previiew-field.html</link>
		<comments>http://www.inblackberry.com/web/development/implements-custome-image-previiew-field.html#comments</comments>
		<pubDate>Thu, 04 Dec 2008 18:02:53 +0000</pubDate>
		<dc:creator>Taigoo</dc:creator>
		
		<category><![CDATA[开发]]></category>

		<category><![CDATA[blackberry]]></category>

		<guid isPermaLink="false">http://www.inblackberry.com/web/development/implements-custome-image-previiew-field.html</guid>
		<description><![CDATA[<p>在上一篇文章&#60;&#60;<a href="http://www.inblackberry.com/web/development/insert-fields-in-blackberry-native-email-system.html">在Blackberry编写邮件的窗体中加入控件</a>&#62;&#62;介绍了如何在Blackberry自带的mail编辑器里加入控件。其中提到了图片预览的控件，我自己花了点时间写了一个.这样BerryMail的附件预览功能基本上大功告成，难吗？不难。</p>
<p>我写的效果如下：</p>
<p><img src="http://farm4.static.flickr.com/3257/3082898046_6a6c282ee0.jpg"> </p>
<p>上面只是简单的介绍了一下，目前该代码还不完善，主要表现在：</p>

<li>图片的缩放问题
<li>图片属性的完善，例如分辨率等
<li>一些样式的调整</li>

<p>尽管这样，我的目的也只想起到一个抛砖引玉的目的，如果哪位感兴趣，那就完善一下吧。</p>
<p>上部分代码：</p>
<p>&#160;</p>
<p>
package com.taigoo.eie.gui.component;

/**
 * it can make image]]></description>
			<content:encoded><![CDATA[<p>在上一篇文章&lt;&lt;<a href="http://www.inblackberry.com/web/development/insert-fields-in-blackberry-native-email-system.html">在Blackberry编写邮件的窗体中加入控件</a>&gt;&gt;介绍了如何在Blackberry自带的mail编辑器里加入控件。其中提到了图片预览的控件，我自己花了点时间写了一个.这样BerryMail的附件预览功能基本上大功告成，难吗？不难。</p>
<p>我写的效果如下：</p>
<p><img src="http://farm4.static.flickr.com/3257/3082898046_6a6c282ee0.jpg"> </p>
<p>上面只是简单的介绍了一下，目前该代码还不完善，主要表现在：</p>
<ol>
<li>图片的缩放问题
<li>图片属性的完善，例如分辨率等
<li>一些样式的调整</li>
</ol>
<p>尽管这样，我的目的也只想起到一个抛砖引玉的目的，如果哪位感兴趣，那就完善一下吧。</p>
<p>上部分代码：</p>
<p>&nbsp;</p>
<p>
<pre>package com.taigoo.eie.gui.component;

/**
 * it can make image preview, and shows the attributes of the image.
 * such as name, size,image type.
 * @author taigoo zhang
 *
 */
public class ImagePreviewField extends Field {

	public final static int IMAGE_ATTRIB_NAME = 0 ;		//image name attribute
	public final static int IMAGE_ATTRIB_PATH = 1 ;	 	//image path
	public final static int IMAGE_ATTRIB_SIZE = 2 ;
	public final static int IMAGE_ATTRIB_RESOLUTION = 3 ;
	public final static int IMAGE_ATTRIB_TYPE = 4 ;

	//The field's width
	private int width ;

	//the field's height
	private int height ;

	//the image height to render
	private int imageHeight = 80 ;

	//the image width to render
	private int imageWidth = 80;

	//image path
	private String imagePath;

	//font
	private Font font;

	private IntHashtable attributes = new IntHashtable();

	private EncodedImage encodeImage ;

	//the space between 2 lines in Y direction
	private int heightOffset = 2 ;

	//the space between 2 lines in X direction
	private int widthOffset = 2 ;

	//the vertical line top/bottom/left/right margin.
	private int lineOffsetTop = 10 ;
	private int lineOffsetBottom = 10 ;
	private int lineOffsetLeft = 5;
	private int lineOffsetRight = 5 ;

	//arch height and width
	private int arcHeight = 15 ;
	private int arcWidth = 15 ;

	//the margin between the fields in it.
	private int marginTop = 10 ;
	private int marginBottom = 10 ;
	private int marginLeft = 10 ;
	private int marginRight = 10;

	/**
	 * Constructor
	 * @param path image path
	 */
	public ImagePreviewField(String path) {
		font = this.getFont();

		imagePath = path;

		this.width = this.getPreferredWidth();
		this.height = this.getPreferredHeight();

		//read data
		readImage();
	}

	/**
	 * @see net.rim.device.api.ui.Field#paint(Graphics)
	 */
	protected void paint(Graphics g) {
		//firstly, draw a round rectangle.
		g.drawRoundRect(0, 0, width, height, arcWidth, arcHeight);

		//draw image
		int imageX = this.marginLeft ;
		int imageY = this.marginTop ;

		//so complex here
		float ratio = this.getScaleRatio() ; //get the ratio in format of Fiexed32 type.
		int divisor = Fixed32.toFP(100) ;
		int multiplier = Fixed32.toFP(400 - new Float(ratio*100).intValue()) ;

		int scaleWidth = Fixed32.toFP(1);
		scaleWidth = Fixed32.div(scaleWidth,divisor);/** First, divide the image scale by 100. */
		scaleWidth = Fixed32.mul(scaleWidth,multiplier); /** Now, multiply the image scale by the multiplier. */

		int scaleHeight = Fixed32.toFP(1);
		scaleHeight = Fixed32.div(scaleHeight,divisor);/** First, divide the image scale by 100. */
		scaleHeight = Fixed32.mul(scaleHeight,multiplier); /** Now, multiply the image scale by the multiplier. */

		int drawImageWidth = new Float(this.encodeImage.getWidth() * ratio).intValue();
		int drawImageHeight = new Float(this.encodeImage.getHeight() * ratio).intValue();
		g.drawImage(imageX, imageY, drawImageWidth, drawImageHeight
				, this.encodeImage.scaleImage32(scaleWidth ,scaleHeight), 0, 0, 0);

		//draw vertical line
		int lineX = imageX + imageWidth + lineOffsetLeft ;
		int lineY = imageY ;

		g.drawLine(lineX, lineY, lineX, lineY + imageHeight);

		//draw text
		//name
		int textX1 = lineX + lineOffsetRight ;
		int textY1 = lineY ;
		g.drawText(this.attributes.get(ImagePreviewField.IMAGE_ATTRIB_NAME).toString(), textX1, textY1 ,
				(int)( getStyle() &amp; DrawStyle.ELLIPSIS | DrawStyle.HALIGN_MASK ), this.getPreferredWidth() - textX1);

		//size
		int textX2 = textX1;
		int textY2 = textY1 + font.getHeight() + this.heightOffset ;
		long fileSize = Long.parseLong(this.attributes.get(ImagePreviewField.IMAGE_ATTRIB_SIZE).toString());
		g.drawText("Size: " +new Long(fileSize/1024).toString()+"K" , textX2, textY2 ,
				(int)( getStyle() &amp; DrawStyle.ELLIPSIS | DrawStyle.HALIGN_MASK ), this.getPreferredWidth() - textX2);

		//type
		int textX3 = textX2 ;
		int textY3 = textY2 + font.getHeight() + this.heightOffset ;
		g.drawText("Type: " + convertImageTypeToString(Integer.parseInt(attributes.get(ImagePreviewField.IMAGE_ATTRIB_TYPE).toString()))
				, textX3, textY3 , (int)( getStyle() &amp; DrawStyle.ELLIPSIS | DrawStyle.HALIGN_MASK )
				, this.getPreferredWidth() - textX3);

	}
		/**
	 * Read the image to byte stream.
	 * @return
	 */
	private byte[] readImage(){
		byte[] data ;
		try {
			FileConnection fileConnection = (FileConnection)Connector.open(imagePath);
            if (fileConnection.exists()) {
                InputStream input = fileConnection.openInputStream();

                int size = input.available();
                data = new byte[size];
                input.read(data, 0, size);

                //read image attributes
                this.readAttributes(fileConnection) ;

                encodeImage = EncodedImage.createEncodedImage(data,0,data.length) ;
                this.attributes.put(ImagePreviewField.IMAGE_ATTRIB_TYPE,new Integer(encodeImage.getImageType())) ;

            } else {
            	throw new Exception("File is not found.");
            }
            fileConnection.close();
        } catch(Exception ioe) {
        	throw new IllegalArgumentException(ioe.getMessage());
        }
        return data;
	}

	/**
	 * Gets the image type.
	 * @return
	 */
	private void readAttributes(FileConnection fc) {
		if(fc != null) {
			this.attributes.put(ImagePreviewField.IMAGE_ATTRIB_PATH, fc.getPath());

			//image name
			String name = fc.getName();
			this.attributes.put(ImagePreviewField.IMAGE_ATTRIB_NAME, name);

			try{

				this.attributes.put(ImagePreviewField.IMAGE_ATTRIB_SIZE, new Long(fc.fileSize()));

			} catch(IOException ioe){
				this.attributes.put(ImagePreviewField.IMAGE_ATTRIB_SIZE, new Long(0));
			}

		}
	}

	/**
	 * Converts the image type from int to string.
	 * @param type
	 * @return
	 */
	private String convertImageTypeToString(int type) {
		String typeString = "IMAGE/UNKNOWN" ;
		switch(type){
		case EncodedImage.IMAGE_TYPE_BMP :
			typeString = "IMAGE/BMP" ;
			break;

		case EncodedImage.IMAGE_TYPE_GIF :
			typeString = "IMAGE/GIF";
			break;
			........
		default:
			typeString = "IMAGE/UNKNOWN" ;
		}

		return typeString;
	}

	private float getScaleRatio() {
		int w = this.encodeImage.getWidth() ;
		int h = this.encodeImage.getHeight() ;

		float ratioWidth = 1.0f *imageWidth/w ;
		float ratioHeight = 1.0f * imageHeight/ h;
	        return (ratioWidth &lt; ratioHeight) ? ratioWidth : ratioHeight ;

	}
}
</pre>
<p>上面只显示了一些关键代码，提供以下思路而已啦。</p>
<p>若索要全代码，和我联系即可。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.inblackberry.com/web/development/implements-custome-image-previiew-field.html/feed</wfw:commentRss>
		</item>
		<item>
		<title>在Google Group上建立一个开发组</title>
		<link>http://www.inblackberry.com/web/news/create-a-blackberry-dev-group.html</link>
		<comments>http://www.inblackberry.com/web/news/create-a-blackberry-dev-group.html#comments</comments>
		<pubDate>Wed, 12 Nov 2008 02:08:51 +0000</pubDate>
		<dc:creator>Taigoo</dc:creator>
		
		<category><![CDATA[新闻]]></category>

		<category><![CDATA[blackberry]]></category>

		<category><![CDATA[开发]]></category>

		<guid isPermaLink="false">http://www.inblackberry.com/web/news/create-a-blackberry-dev-group.html</guid>
		<description><![CDATA[<p>如题，地址为：</p>
<p><a title="http://groups.google.com/group/blackberry-dev" href="http://groups.google.com/group/blackberry-dev">http://groups.google.com/group/blackberry-dev</a></p>
<p>这个组可以讨论任何关于Blackberry的事情。</p>
<p>欢迎大家加入。</p>
]]></description>
			<content:encoded><![CDATA[<p>如题，地址为：</p>
<p><a title="http://groups.google.com/group/blackberry-dev" href="http://groups.google.com/group/blackberry-dev">http://groups.google.com/group/blackberry-dev</a></p>
<p>这个组可以讨论任何关于Blackberry的事情。</p>
<p>欢迎大家加入。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.inblackberry.com/web/news/create-a-blackberry-dev-group.html/feed</wfw:commentRss>
		</item>
		<item>
		<title>黑莓通过编程的方式挂电话</title>
		<link>http://www.inblackberry.com/web/development/how-to-disconnect-phone-call.html</link>
		<comments>http://www.inblackberry.com/web/development/how-to-disconnect-phone-call.html#comments</comments>
		<pubDate>Mon, 13 Oct 2008 09:56:25 +0000</pubDate>
		<dc:creator>Taigoo</dc:creator>
		
		<category><![CDATA[开发]]></category>

		<category><![CDATA[api]]></category>

		<category><![CDATA[call]]></category>

		<guid isPermaLink="false">http://www.inblackberry.com/web/development/how-to-disconnect-phone-call.html</guid>
		<description><![CDATA[<p>最近使用了很多在Blackberry运行的来电防火墙软件。其实他们的实现方式很简单。</p>
<p>先分析一下，如何挂断电话呢？如果你在RIM提供的API里找一圈，你会特别失望，因为它根本没有提供对应的API。这点上，我觉得RIM特别不道德。</p>
<p>但是是不是没有方法了呢？我觉得办法还是有的。</p>
<p>我想出的办法就是模拟挂电话这个操作。</p>
<p>3个步骤：</p>
<ul>
<li>按下去:press down</li>
<li>按住，哪怕0.1s:Hold on</li>
<li>松开: press up</li>
</ul>
<p>代码如下：</p>
EventInjector.KeyEvent pressEndKey=
    new]]></description>
			<content:encoded><![CDATA[<p>最近使用了很多在Blackberry运行的来电防火墙软件。其实他们的实现方式很简单。</p>
<p>先分析一下，如何挂断电话呢？如果你在RIM提供的API里找一圈，你会特别失望，因为它根本没有提供对应的API。这点上，我觉得RIM特别不道德。</p>
<p>但是是不是没有方法了呢？我觉得办法还是有的。</p>
<p>我想出的办法就是模拟挂电话这个操作。</p>
<p>3个步骤：</p>
<ul>
<li>按下去:press down</li>
<li>按住，哪怕0.1s:Hold on</li>
<li>松开: press up</li>
</ul>
<p>代码如下：</p>
<pre>EventInjector.KeyEvent pressEndKey=
    new EventInjector.KeyEvent(EventInjector.KeyEvent.KEY_DOWN,
Keypad.map(Keypad.KEY_END),KeypadListener.STATUS_NOT_FROM_KEYPAD,100);

EventInjector.KeyEvent releaseEndKey=
                new EventInjector.KeyEvent(
                    EventInjector.KeyEvent.KEY_UP,
                    Keypad.map(Keypad.KEY_END),
                     KeypadListener.STATUS_NOT_FROM_KEYPAD,100);

EventInjector.invokeEvent(pressEndKey);
EventInjector.invokeEvent(releaseEndKey);</pre>
<p>是不是觉得很简单呢。</p>
<p>不过，这个方法，包括我上面说的几个软件都有一个共同的缺点，而且我认为是一个特别不好的方式，那就是当对方拨通电话之后，响了一声就挂断。这对于打电话的人来说是特别不友好的。</p>
<p>试想一下，你如果打电话给对方，然后啪一声挂了，接着你又打，又是啪一声。你什么感觉？</p>
]]></content:encoded>
			<wfw:commentRss>http://www.inblackberry.com/web/development/how-to-disconnect-phone-call.html/feed</wfw:commentRss>
		</item>
		<item>
		<title>Blackberry HomeScreen上滑动应用程序时修改其图标</title>
		<link>http://www.inblackberry.com/web/development/change-icon-when-rolling-the-application-on-homescreen.html</link>
		<comments>http://www.inblackberry.com/web/development/change-icon-when-rolling-the-application-on-homescreen.html#comments</comments>
		<pubDate>Tue, 16 Sep 2008 12:28:54 +0000</pubDate>
		<dc:creator>Taigoo</dc:creator>
		
		<category><![CDATA[开发]]></category>

		<guid isPermaLink="false">http://www.inblackberry.com/web/development/change-icon-when-rolling-the-application-on-homescreen.html</guid>
		<description><![CDATA[<p>该文难度等级为3。</p>
<p>这个需求很简单，就是滑动Blackberry上的Application,其Icon将会该变。这个问题已经有很多人问我了，于是决定今天在这里解决一下。</p>
<p>效果</p>
<p>先看实现的效果。</p>
<p>之前的效果：</p>
<p><a title="icon-old by confach, on Flickr" href="http://www.flickr.com/photos/taigoo/2862576444/"><img src="http://farm4.static.flickr.com/3174/2862576444_eb4a58c548_o.png" border="0" alt="icon-old"]]></description>
			<content:encoded><![CDATA[<p>该文难度等级为3。</p>
<p>这个需求很简单，就是滑动Blackberry上的Application,其Icon将会该变。这个问题已经有很多人问我了，于是决定今天在这里解决一下。</p>
<p><strong>效果</strong></p>
<p>先看实现的效果。</p>
<p>之前的效果：</p>
<p><a title="icon-old by confach, on Flickr" href="http://www.flickr.com/photos/taigoo/2862576444/"><img src="http://farm4.static.flickr.com/3174/2862576444_eb4a58c548_o.png" border="0" alt="icon-old" width="240" height="160" /></a></p>
<p>滑倒的效果：</p>
<p><a title="icon by confach, on Flickr" href="http://www.flickr.com/photos/taigoo/2862576370/"><img src="http://farm4.static.flickr.com/3231/2862576370_e4b206c9b4_o.png" border="0" alt="icon" width="240" height="160" /></a></p>
<p><strong>原理</strong></p>
<p>为了实现该功能，API提供HomeScreen类，该类有个方法setRolloverIcon来实现该功能。</p>
<p>看看该API。</p>
<pre>public static final void setRolloverIcon(Bitmap rollovericon)

    Sets the rollover icon for use with this application.
    Note:If you have never before specified a main application icon for the application, you will need to call updateIcon and provide an icon before setting the rollover icon, otherwise the system will override the main icon and the rollover icon as set by this method with the default icons for the current theme.
    Note:Changes made via this api are NOT persisted across resets

    Parameters:
        rollovericon - the icon to use when the application icon is in focus on the home screen
    Since:
        JDE 4.1.0

public static final void setRolloverIcon(Bitmap rollovericon,
                                         int index)

    Sets the rollover icon for use with this application entry point. The index corresponds to the order in which alternate entry points were created in the original project workspace, and can be determined via the .rapc file generated for the project.
    Note:If you have never before specified a main application icon for the application, you will need to call updateIcon and provide an icon before setting the rollover icon, otherwise the system will override the main icon and the rollover icon as set by this method with the default icons for the current theme.
    Note:Changes made via this api are NOT persisted across resets

    Parameters:
        rollovericon - the icon to use when the application icon is in focus on the home screen
        index - the index of the application entry point to update
    Throws:
        IllegalArgumentException - if the index doesn't correspond to a valid entry point</pre>
<p><strong>源代码</strong></p>
<pre>/*
 * RollerIconApplication.java
 *
 **
 * Copyright (C) Taiguo Zhang
 *
 * Author: Taiguo Zhang
 * Email: confach (AT) gmail.com
 * Thanks RIM provide the way to solve this issue.
 */

package com.taigoo.blackberry.examples.rollerIcon;

import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.blackberry.api.homescreen.HomeScreen;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.system.*;

/**
 *
 *
 */
public class RollerIconApplication extends UiApplication {
   public static void main(String[] args)
    {
        //check the args and switch the process according to the argus.
        //if it is gui, run as normal.
        if (args != null &amp;&amp; args.length &gt; 0 &amp;&amp; args[0].equals("gui"))
        {
           RollerIconApplication theApp = new RollerIconApplication(false);
            theApp.enterEventDispatcher();
        }
        else
        {
            RollerIconApplication theApp = new RollerIconApplication(true);
            theApp.enterEventDispatcher();
        }
    }
     //构造函数。
     //autoStart -当autoStart为true，程序自动启动，需要修改icon
     //          -当autoStart为false，程序按照常规那样启动，什么都不做
     public RollerIconApplication(boolean autoStart)
    {
        //应用程序设置为自动，这样才能执行下面这段修改icon的代码
        if (autoStart)
        {
        //Setup the rollover icons.
        final Bitmap regIcon = Bitmap.getBitmapResource("1.png");
        final Bitmap icon = Bitmap.getBitmapResource("2.png");

        invokeLater(new Runnable()
        {
            public void run()
            {
                ApplicationManager theApp =ApplicationManager.getApplicationManager();
                boolean isStartup = true;
                //因为程序启动需要时间，所以只能等启动完毕后才能修改。
                while (isStartup)
                {
                    //Check if the BlackBerry has completed its startup process.
                    if (theApp.inStartup())
                    {
                        //The BlackBerry is still starting up, sleep for 1 second.
                        try
                        {
                            Thread.sleep(1000);
                        }
                        catch (Exception ex)
                        {
                          //Couldn't sleep, handle exception.
                        }
                    }
                    else
                    {
                        //The BlackBerry has finished its  startup process.
                        //Set the rollover icons.
                        HomeScreen.updateIcon(regIcon, 0);
                        HomeScreen.setRolloverIcon(icon, 0);
                        isStartup = false;
                    }
                 }
                 //直接退出应用程序
                 System.exit(0);
            }
        });

    }
    else
    {
         //正常启动
         MainScreen ms = new MainScreen();
         ms.setTitle(new LabelField("Hello there."));
         this.pushScreen(ms);
    }
  }
}</pre>
<p><strong>代码分析</strong></p>
<p>可以从以下几个方面去考虑</p>
<ul>
<li>为了修改Icon，必须调用setRolloverIcon</li>
<li>为了调用setRolloverIcon，必须有bitmap，那就有了：
<pre>final Bitmap regIcon = Bitmap.getBitmapResource("1.png");
final Bitmap icon = Bitmap.getBitmapResource("2.png");</pre>
</li>
<li>因为每个应用程序需有启动时间，只有当启动之后才好设置，于是就有了上面的循环。如果正在运行，让等待1s，然后再判断，直到启动为止，这样就可以设置了。</li>
<li>因为改变是一个功能，另外的就是程序的正常业务功能。这就需要在启动时增加一个参数，这里是gui。只有放启动时为gui，那么程序才能按照正常那样工作。</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.inblackberry.com/web/development/change-icon-when-rolling-the-application-on-homescreen.html/feed</wfw:commentRss>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 1.840 seconds -->
