我想模糊从原始文件夹播放视频的videoview。
使用渲染脚本或类似Blurry的库可以模糊化imageview或包含可绘制背景的布局。但是如何模糊视频呢?我尝试了将视频的uri路径转换为位图,将视频转换为缩略图的方法,但是没有用。
这是我的代码。
package com.example.myapplication
import android.graphics.BitmapFactory
import android.graphics.drawable.BitmapDrawable
import android.net.Uri
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.widget.MediaController
import android.widget.VideoView
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val videoView = findViewById(R.id.videoView) as VideoView
val mediaController = MediaController(this)
mediaController.setAnchorView(videoView)
val uri = Uri.parse("android.resource://" + packageName + "/" + R.raw.samplevideo)
videoView.setMediaController(mediaController)
videoView.setVideoURI(uri)
videoView.requestFocus()
videoView.start()
var runnable = Runnable {
var resultBmp = BlurBuilder.blur(this, BitmapFactory.decodeResource(resources, R.raw.samplevideo))
var drawable = BitmapDrawable(resources, resultBmp)
videoView.background = drawable
}
videoView.post(runnable)
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/relativeLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<VideoView
android:id="@+id/videoView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
BlurBuilder.kt
package com.example.myapplication;
import android.content.Context;
import android.graphics.Bitmap;
import android.renderscript.Allocation;
import android.renderscript.Element;
import android.renderscript.RenderScript;
import android.renderscript.ScriptIntrinsicBlur;
object BlurBuilder {
private val BITMAP_SCALE = 0.6f
private val BLUR_RADIUS = 15f
fun blur(context: Context, image: Bitmap): Bitmap {
val width = Math.round(image.width * BITMAP_SCALE)
val height = Math.round(image.height * BITMAP_SCALE)
val inputBitmap = Bitmap.createScaledBitmap(image, width, height, false)
val outputBitmap = Bitmap.createBitmap(inputBitmap)
val rs = RenderScript.create(context)
val intrinsicBlur = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs))
val tmpIn = Allocation.createFromBitmap(rs, inputBitmap)
val tmpOut = Allocation.createFromBitmap(rs, outputBitmap)
intrinsicBlur.setRadius(BLUR_RADIUS)
intrinsicBlur.setInput(tmpIn)
intrinsicBlur.forEach(tmpOut)
tmpOut.copyTo(outputBitmap)
return outputBitmap
}
}
请帮助我模糊视频视图或包含视频视图的相对布局。