Skip to content Skip to sidebar Skip to footer

Need A Rotatable Circle For Android Application. Customized Object Or Widget?

I need to create a circle that rotates and contains data for my application. Should I create a customized object for my application or should I make a in-application widget? While

Solution 1:

This is a rotatable LinearLayout that you can put everything in it and you can rotate it by degree if you customize it. use rotate() method to rotate it and...

enjoy! ;)

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.LinearLayout;

publicclassRotateLinearLayoutextendsLinearLayout {

    privateMatrixmForward=newMatrix();
    privateMatrixmReverse=newMatrix();
    privatefloat[] mTemp = newfloat[2];
    privatefloatdegree=0;

    publicRotateLinearLayout(Context context) {
        super(context);
    }

    publicRotateLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @OverrideprotectedvoiddispatchDraw(Canvas canvas) {

        try {
            if (degree == 0) {
                super.dispatchDraw(canvas);
                return;
            }
            canvas.rotate(degree, getWidth() / 2, getHeight() / 2);

            mForward = canvas.getMatrix();
            mForward.invert(mReverse);
            canvas.save();
            canvas.setMatrix(mForward); // This is the matrix we need to use for// proper positioning of touch eventssuper.dispatchDraw(canvas);
            canvas.restore();
            invalidate();
        } catch (Exception e) {

        }

    }

    @OverridepublicbooleandispatchTouchEvent(MotionEvent event) {
        if (degree == 0) {
            returnsuper.dispatchTouchEvent(event);
        }
        // final float[] temp = mTemp;// temp[0] = event.getX();// temp[1] = event.getY();// mReverse.mapPoints(temp);// event.setLocation(temp[0], temp[1]);
        event.setLocation(getWidth() - event.getX(), getHeight() - event.getY());
        returnsuper.dispatchTouchEvent(event);
    }

    publicvoidrotate() {
        if (degree == 0) {
            degree = 180;
        } else {
            degree = 0;
        }
    }

}

Update:

add this code to your xml layout and put your Views like ImageView or another LinearLayout in it :

<org.mabna.order.ui.RotateLinearLayout  android:id="@+id/llParent"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center"
    android:gravity="center"
    android:orientation="horizontal" >



<ImageView
                        android:id="@+id/myImage"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_margin="5dip"
                        android:src="@drawable/main01" />

</org.mabna.order.ui.RotateLinearLayout>

in onCreate() method:

llParent = (RotateLinearLayout) this.findViewById(R.id.llParent);

in onClickListener of a button:

protectedvoidbtnRotate_onClick() {
        llParent.rotate();
    }

Update2:

You can use an animation for rotation before real rotation (llParent.rotate();). it needs an animation layout like rotate_dialog.xml:

<?xml version="1.0" encoding="utf-8"?><rotatexmlns:android="http://schemas.android.com/apk/res/android"android:duration="1000"android:fromDegrees="-180"android:toDegrees="0"android:pivotX="50%"android:pivotY="50%"android:fillAfter="true" />

and in your code:

protectedvoidbtnRotate_onClick() {
        // rotate 
        Animation rotateAnimation = AnimationUtils.loadAnimation(this,
                R.anim.rotate_dialog);
        llParent.startAnimation(rotateAnimation);
        llParent.rotate();
    }

Solution 2:

There is a fairly easy way to make a rotating animation from a custom widget derived from the View class. After the view is created and placed in your layout, you can call View.setAnimation(Animation) or View.startAnimation(Animation), supplying a RotateAnimation on the view to start it. Here is an example of a rotation animation defined in xml, that can be loaded from your activity with getResources().getAnimation(int).

<?xml version="1.0" encoding="utf-8"?><rotatexmlns:android="http://schemas.android.com/apk/res/android"android:fromDegrees="float"android:toDegrees="float"android:pivotX="float"android:pivotY="float" />

Post a Comment for "Need A Rotatable Circle For Android Application. Customized Object Or Widget?"