Blackberry HomeScreen上滑动应用程序时修改其图标
Confach 发表于 September 16, 2008 8:28 pm
版权信息 :严禁转载, 若想推荐或收藏,请用链接的形式.
该文难度等级为3。
这个需求很简单,就是滑动Blackberry上的Application,其Icon将会该变。这个问题已经有很多人问我了,于是决定今天在这里解决一下。
效果
先看实现的效果。
之前的效果:
滑倒的效果:
原理
为了实现该功能,API提供HomeScreen类,该类有个方法setRolloverIcon来实现该功能。
看看该API。
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
源代码
/*
* 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 && args.length > 0 && 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);
}
}
}
代码分析
可以从以下几个方面去考虑
- 为了修改Icon,必须调用setRolloverIcon
- 为了调用setRolloverIcon,必须有bitmap,那就有了:
final Bitmap regIcon = Bitmap.getBitmapResource("1.png"); final Bitmap icon = Bitmap.getBitmapResource("2.png"); - 因为每个应用程序需有启动时间,只有当启动之后才好设置,于是就有了上面的循环。如果正在运行,让等待1s,然后再判断,直到启动为止,这样就可以设置了。
- 因为改变是一个功能,另外的就是程序的正常业务功能。这就需要在启动时增加一个参数,这里是gui。只有放启动时为gui,那么程序才能按照正常那样工作。



1. jacul 发表于 December 14,2008 01:44:04
改变图标的时候程序已经运行了吗?main函数里的args是什么意思?或者说每次光标移到这个程序上就先在后台启动,改变图标之后退出,难道是这样?