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
|
2017-02-21 08:42:07 +01:00
|
|
|
import android.support.annotation.UiThread
|
2017-02-20 16:13:48 +01:00
|
|
|
import android.view.View
|
|
|
|
|
|
|
|
private val tempLocation = IntArray(2)
|
|
|
|
private val tempRect = Rect()
|
|
|
|
|
2017-02-21 08:42:07 +01:00
|
|
|
@UiThread
|
2017-02-20 16:13:48 +01:00
|
|
|
fun View.getBounds(rect: RectF) {
|
|
|
|
rect.set(x, y, x + width, y + height)
|
|
|
|
}
|
|
|
|
|
2017-02-21 08:42:07 +01:00
|
|
|
@UiThread
|
2017-02-20 16:13:48 +01:00
|
|
|
fun View.getFrame(rect: Rect) {
|
|
|
|
rect.set(left, top, right, bottom)
|
|
|
|
}
|
|
|
|
|
2017-02-21 08:42:07 +01:00
|
|
|
@UiThread
|
|
|
|
fun View.getFrameRelatedTo(rect: Rect, other: View? = null) {
|
2017-02-20 16:13:48 +01:00
|
|
|
this.getFrame(rect)
|
2017-02-21 08:42:07 +01:00
|
|
|
if (other == null) {
|
|
|
|
offsetToRoot(this, rect)
|
|
|
|
} else if (other === this) {
|
|
|
|
rect.offsetTo(0, 0)
|
|
|
|
} else if (other !== parent) {
|
|
|
|
offsetToRoot(this, rect)
|
|
|
|
other.getFrame(tempRect)
|
|
|
|
offsetToRoot(other, tempRect)
|
2017-02-20 16:13:48 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|