Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Rotate an image around its center

The following code should rotate an image around it's center. I haven't tried it on iPhone. But on Android, it rotates around the upper-left corner of the image instead. Anyone have an idea on how to make it rotate around the image's middle?

// Just past me into into a blank app.js file and run
window = Titanium.UI.createWindow({
    backgroundColor:'#47a',
    exitOnClose: true
});

//load test image from the appcelerator web site
var image = Titanium.UI.createImageView({
    image:'default_app_logo.png',
    backgroundColor:'transparent',
    canScale:false,
    anchorPoint:{x:0.5,y:0.5},
    height:'200',
    width:'200'
});


// Spin the image
var t = Ti.UI.create2DMatrix();
var spin = Titanium.UI.createAnimation();
t = t.rotate(30);
spin.transform = t;
spin.duration = 50;

window.add(image);
image.animate(spin);
window.open({fullscreen:true});
— asked November 29th 2010 by Tim Poulsen
  • android
  • image
  • rotate
0 Comments

3 Answers

  • I know it's Too late for answering this question but still i give it for make community strong..

    This is the code that works in both

    // Just past me into into a blank app.js file and run
    var win = Titanium.UI.createWindow({
        backgroundColor : 'white',
    
    });
    
    //load test image from the appcelerator web site
    var img = Titanium.UI.createImageView({
        image : 'wheel.2.png',
        backgroundColor : 'transparent',
        anchorPoint : {
            x : 0.5,
            y : 0.5
        },
        height : 100,
        width : 100
    });
    
    // Spin the image
    
    
    var matrix2d = Ti.UI.create2DMatrix();
    matrix2d = matrix2d.rotate(180); // in degrees
    // matrix2d = matrix2d.scale(1.5); // scale to 1.5 times original size
    var a = Ti.UI.createAnimation({
        transform: matrix2d,
        duration: 1000,
        autoreverse: true,
        repeat: 3
    });
    
    win.add(img);
    img.animate(a);
    win.open({
        fullscreen : true
    });
    
    — answered January 3rd 2013 by Sarafaraz Babi
    permalink
    2 Comments
    • Sarafaraz is correct on this. It should be:

      anchorPoint : {
          x : '0.5',
          y : '0.5'
      },
      

      — commented May 15th 2013 by Boost Media
    • Mine work with just 0.5. However could you make it work if imageview has a borderWidth and color set?

      — commented June 11th 2013 by archileus tee
  • anchorPoint:{x:0.5, y:0.5}
    

    I think the values here are measured in pixels. Try:

    anchorPoint:{x:100, y:100}
    
    — answered February 8th 2011 by Jonathan Chaffer
    permalink
    2 Comments
    • I was hopeful…but I'm afraid I get the same result. The image rotates around its upper-left corner. Thanks anyway!

      — commented February 8th 2011 by Tim Poulsen
    • For those who still want to know how to do it you should use this to rotate around the image center
      anchorPoint:{x:'50%',y:'50%'}

      — commented October 14th 2012 by Jerome Ueberschlag
  • this a question i have seen on other forum and i fianlly get the codes to rotate images vb.net around the center. thank you .

    — answered July 19th 2013 by sdfew fdsfd
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.