Skip to content

Instantly share code, notes, and snippets.

@Kenny50
Created July 2, 2023 05:46
Show Gist options
  • Save Kenny50/5e640379f56b09c64c9213be2320360c to your computer and use it in GitHub Desktop.
Save Kenny50/5e640379f56b09c64c9213be2320360c to your computer and use it in GitHub Desktop.
Update webview rotate with android animate
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
@RequiresApi(Build.VERSION_CODES.R)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
updateFullScreenSize()
}
@RequiresApi(Build.VERSION_CODES.R)
fun updateFullScreenSize(){
val bounds = windowManager.currentWindowMetrics.bounds
globalHeight = bounds.right
globalWidth = bounds.bottom
}
}
var globalHeight = 0
private set
var globalWidth = 0
private set
package com.kenny.myapplication.custom
import android.annotation.SuppressLint
import android.content.Context
import android.os.Build
import android.util.AttributeSet
import android.view.ViewGroup
import android.webkit.WebView
import androidx.annotation.RequiresApi
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.constraintlayout.widget.ConstraintSet
import androidx.core.view.updateLayoutParams
import com.kenny.myapplication.R
import com.kenny.myapplication.globalHeight
import com.kenny.myapplication.globalWidth
import java.lang.StringBuilder
import java.util.*
//youtube撥放器
@SuppressLint("SetJavaScriptEnabled")
class YtPlayer @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defaultStyle: Int = 0
) : WebView(context, attrs, defaultStyle) {
init {
this.settings.apply {
javaScriptEnabled = true
useWideViewPort = true
loadWithOverviewMode = true
mediaPlaybackRequiresUserGesture = false
}
}
private fun getRawFileFromResource(resourceId: Int): String {
val sb = StringBuilder()
val s = Scanner(resources.openRawResource(resourceId))
while (s.hasNextLine()) {
sb.append(s.nextLine().toString() + "\n")
}
return sb.toString()
}
fun loadYTId(id: String) {
(this as WebView).loadData(
getRawFileFromResource(R.raw.youtube_iframe_html)
.replace(
"replaceMe",
"https://www.youtube.com/embed/$id?autoplay=1&fs=0&rel=0&showinfo=0&modestbranding=1&iv_load_policy=3"
),
"text/html",
"UTF-8"
)
}
fun clearAllResource() {
(this as? WebView)?.apply {
loadUrl("about:blank")
clearHistory()
clearView()
destroy()
}
}
private fun rotateScreen(degree:Float) {
val viewHeightAfterRotate = globalHeight
val viewWidthAfterRotate = globalWidth
updateLayoutParams {
this.width = viewWidthAfterRotate
this.height = viewHeightAfterRotate
translationX = (height - width) / 2f
translationY = -(height - width) / 2f
}
rotation = degree
}
fun toRightLandscape(){
rotateScreen(90f)
}
fun toLeftLandscape(){
rotateScreen(-90f)
}
fun toPortrait(){
updateLayoutParams {
this.width = globalHeight
this.height = globalHeight*9/16
x = 0f
y = 0f
}
rotation = 0f
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment