android spinning image

Posted by user270811 on Stack Overflow See other posts from Stack Overflow or by user270811
Published on 2010-04-14T00:46:25Z Indexed on 2010/04/14 0:53 UTC
Read the original article Hit count: 639

Filed under:
|
|

hi,

i am trying to create two spinning wheels, as in pulleys, so everytime the attached rope moves, the two pulleys will rotate. i have tried two approaches:

1) use Matrix.postRotate within the onDraw() method of the View class, which calls the following:

private void drawSpinningWheel(Canvas canvas)
{
    try
    {
        canvas.save();

            Bitmap bitmapOrg = null;

        int iDrawable = R.drawable.spinning_button;

        bitmapOrg = BitmapFactory.decodeResource(getResources(), iDrawable);

        if(bitmapOrg != null)
        {
            int width = bitmapOrg.getWidth(); 
            int height = bitmapOrg.getHeight(); 
            int newWidth = 24; 
            int newHeight = 24; 

            // calculate the scale - in this case = 0.4f 
            float scaleWidth = ((float) newWidth) / width; 
            float scaleHeight = ((float) newHeight) / height; 

            // createa matrix for the manipulation 
            Matrix matrix = new Matrix(); 
            // resize the bit map 
            matrix.postScale(scaleWidth, scaleHeight); 
            matrix.postRotate((float) mDegrees++);

            Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, 
                    width, height, matrix, true); 

            canvas.drawBitmap(resizedBitmap, matrix, null);

        }

        canvas.restore();
    }
    catch(Exception e)
    {
        Log.e(TAG + "drawSpinningWheel", e.getMessage());
    }

}

but it seems like the image not only spins but rotates around another axis

2) use SurfaceView and a separate thread, in the run() call this:

    private void doDraw(Canvas canvas) {
        // Draw the background image. Operations on the Canvas accumulate
        // so this is like clearing the screen.
        canvas.drawBitmap(mBackgroundImage, 0, 0, null);

        int yTop = mCanvasHeight - ((int) mY + mSpinningWheelImageHeight / 2);
        int xLeft = (int) mX - mSpinningWheelImageWidth / 2;

...

        // Draw the ship with its current rotation
        canvas.save();
        canvas.rotate((float) mHeading++, (float) mX, mCanvasHeight
                - (float) mY);

            mSpinningWheelImage.setBounds(xLeft, yTop, xLeft + mSpinningWheelImageWidth, yTop
                    + mSpinningWheelImageHeight);
            mSpinningWheelImage.draw(canvas);


        canvas.restore();
    }

i get the spinning to work but i can't add another spinning wheel. i even tried to create another thread for the second spinning wheel, only one shows up. can someone point me in the right direction? thanks.

© Stack Overflow or respective owner

Related posts about android

Related posts about spinning