1. Check out our app, Forums for Android! Download and leave feedback here!

Move around image

Discussion in 'Application Development' started by nayan.kapadia, Feb 22, 2010.

  1. nayan.kapadia

    nayan.kapadia New Member
    Thread Starter
    5

    Feb 10, 2010
    1
    0
    5
    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.
     

    Advertisement

  2. Carl C

    Carl C Well-Known Member
    138

    Sep 11, 2009
    6,062
    38
    138
    Student
    London
    Moving to developers section as you may get a better response :)
     
  3. jon21569

    jon21569 Member
    16

    Aug 10, 2008
    23
    7
    16
    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);
    }
     

Share This Page

Loading...