I created a view to draw custom objects and graphics class to draw my objects. Here is a code example.
Graphics.java class
private Canvas m_g;
private Paint m_paint;
private int m_iTranslateX;
private int m_iTranslateY;
private Rect m_rect = new Rect();
public void drawImage( Bitmap image, int iAlpha, int iX, int iY, int iWidth, int iHeight, float fpRotate ) {
if ( iWidth == 0 || iHeight == 0 )
return;
if ( image != null ) {
m_rect.set( iX+m_iTranslateX, iY+m_iTranslateY, iX+image.getWidth()+m_iTranslateX, iY+image.getHeight()+m_iTranslateY );
if ( iWidth > 0 && iHeight > 0 )
m_rect.set( iX+m_iTranslateX, iY+m_iTranslateY, iX+iWidth+m_iTranslateX, iY+iHeight+m_iTranslateY );
m_paint.setAlpha( iAlpha );
m_g.save();
m_g.rotate( fpRotate, iX+(image.getWidth()>>1)+m_iTranslateX, iY+(image.getHeight()>>1)+m_iTranslateY );
m_g.drawBitmap( image, null, m_rect, m_paint );
m_paint.setAlpha( kMaxAlpha );
m_g.restore();
}
}
MyCanvas.java class
private int imageX = 0;
private int imageY = 0;
private int imageWidth = 0;
private int imageHeight = 0;
public boolean onTouchEvent(MotionEvent event) {
final int x = (int)event.getX();
final int y = (int)event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_UP:
imageX = x;
imageY = y;
break;
}
return true;
}
protected void onDraw(Canvas canvas) {
g.drawImage(image, 255, imageX, imageY, imageWidth, imageHeight, 0);
}