Twidere-App-Android-Twitter.../twidere/src/main/kotlin/org/mariotaku/twidere/extension/ViewExtensions.kt

70 lines
2.0 KiB
Kotlin
Raw Normal View History

2017-02-20 16:13:48 +01:00
/*
* Twidere - Twitter client for Android
*
* Copyright (C) 2012-2017 Mariotaku Lee <mariotaku.lee@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.mariotaku.twidere.extension
import android.graphics.Rect
import android.graphics.RectF
import android.support.annotation.MainThread
import android.view.View
private val tempLocation = IntArray(2)
private val tempRect = Rect()
@MainThread
fun View.getBounds(rect: RectF) {
rect.set(x, y, x + width, y + height)
}
@MainThread
fun View.getFrame(rect: Rect) {
rect.set(left, top, right, bottom)
}
@MainThread
fun View.getFrameRelatedTo(rect: Rect, view: View? = null) {
if (view != null) {
view.getFrame(tempRect)
offsetToRoot(view, tempRect)
}
this.getFrame(rect)
offsetToRoot(this, rect)
if (view != null) {
rect.offset(-tempRect.left, -tempRect.top)
}
}
fun View.getLocationOnScreen(rect: Rect) {
getLocationOnScreen(tempLocation)
rect.set(tempLocation[0], tempLocation[1], tempLocation[0] + width, tempLocation[1] + height)
}
fun View.getLocationInWindow(rect: Rect) {
getLocationInWindow(tempLocation)
rect.set(tempLocation[0], tempLocation[1], tempLocation[0] + width, tempLocation[1] + height)
}
private fun offsetToRoot(view: View, rect: Rect) {
var parent = view.parent as? View
while (parent != null) {
rect.offset(parent.left, parent.top)
parent = parent.parent as? View
}
}