まず、ビットマップを作成し、そこからグラフィックスオブジェクトを取得して、カスタム描画を行うことができます。
Bitmap myBitmap = new Bitmap(widht, height);
Graphics graphics = Graphics.create(myBitmap);
// now you can draw anything on graphics supported by API
[編集]
画面上に画像をドラッグするためのコードスニペットを追加しました。このコードが自分の側で機能するかどうかを確認できます。
class MyScreen extends MainScreen {
private final Bitmap myBm;
private int wBm;
private int hBm;
private int xBm;
private int yBm;
private boolean dragStarted;
public MyScreen() {
myBm = Bitmap.getBitmapResource("img/myBitmap.png");
if (myBm != null) {
wBm = myBm.getWidth();
hBm = myBm.getHeight();
}
}
protected void paint(Graphics graphics) {
// need to adjust z-index of the items added on screen.
super.paint(graphics);
if (myBm != null) {
graphics.drawBitmap(xBm, yBm, wBm, hBm, myBm, 0, 0);
}
}
protected boolean touchEvent(TouchEvent event) {
int x = event.getX(1);
int y = event.getY(1);
int eventCode = event.getEvent();
if (dragStarted == false & (eventCode == TouchEvent.DOWN)) {
if (isDragActivated(x, y)) {
// need to adjust offset.
dragStarted = true;
updateBitmapLocation(x, y);
}
}
if (eventCode == TouchEvent.MOVE && dragStarted) {
updateBitmapLocation(x, y);
}
if (eventCode == TouchEvent.UP && dragStarted) {
dragStarted = false;
updateBitmapLocation(x, y);
}
// need to decide what to return
return true;
}
private void updateBitmapLocation(int xTouch, int yTouch) {
if (xTouch != xBm && yTouch != yBm) {
xBm = xTouch;
yBm = yTouch;
invalidate();
}
}
private boolean isDragActivated(int x, int y) {
return (x >= xBm && x <= xBm + wBm && y >= yBm && y <= yBm + hBm);
}
}