SpinnerDemo.java
/* * SpinnerDemo.java * * Copyright �1998-2010 Research In Motion Ltd. * * Note: For the sake of simplicity, this sample application may not leverage * resource bundles and resource strings. However, it is STRONGLY recommended * that application developers make use of the localization features available * within the BlackBerry development platform to ensure a seamless application * experience across a variety of languages and geographies. For more information * on localizing your application, please refer to the BlackBerry Java Development * Environment Development Guide associated with this release. */ package com.rim.samples.device.ui.spinnerdemo; import net.rim.device.api.ui.UiApplication; /** * This sample application demonstrates the * net.rim.device.api.ui.picker.DateTimePicker class and the * net.rim.device.api.ui.component.TextSpinBoxField class. */ public class SpinnerDemo extends UiApplication { /** * Entry point for application * @param args Command line arguments (not used) */ public static void main(String[] args) { // Create a new instance of the application and make the currently // running thread the application's event dispatch thread. SpinnerDemo app = new SpinnerDemo(); app.enterEventDispatcher(); } /** * Creates a new SpinnerDemo object */ public SpinnerDemo() { pushScreen(new SpinnerDemoScreen()); } }
SpinnerDemoScreen.java
/* * SpinnerDemoScreen.java * * Copyright �1998-2010 Research In Motion Ltd. * * Note: For the sake of simplicity, this sample application may not leverage * resource bundles and resource strings. However, it is STRONGLY recommended * that application developers make use of the localization features available * within the BlackBerry development platform to ensure a seamless application * experience across a variety of languages and geographies. For more information * on localizing your application, please refer to the BlackBerry Java Development * Environment Development Guide associated with this release. */ package com.rim.samples.device.ui.spinnerdemo; import java.util.Calendar; import java.util.Date; import net.rim.device.api.i18n.SimpleDateFormat; import net.rim.device.api.ui.Field; import net.rim.device.api.ui.Screen; import net.rim.device.api.ui.TouchEvent; import net.rim.device.api.ui.UiApplication; import net.rim.device.api.ui.component.BasicEditField; import net.rim.device.api.ui.component.DateField; import net.rim.device.api.ui.component.LabelField; import net.rim.device.api.ui.component.SeparatorField; import net.rim.device.api.ui.component.TextSpinBoxField; import net.rim.device.api.ui.container.HorizontalFieldManager; import net.rim.device.api.ui.container.MainScreen; import net.rim.device.api.ui.container.PopupScreen; import net.rim.device.api.ui.container.VerticalFieldManager; import net.rim.device.api.ui.picker.DateTimePicker; /** * MainScreen class for the Date Time Picker Spinner Demo application */ public class SpinnerDemoScreen extends MainScreen { private static final int DEFAULT = 1; private static final int DATE = 2; private static final int TIME = 3; private static final int LONG = 4; private static final int EXPIRY = 5; private static final int SPINBOX = 6; private BasicEditField _editFieldDefault; private BasicEditField _editFieldDate; private BasicEditField _editFieldTime; private BasicEditField _editFieldLong; private BasicEditField _editFieldExpiry; private BasicEditField _editFieldSpinbox; private final SimpleDateFormat _defaultDateFormat = new SimpleDateFormat(SimpleDateFormat.DATETIME_DEFAULT|SimpleDateFormat.TIME_DEFAULT); private final SimpleDateFormat _dateFormat = new SimpleDateFormat("yyyy-MM-dd"); private final SimpleDateFormat _timeFormat = new SimpleDateFormat("hh:mm:ss aa"); private final SimpleDateFormat _longDateFormat = new SimpleDateFormat(SimpleDateFormat.DATE_LONG|SimpleDateFormat.TIME_DEFAULT); private final SimpleDateFormat _expiryDateFormat = new SimpleDateFormat("MM/yyyy"); private Calendar _defaultCal; private Calendar _dateCal; private Calendar _timeCal; private Calendar _longDateCal; private Calendar _expiryDateCal; private CustomSpinnerPopup _customSpinnerPopup; /** * Creates a new SpinnerDemoScreen object */ public SpinnerDemoScreen() { setTitle("Spinner Demo"); // Initialize a VerticalFieldManager VerticalFieldManager fieldManager = new VerticalFieldManager(); // Add a typical date field to the VerticalFieldManager fieldManager.add(new LabelField("Date Field\n",Field.FIELD_HCENTER)); DateField dateField = new DateField("Date/time: ", System.currentTimeMillis(), DateField.DATE_TIME); fieldManager.add(dateField); // Add UI elements to the VerticalFieldManager, including fields // which will allow a user to make selections from date/time and // custom spinners. fieldManager.add(new SeparatorField()); fieldManager.add(new LabelField("Date Time Picker\n",Field.FIELD_HCENTER)); _editFieldDefault = new BasicEditField("Default: ","Click to select"); fieldManager.add(_editFieldDefault); _editFieldDate = new BasicEditField("Date only: ","Click to select"); fieldManager.add(_editFieldDate); _editFieldTime = new BasicEditField("Time only: ","Click to select"); fieldManager.add(_editFieldTime); _editFieldLong = new BasicEditField("Long date: ","Click to select"); fieldManager.add(_editFieldLong); _editFieldExpiry = new BasicEditField("Expiry date: ","Click to select"); fieldManager.add(_editFieldExpiry); fieldManager.add(new SeparatorField()); fieldManager.add(new LabelField("Text Spin Box Field\n",Field.FIELD_HCENTER)); _editFieldSpinbox = new BasicEditField("City: ","Click to select"); fieldManager.add(_editFieldSpinbox); // Add the VerticalFieldManager to the screen add(fieldManager); } /** * Displays a spinner dialog for a given type * @param type Type of spinner to display */ void showSpinnerDialog(int type) { switch(type) { case DEFAULT: DateTimePicker datePicker = DateTimePicker.createInstance(_defaultCal); if(datePicker.doModal()) { StringBuffer dateStr = new StringBuffer(); _defaultCal = datePicker.getDateTime(); _defaultDateFormat.format(_defaultCal, dateStr, null); _editFieldDefault.setText(dateStr.toString()); } break; case DATE: DateTimePicker datePickerDate = DateTimePicker.createInstance( _dateCal, "yyyy-MM-dd", null); if(datePickerDate.doModal()) { StringBuffer dateStrDate = new StringBuffer(); _dateCal = datePickerDate.getDateTime(); _dateFormat.format(_dateCal, dateStrDate, null); _editFieldDate.setText(dateStrDate.toString()); } break; case TIME: DateTimePicker datePickerTime = DateTimePicker.createInstance( _timeCal, null, "hh:mm:ss aa"); if(datePickerTime.doModal()) { StringBuffer dateStrTime = new StringBuffer(); _timeCal = datePickerTime.getDateTime(); _timeFormat.format(_timeCal, dateStrTime, null); _editFieldTime.setText(dateStrTime.toString()); } break; case LONG: DateTimePicker datePickerLong = DateTimePicker.createInstance( _longDateCal, SimpleDateFormat.DATE_LONG, SimpleDateFormat.TIME_DEFAULT); if(datePickerLong.doModal()) { StringBuffer dateStrLong = new StringBuffer(); _longDateCal = datePickerLong.getDateTime(); _longDateFormat.format(_longDateCal, dateStrLong, null); _editFieldLong.setText(dateStrLong.toString()); } break; case EXPIRY: DateTimePicker datePickerExpiry = DateTimePicker.createInstance( _expiryDateCal, _expiryDateFormat.toPattern(), null); if(datePickerExpiry.doModal()) { StringBuffer dateStrExpiry = new StringBuffer(); _expiryDateCal = datePickerExpiry.getDateTime(); _expiryDateFormat.format(_expiryDateCal, dateStrExpiry, null); _editFieldExpiry.setText(dateStrExpiry.toString()); } break; case SPINBOX: if(_customSpinnerPopup == null) { _customSpinnerPopup = new CustomSpinnerPopup(); } UiApplication.getUiApplication().pushModalScreen(_customSpinnerPopup); if(_customSpinnerPopup.isSet()) { String choice = _customSpinnerPopup.getChoice(); _editFieldSpinbox.setText(choice); } break; } } /** * @see Screen#touchEvent(TouchEvent) */ protected boolean touchEvent(TouchEvent message) { if(message.getEvent() == TouchEvent.CLICK) { int type= 0; if(_editFieldDefault.isFocus()) { type = DEFAULT; } else if (_editFieldDate.isFocus()) { type = DATE; } else if (_editFieldTime.isFocus()) { type = TIME; } else if (_editFieldLong.isFocus()) { type = LONG; } else if (_editFieldExpiry.isFocus()) { type = EXPIRY; } else if (_editFieldSpinbox.isFocus()) { type = SPINBOX; } if(type > 0) { showSpinnerDialog(type); } return true; } return super.touchEvent(message); } /** * @see Screen#navigationClick(int, int) */ protected boolean navigationClick(int status, int time) { int type = 0; boolean returnValue = false; if(_editFieldDefault.isFocus()) { type = DEFAULT; returnValue = true; } else if (_editFieldDate.isFocus()) { type = DATE; returnValue = true; } else if (_editFieldTime.isFocus()) { type = TIME; returnValue = true; } else if (_editFieldLong.isFocus()) { type = LONG; returnValue = true; } else if (_editFieldExpiry.isFocus()) { type = EXPIRY; returnValue = true; } else if (_editFieldSpinbox.isFocus()) { type = SPINBOX; returnValue = true; } if(type > 0) { showSpinnerDialog(type); } return returnValue; } /** * @see MainScreen#onSavePrompt() */ protected boolean onSavePrompt() { // Suppress the save dialog return true; } /** * A PopupScreen to display a TextSpinBoxField */ public final static class CustomSpinnerPopup extends PopupScreen { private TextSpinBoxField _spinBoxField; private boolean _isSet; /** * Creates a new CustomSpinnerPopup object */ public CustomSpinnerPopup() { super(new VerticalFieldManager(), Screen.DEFAULT_CLOSE); String[] choices = {"New York", "Paris", "Barcelona", "Beijing", "Moscow", "Brasilia", "Melbourne"}; _spinBoxField = new TextSpinBoxField(choices); _spinBoxField.setVisibleRows(3); add(new LabelField("Choose city:")); add(new SeparatorField()); HorizontalFieldManager hfm = new HorizontalFieldManager(Field.FIELD_HCENTER); hfm.add(_spinBoxField); add(hfm); } /** * Retrieves the currently selected choice * @return The currently selected choice */ public String getChoice() { return (String)_spinBoxField.get(_spinBoxField.getSelectedIndex()); } /** * Indicates whether the TextSpinBoxField has changed from * its initial state. * @return True if the selected choice has been modified, otherwise false */ public boolean isSet() { return _isSet; } /** * @see Screen#invokeAction(int) */ protected boolean invokeAction(int action) { if(action == ACTION_INVOKE) { if(!_isSet) { _isSet = true; } close(); return true; } return false; } /** * @see Screen#close() */ public void close() { if(!_isSet) { _spinBoxField.setSelectedIndex(0); } super.close(); } } }
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/icon_home_button_img"/>
<item android:drawable="@drawable/icon_home_shape_overlay"/> </layer-list>
icon_home_shape_overlay如下
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#60000000"/>
<stroke android:width="3dp" color="#ff000000"/>
<corners android:radius="10dp" />
</shape>
或者直接使用一种效果
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#99FFFFFF"/>
<corners android:radius="30px"/>
<padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp" />
</shape>
然后 android:background="@drawable/my_shape_file"
2.图片本身加上圆角
Bitmap myCoolBitmap = ... ; // <-- Your bitmap you want rounded
int w = myCoolBitmap.getWidth(), h = myCoolBitmap.getHeight();
Bitmap rounder = Bitmap.createBitmap(w,h,Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(rounder);
Paint xferPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
xferPaint.setColor(Color.RED);
canvas.drawRoundRect(new RectF(0,0,w,h), 20.0f, 20.0f, xferPaint);
xferPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
canvas.drawBitmap(myCoolBitmap, 0,0, null);
canvas.drawBitmap(rounder, 0, 0, xferPaint);
或者
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
final float roundPx = 12;
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
SocketDemo.java
/** * SocketDemo.java * * Copyright �1998-2010 Research In Motion Ltd. * * Note: For the sake of simplicity, this sample application may not leverage * resource bundles and resource strings. However, it is STRONGLY recommended * that application developers make use of the localization features available * within the BlackBerry development platform to ensure a seamless application * experience across a variety of languages and geographies. For more information * on localizing your application, please refer to the BlackBerry Java Development * Environment Development Guide associated with this release. */ package com.rim.samples.device.socketdemo; import net.rim.device.api.ui.*; /** * This sample enables client/server communication using a simple implementation * of TCP sockets. The client application allows the user to select direct TCP as * the connection method. If direct TCP is not selected, a proxy TCP connection * is opened using the BlackBerry MDS Connection Service. The server application * can be found in com/rim/samples/server/socketdemo. */ public class SocketDemo extends UiApplication { SocketDemoScreen _screen; /** * Entry point for application. * @param Command line arguments. */ public static void main(String[] args) { // Create a new instance of the application and make the currently // running thread the application's event dispatch thread. SocketDemo app = new SocketDemo(); app.enterEventDispatcher(); } // Constructor public SocketDemo() { // Create a new screen for the application. _screen = new SocketDemoScreen(); // Push the screen onto the UI stack for rendering. pushScreen(_screen); } /** * Provides access to this application's UI screen * @return This application's UI screen. */ SocketDemoScreen getScreen() { return _screen; } }
SocketDemoScreen.java
/* * SocketDemoScreen.java * * Copyright �1998-2010 Research In Motion Ltd. * * Note: For the sake of simplicity, this sample application may not leverage * resource bundles and resource strings. However, it is STRONGLY recommended * that application developers make use of the localization features available * within the BlackBerry development platform to ensure a seamless application * experience across a variety of languages and geographies. For more information * on localizing your application, please refer to the BlackBerry Java Development * Environment Development Guide associated with this release. */ package com.rim.samples.device.socketdemo; import net.rim.device.api.system.*; import net.rim.device.api.ui.*; import net.rim.device.api.ui.component.*; import net.rim.device.api.ui.container.*; /** * A MainScreen class to allow for user interaction. */ public class SocketDemoScreen extends MainScreen { private EditField _hostField; private CheckboxField _useDirectTcpField; private RichTextField _statusField; private StringBuffer _message; private boolean _threadRunning = false; // Constructor public SocketDemoScreen() { setTitle(new LabelField("Socket Demo")); add(new RichTextField("Enter local host name in the field below and select 'Go' from the menu." ,Field.NON_FOCUSABLE)); add(new SeparatorField()); // Need to get the local host name from the user because access to // 'localhost' and 127.0.0.1 is restricted. _hostField = new EditField("Local Host: " , ""); add(_hostField); _useDirectTcpField = new CheckboxField("Use Direct TCP" , RadioInfo.getNetworkType() == RadioInfo.NETWORK_IDEN); add(_useDirectTcpField); _statusField = new RichTextField(Field.NON_FOCUSABLE); add(_statusField); _message = new StringBuffer(); } /** * Method to display a message to the user. * @param msg The message to display. */ void updateDisplay(final String msg) { UiApplication.getUiApplication().invokeLater(new Runnable() { public void run() { _message.append(msg); _message.append('\n'); _statusField.setText(_message.toString()); } }); } /** * Returns the text entered by the user. * @return text entered by the user. */ String getHostFieldText() { return _hostField.getText(); } /** * Indicates whether the direct TCP checkbox is checked. * @return True if checkbox is checked, otherwise false. */ boolean isDirectTCP() { return _useDirectTcpField.getChecked(); } /** * Setter for boolean _threadRunning * @param running True if a ConnectThread is running, otherwise false. */ void setThreadRunning(boolean running) { _threadRunning = running; } /** *@see net.rim.device.api.ui.container.MainScreen#makeMenu(Menu,int) */ protected void makeMenu(Menu menu, int instance) { // If a ConnectThread is running we won't add our menu item if (!_threadRunning) { menu.add(_go); } super.makeMenu(menu, instance); } /** * Prevent the save dialog from being displayed, nothing to save. * * @see net.rim.device.api.ui.container.MainScreen#onSavePrompt() */ public boolean onSavePrompt() { return true; } /** * Handles the user pressing ENTER while the * 'use direct tcp' CheckboxField has focus. * * @see net.rim.device.api.ui.Screen#keyChar(char,int,int) * */ protected boolean keyChar( char key, int status, int time ) { if ( key == Characters.ENTER ) { Field fieldWithFocus = getFieldWithFocus(); if(fieldWithFocus == _useDirectTcpField) { if(_useDirectTcpField.getChecked()) { _useDirectTcpField.setChecked(false); } else { _useDirectTcpField.setChecked(true); } return true; // We've consumed the event. } } return super.keyChar( key, status, time ); // We'll let super handle the event. } /** * An anonymous MenuItem class. */ private MenuItem _go = new MenuItem("Go" , 11000, 0) { public void run() { // Don't do anything unless there is a host name in the _host field. if (_hostField.getText().length() > 0) { new ConnectThread().start(); _threadRunning = true; // Hide the virtual keyboard so the user can see status updates. if(VirtualKeyboard.isSupported()) { VirtualKeyboard keyboard = getVirtualKeyboard(); if(keyboard.getVisibility() != VirtualKeyboard.HIDE) { keyboard.setVisibility(VirtualKeyboard.HIDE); } } } else { Dialog.ask(Dialog.D_OK, "Please enter a valid host name" ); } } }; }
ConnectThread.java
/* * ConnectThread.java * * Copyright �1998-2010 Research In Motion Ltd. * * Note: For the sake of simplicity, this sample application may not leverage * resource bundles and resource strings. However, it is STRONGLY recommended * that application developers make use of the localization features available * within the BlackBerry development platform to ensure a seamless application * experience across a variety of languages and geographies. For more information * on localizing your application, please refer to the BlackBerry Java Development * Environment Development Guide associated with this release. */ package com.rim.samples.device.socketdemo; import java.io.*; import javax.microedition.io.*; import net.rim.device.api.ui.*; /** * A thread class to handle communication with the server component. */ public class ConnectThread extends Thread { private InputStreamReader _in; private OutputStreamWriter _out; private SocketDemoScreen _screen; // Constructor public ConnectThread() { _screen = ((SocketDemo)UiApplication.getUiApplication()).getScreen(); } /** * Pass some data to the server and wait for a response. * @param data The data to send. */ private void exchange(String data) throws IOException { // Cache the length locally for better efficiency. int length = data.length(); // Create an input array just big enough to hold the data // (we're expecting the same string back that we send). char[] input = new char[length]; _out.write(data, 0, length); // Read character by character into the input array. for (int i = 0; i < length; ++i) { input[i] = (char)_in.read(); } // Hand the data to the parent class for updating the GUI. By explicitly // creating the stringbuffer we can save a few object creations. StringBuffer s = new StringBuffer(); s.append("Received: ") ; s.append(input, 0, length); _screen.updateDisplay(s.toString()); } /** * Implementation of Thread. */ public void run() { StreamConnection connection = null; try { _screen.updateDisplay("Opening Connection..."); String url = "socket://" + _screen.getHostFieldText() + ":44444" + (_screen.isDirectTCP() ? ";deviceside=true" : ""); connection = (StreamConnection)Connector.open(url); _screen.updateDisplay("Connection open"); _in = new InputStreamReader(connection.openInputStream()); _out = new OutputStreamWriter(connection.openOutputStream()); // Send the HELLO string. exchange("Hello"); // Execute further data exchange here... // Send the GOODBYE string. exchange("Goodbye and farewell"); _screen.updateDisplay("Done!"); } catch(IOException e) { System.err.println(e.toString()); } finally { _screen.setThreadRunning(false); try { _in.close(); } catch(IOException ioe) { } try { _out.close(); } catch(IOException ioe) { } try { connection.close(); } catch(IOException ioe) { } } } }