Martynas
Martynas

Reputation: 627

rotating custom image around its center

I have an png image width 200 x height 50 in pixel. I need to rotate it around its center by an angle.

enter image description here

My onDraw method

@Override
    public void onDraw(Canvas canvas){
        initDrawingTools();
        drawRect(canvas);
        drawBack(canvas);
        Matrix mat = new Matrix();
        Bitmap bMap = BitmapFactory.decodeResource(getResources(),R.drawable.needle);
        cX = getWidth()/2-bMap.getWidth()/2;
        cY = getHeight()/2-bMap.getHeight()/2;
        mat.setTranslate(cX, cY);
        mat.postRotate(angleSpeed,cX, cY);      
        Bitmap bMapRotate = Bitmap.createBitmap(bMap, 0, 0,bMap.getWidth(),bMap.getHeight(), mat, true);
        canvas.drawBitmap(bMapRotate, mat, null);
    }

This is the closest i managed. As i understand the center of image is floating while it is rotating. For example: 0 degree - 200x50, 90 degree 50x200 etc. And in this case its not rotating around its center. Could someone give me some hints or explain how to get the result?

EDIT working Mikel Pascualc suggestion:

How to make arrow remain in angled position after animation???

seekBar = (SeekBar)findViewById(R.id.seekBar1);
        seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
               ROTATE_TO = speed;
               spin();}
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                ROTATE_FROM = speed;}
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                speed = progress;
//            textView = (TextView)findViewById(R.id.textView1);
//            textView.setText(progress);
            //rodykle.onSpeedChanged((float)progress);
            }
        });
    }
    public void spin(){
        TextView textView1 = (TextView)findViewById(R.id.textView1);
        textView1.setText("From: " + ROTATE_FROM + "\nTo: " + ROTATE_TO + "\nProgress: " + speed);
        ImageView needle = (ImageView) findViewById(R.id.needle1);
        RotateAnimation r = new RotateAnimation(ROTATE_FROM, ROTATE_TO, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        r.setDuration((long) 2*1000);
        r.setRepeatCount(0);
        r.setFillAfter(true); // <-- ADDED THIS TO STAY IMG IN ANGLE AFTER ANIMATION
        needle.startAnimation(r);
    }

Upvotes: 3

Views: 900

Answers (2)

Mikel Pascual
Mikel Pascual

Reputation: 2211

You better use an animation. Example:

public class NeedleRotateActivity extends Activity {
private static final float ROTATE_FROM = 0.0f;
private static final float ROTATE_TO = -10.0f * 360.0f;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ImageView needle = (ImageView) findViewById(R.id.needle);

    RotateAnimation r = new RotateAnimation(ROTATE_FROM, ROTATE_TO, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    r.setDuration((long) 2*1500);
    r.setRepeatCount(0);
    r.setFillAfter(true);
    needle.startAnimation(r);
}

Upvotes: 3

Shirish Herwade
Shirish Herwade

Reputation: 11711

I have done a similar thing to this. I rotated a bottle round itself to slowly reduce the speed until it stops. Sharing a piece of code from that, hope it help you.

rotateAnimation = new RotateAnimation(0, 360,
                    Animation.RELATIVE_TO_SELF, 0.5f,
                    Animation.RELATIVE_TO_SELF, 0.5f);
            rotateAnimation.setInterpolator(new LinearInterpolator());
            rotateAnimation.setDuration(duration);
            rotateAnimation.setRepeatCount(0);
            imgBottle.startAnimation(rotateAnimation);

            rotateAnimation.setAnimationListener(new AnimationListener() {
                public void onAnimationStart(Animation anim) {
                };

                public void onAnimationRepeat(Animation anim) {
                };

                public void onAnimationEnd(Animation anim) {
                    duration = duration + 70;
                };
            });

imgBottle.startAnimation(rotateAnimation);

Upvotes: 0

Related Questions