Beruflich Dokumente
Kultur Dokumente
Page | 1
Contents
1. Introducing the S-Pen SDK ..................................................................................................... 4
1-1. About the S-Pen SDK ................................................................................................... 4
1-2. Packages in the S-Pen SDK .......................................................................................... 4
2. Setting up the S-Pen SDK ....................................................................................................... 5
2-1. The S-Pen SDK ............................................................................................................. 5
2-2. Installing the S-Pen Library to an Android project ...................................................... 5
3. Getting Started with the Sample Code .................................................................................. 6
3-1. Sample Code: Example 1 ............................................................................................ 6
3-2. The CanvasView Class ................................................................................................. 8
3-2-1. Initializing the CanvasView .............................................................................. 8
3-2-2. Changing the mode for the CanvasView ......................................................... 9
3-2-3. The Undo/Redo operations in the CanvasView............................................. 10
3-2-4. Saving/Loading Drawings .............................................................................. 12
3-2-5. Zooming In and Out ....................................................................................... 13
3-2-7. Setting the Background ................................................................................. 15
3-3. The SettingView Class ............................................................................................... 15
3-3-1. Initializing the SettingView ............................................................................ 15
3-3-2. Set the SettingView to the CanvasView ........................................................ 16
3-3-3. Showing/Closing the SettingView ................................................................. 16
3-3-4. Making Sure That the Settingview on the Screen is Being Viewed ............... 17
3-3-5. Pen/Eraser SettingView ................................................................................. 18
3-4. The PenSettingInfo Class ........................................................................................... 19
3-4-1. Initializing the PenSettingInfo........................................................................ 19
3-4-2. Using the OnSettingInfoChangedListener listener ........................................ 19
3-4-3. Replacing the CanvasView’S-PenSettingInfo with a Custom Object ............. 20
3-5. The SCanvasView Class ............................................................................................. 20
3-5-1. Initializing the SCanvasView .......................................................................... 20
3-5-2. Basic Animation Functions ............................................................................ 21
3-5-3. Using onPlayCompleteListener ...................................................................... 23
3-5-4. Using onPlayProgressChangeListener ........................................................... 24
3-5-5. Adding Text to the Canvas ............................................................................. 24
3-5-6. Adding Images to the Canvas ........................................................................ 26
3-5-7. Using the onSelectChangeListener Listener .................................................. 26
3-5-8. Using onFileProcessingProgressListener ....................................................... 27
3-5-9. Background Settings ...................................................................................... 28
3-5-10. Scratch effect ............................................................................................... 29
3-5-11. Adding Additional Information to the Canvas ............................................. 30
Page | 2
Copyright
Page | 3
S-Pen SDK 2.0.1 Tutorial
with Sample Code
This document provides S-Pen installation instructions, a tutorial, and sample code to help
developers make the most of the S-Pen SDK. For detailed information about how to use the
S-Pen SDK APIs, please refer to the API in the Documents directory of the SDK.
This document consists of an introduction and the basic installation method for the S-Pen
SDK and describes the API functions through simple sample code. Information on
configuring and constructing the basic Android development environment is not included.
Page | 4
2. Setting up the S-Pen SDK
2-1. The S-Pen SDK
The following components are required to create the Android Project in Eclipse necessary
to use the S-Pen SDK. For information about how to download and install them, please
refer to the associated links.
JDK : Java SE Development Kit
http://www.oracle.com/technetwork/java/javase/downloads/index.html
Android SDK
http://developer.android.com/sdk/index.html
IDE : Eclipse
http://www.eclipse.org/downloads/
Page | 5
3. Getting Started with the Sample Code
3-1. Sample Code: Example 1
The SDK sample code in Example 1 demonstrates the basic S-Pen APIs.
The Example #1 sample code generates a screen with buttons in the top pane and a canvas
area in the bottom pane. The buttons in the top pane perform the following functions:
pen – Switches to the pen function or opens the pen settings popup window in pen
mode.
erase – Switches to the eraser function or opens the eraser settings popup window
in eraser mode.
undo – Cancels a drawing operation.
redo – Performs the last canceled drawing again.
The canvas area in the bottom pane of the screen shows what the user has drawn on the
screen.
CanvasView and SettingView are S-Pen API classes used in Example 1’s sample code.
For more detailed information on the available APIs, please refer to the API reference
documents in the Documentation directory of the SDK.
Page | 6
Below is the layout for Example 1.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/settingBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="pen"
android:layout_weight="1"/>
<Button
android:id="@+id/eraseBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="erase"
android:layout_weight="1"/>
<Button
android:id="@+id/undoBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="undo"
android:layout_weight="1"/>
<Button
android:id="@+id/redoBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="redo"
android:layout_weight="1"/>
</LinearLayout>
<FrameLayout
android:id="@+id/canvas_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
Page | 7
<com.samsung.sdraw.CanvasView
android:id="@+id/canvas_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/smemo_bg" />
<com.samsung.sdraw.SettingView
android:id="@+id/setting_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>
</LinearLayout>
The layout in the execution screen consists of the button area in the top pane and the
canvas area in the bottom pane. Note that the S-Pen SDK’s View of the CanvasView and
SettingView are included in the bottom pane of the screen.
Page | 8
The sample code above allows you to draw on the screen.
// Eraser Mode
mCanvasView.changeModeTo( CanvasView.ERASER_MODE );
Page | 9
3-2-3. The Undo/Redo operations in the CanvasView
Redo or undo the drawing on the CanvasView with single instructions:
mCanvasView.undo(); // Undo
mCanvasView.redo(); // Redo
The undo and redo operations are executed through the undo and redo methods of the
CanvasView in the click event listeners of the corresponding buttons. The maximum
number for the undo or redo operations is currently 30.
Page | 10
Identify the availability of the undo and redo operations using the isUndoable and
isRedoable methods, and enable or disable the buttons accordingly.
mUndoBtn.setEnabled( mCanvasView.isUndoable());
// Check Undoable
mRedoBtn.setEnabled( mCanvasView.isRedoable());
// Check Redoable
[Code 05] Changing the Status of the Buttons Depending on the Availability of Undo
and Redo Operations.
Page | 11
Additionally, you can set up the OnHistoryChangeListener listener, which is the
drawing event listener for the CanvasView. It can identify if the undo or redo operation is
available through the listener.
mCanvasView.setOnHistoryChangeListener(historyChangeListener);
// Set the Listener for the CanvasView
private CanvasView.OnHistoryChangeListener
historyChangeListener = new
CanvasView.OnHistoryChangeListener()
{
@Override
public void onHistoryChanged( boolean bUndoable,
boolean bRedoable)
{
}
};
// Save
public boolean saveCanvas()
{
byte[] buffer = mCanvasView.getData();
Page | 12
if( ExampleUtils.writeBytedata(savePath, buffer))
return true;
else
return false;
}
// Load
public boolean loadCanvas( String filename )
{
String loadPath = mFolder.getPath() + "/"
+ fileName;
byte[] buffer
= ExampleUtils.readBytedata(loadPath);
mCanvasView.setData(buffer);
return true;
}
mCanvasView.zoomTo( 2 );
//Enlarge the view 2 times, compared to the original size
mCanvasView.zoomTo( 1 );
//Go back to the image’s original size
Page | 13
[Figure 05] Zooming in
3-2-6. Panning
Pan across the drawing screen of the CanvasView by specifying the x and y coordinates.
The SPen_Example_ZoomPan sample application shows how to use this feature.
Note that the move distance applied regardless of the zoom level. So if you move the
canvas by 1 pixel horizontally when the zoom level is 2x, it will only move by 1 pixel on the
screen (not 2 pixels).
Page | 14
3-2-7. Setting the Background
The background is divided into top, middle and bottom. To set a background for your
CanvasView, specify separate images for each part.
Bitmap topBg =
BitmapFactory.decodeResource( getResources(),
R.drawable.bg_top);
Bitmap middleBg =
BitmapFactory.decodeResource( getResources(),
R.drawable. bg_middle);
Bitmap bottomBg =
BitmapFactory.decodeResource( getResources(),
R.drawable.bg_bottom );
mCanvasView.setBackgroundTemplete
( topBg, middleBg, bottomBg );
It is best to use images with the same width as the canvas. Otherwise, the images are
stretched to the width of the CanvasView while keeping the aspect ratio.
mSettingView = ( SettingView )
findViewById( R.id.setting_view );
Page | 15
3-3-2. Set the SettingView to the CanvasView
Connect the CanvasView with the SettingView so that the changes you make to
the pen settings in the SettingView are applied to the CanvasView. Do this using
the setSettingView method of the CanvasView.
However, as you can see in the code of Example #1, you have to change the mode in
advance using the changeModeTo method of the ModeContext in the CanvasView
to show the proper settings popup window on the screen.
Page | 16
[Figure 06] Pen/Eraser Mode Settings Popup Windows
3-3-4. Making Sure That the Settingview on the Screen is Being Viewed
To identify if the SettingView is being displayed on the screen, use the following
sample code using the isShown method. However, to determine if the settings popup
window is for pen or eraser mode, you must deliver the SettingView type as the
argument.
[Code 15] Identifying the Status of the Pen or the Erase Settings Popup Window
Page | 17
3-3-5. Pen/Eraser SettingView
In the pen settings popup window, you can select one of the four pen types available (solid,
brush, pencil, highlighter) and set the color, thickness, and transparency of the chosen pen
type. The settings popup window provides a preview of the configured pen properties.
In the eraser settings popup window, adjust the eraser thickness and erase all the drawings
from the CanvasView using the Clear All button.
Page | 18
3-4. The PenSettingInfo Class
The PenSettingInfo class manages the pen settings. It has methods for getting and
setting various pen/eraser parameters, such as width, alpha, color and pen type. It is used
internally by the CanvasView and SettingView, but you can also construct the
PenSettingInfo object manually, using a class constructor. The sample application
SPen_Example_PenSettingInfo shows how to use this class (you can find the full code in
Example #3 of the S-Pen SDK).
mPenSettingInfo = mCanvasView.getPenSettingInfo();
mPenSettingInfo.setOnSettingInfoChangedListener
( mOnSettingInfoChangedListener );
This listener implements functions for changing the pen type, width, color and alpha.
PenSettingInfo.OnSettingInfoChangedListener
mOnSettingInfoChangedListener
= new PenSettingInfo.OnSettingInfoChangedListener()
{
@Override
public void onPenWidthChanged(int arg0, int arg1) {
… }
@Override
public void onPenTypeChanged(int arg0) { … }
@Override
public void onPenColorChanged(int arg0, int arg1)
{ … }
@Override
Page | 19
public void onPenAlphaChanged(int arg0, int arg1) {
…
}
@Override
public void onEraserWidthChanged(int arg0) {
…
}
};
Page | 20
3-5-2. Basic Animation Functions
When using the SCanvasView class, all canvas operations such as drawing and inserting
text and images are saved and can be viewed as an animation. To use this feature create a
SAMM library inside your SCanvasView.
canvasOption.mPlayOption
.setTransparentBGImageAnimation(false);
// BG Audio volume
if(!canvasOption.mPlayOption.setBGAudioVolume(1.0f))
return;
//Animation speed
if(!canvasOption.mPlayOption
.setAnimationSpeed
(SOptionPlay.ANIMATION_SPEED_AUTO) )
return;
Page | 21
//Apply options to SCanvasView
if(!mCanvasView.setOption(canvasOption))
return;
For more options see the S-Pen API documentation for the SOptionPlay
And SOptionSAMM classes.
// Start Animation
mCanvasView.doAnimationStart();
Page | 22
You can check the animation state by using the getAnimationState() function and
change it accordingly.
if(nAnimationState==SAMMLibConstants.ANIMATION_STATE_ON_STOP)
{
// Start Animation
mCanvasView.doAnimationStart();
}
else if( nAnimationState ==
SAMMLibConstants.ANIMATION_STATE_ON_PAUSED )
mCanvasView.doAnimationResume();
else if( nAnimationState ==
SAMMLibConstants.ANIMATION_STATE_ON_RUNNING )
mCanvasView.doAnimationPause();
}
Page | 23
3-5-4. Using onPlayProgressChangeListener
The onPlayProgressChangeListener is called when animation progress changes.
Its function onChangeProgress(int nProgress) gets the current animation
progress indicator in percent, from 0 to 100.
mProgress.setProgress(nProgress);
}
};
mCanvasView.changeModeTo( CanvasView.TEXT_MODE );
Page | 24
Tapping the canvas will open a scalable text box.
Page | 25
3-5-6. Adding Images to the Canvas
To add images to the canvas, create a SObjectImage object and add it to canvas with
the
insertSAMMImage( SObjectImage imgData,
boolean bSelectObject ) function.
Page | 26
3-5-8. Using onFileProcessingProgressListener
This listener monitors the progress of loading files. It has two functions:
@Override
public void onLoadComplete(boolean bLoadResult) {
…
}
};
Page | 27
3-5-9. Background Settings
There are a number of background settings you can use; for example, you can add a
memo to background.
pageMemo.setText(tmpStr);
mCanvasView.setPageMemo(pageMemo, 0);
You can either set a specific background color or use an image from a file.
Page | 28
3-5-10. Scratch effect
To create a “scratch off” effect, where an image can be “scratched off” to reveal another
image, set the cover image with the setClearImageBitmap function.
Page | 29
3-5-11. Adding Additional Information to the Canvas
You can also add other information to your canvas, such as title and tags.
Page | 30