How to rotate view by coordinate also in Android

2019-09-05 07:30发布

问题:

first understand my question properly before voting,

i have view that's like ,

  <View
                    android:id="@+id/View01"
                    android:layout_width="280dp"
                    android:layout_height="150dp"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentTop="true"
                    android:layout_marginLeft="50dp"
                    android:layout_marginTop="83dp"
                    android:background="#212121"
                    android:rotation="-5"
                    android:visibility="visible" />

and output is,

you can see that when i use android:rotation="-5", it rotate views(black box) but their coordinates remains same (blue rectangle).

i want to use new Rect after rotation but i fails to do so, i read about Rect.mapPoint but still fails to achieve goal, any help for this?

EDIT :

final xml looks like,

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    xmlns:app="http://schemas.android.com/apk/res/com.android.game.robbrygame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/vScrollview"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:scrollbars="none" >

        <HorizontalScrollView
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/hScrollview"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:scrollbars="none" >

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <View
                    android:id="@+id/View01"
                    android:layout_width="280dp"
                    android:layout_height="150dp"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentTop="true"
                    android:layout_marginLeft="50dp"
                    android:layout_marginTop="83dp"
                    android:background="#212121"
                    android:rotation="-5"
                    android:visibility="visible" />

            </RelativeLayout>
        </HorizontalScrollView>
    </ScrollView>

</RelativeLayout>

and i have so many view like View01 and so on.... !!!

回答1:

you can use getLocalVisibleRect()

View view01 = findViewById(R.id.View01);

view01.post(new Runnable() {
            @Override
            public void run() {
                view01.getLocalVisibleRect(rectf);
                Log.d("WIDTH        :", String.valueOf(rectf.width()));
                Log.d("HEIGHT       :", String.valueOf(rectf.height()));
                Log.d("left         :", String.valueOf(rectf.left));
                Log.d("right        :", String.valueOf(rectf.right));
                Log.d("top          :", String.valueOf(rectf.top));
                Log.d("bottom       :", String.valueOf(rectf.bottom));
   }
 });

you need to use .post() to be sur the layout is set