1
+ /*
2
+ * Copyright 2020 LiteKite Startup. All rights reserved.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ package com.litekite.widget
18
+
19
+ import android.content.Context
20
+ import android.util.AttributeSet
21
+ import android.view.ViewGroup
22
+
23
+ /* *
24
+ * This ViewGroup contains a single view, which will be rotated by 90 degrees counterclockwise.
25
+ *
26
+ * <p>This implementation was referenced from Android Open Source Project's MusicFX Android App:
27
+ * aosp_root/packages/apps/MusicFX/src/com/android/musicfx/SeekBarRotator.java</p>
28
+ *
29
+ * @author Vignesh S
30
+ * @version 1.0, 03/11/2020
31
+ * @since 1.0
32
+ */
33
+ @Suppress(" UNUSED" )
34
+ class ViewRotator @JvmOverloads constructor(
35
+ context : Context ,
36
+ attrs : AttributeSet ? = null ,
37
+ defStyleAttr : Int = 0
38
+ ) : ViewGroup(context, attrs, defStyleAttr) {
39
+
40
+ override fun onMeasure (widthMeasureSpec : Int , heightMeasureSpec : Int ) {
41
+ val childView = getChildAt(0 )
42
+ if (childView != null && childView.visibility != GONE ) {
43
+ // swap width and height for child
44
+ measureChild(childView, heightMeasureSpec, widthMeasureSpec)
45
+ setMeasuredDimension(childView.measuredHeightAndState, childView.measuredWidthAndState)
46
+ } else {
47
+ setMeasuredDimension(
48
+ resolveSizeAndState(0 , widthMeasureSpec, 0 ),
49
+ resolveSizeAndState(0 , heightMeasureSpec, 0 )
50
+ )
51
+ }
52
+ }
53
+
54
+ override fun onLayout (changed : Boolean , l : Int , t : Int , r : Int , b : Int ) {
55
+ val childView = getChildAt(0 )
56
+ if (childView != null && childView.visibility != GONE ) {
57
+ // rotate the child 90 degrees counterclockwise around its upper-left
58
+ childView.pivotX = 0f
59
+ childView.pivotY = 0f
60
+ childView.rotation = - 90f
61
+ // place the child below this view, so it rotates into view
62
+ val width = r - l
63
+ val height = b - t
64
+ childView.layout(0 , height, height, width + height)
65
+ }
66
+ }
67
+
68
+ }
0 commit comments