Move around image


Last Updated:

  1. nayan.kapadia

    nayan.kapadia New Member This Topic's Starter

    Joined:
    Feb 10, 2010
    Messages:
    1
    Likes Received:
    0
    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

    Joined:
    Sep 11, 2009
    Messages:
    6,062
    Likes Received:
    38
    Moving to developers section as you may get a better response :)
     
  3. jon21569

    jon21569 Member

    Joined:
    Aug 10, 2008
    Messages:
    23
    Likes Received:
    7
    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...