• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Move around image

sample code for zoom in and zoom out of images is:

private Drawable image;
image.setBounds(0, 0, getWidth(), getHeight());
image.draw(canvas);

zoom in and out is working fine.now I want to include move around routine for image in this code.So how can I move image on emulator using touch event?

Please give me information. thanks in advance.
 
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);
}
 
Back
Top Bottom