目前在我正在开发的Android应用程序中,我正在循环遍历图像的像素以使其模糊.这在640x480图像上大约需要30秒.
在Android Market中浏览应用程序时,我遇到了一个包含模糊功能的应用程序,并且它们的模糊非常快(例如5秒),因此它们必须使用不同的模糊方法.
除了循环像素之外,任何人都知道更快的方法吗?
我有一个简短的问题:
假设我有一个(可变的)位图,我需要修改(添加图像,文本等...).
我没有考虑使用许多特殊的类来绘制画布(画布,画布,矩阵等),而是考虑为什么不使用Android的内置类来完成这项任务,只有当我需要真正定制的操作时,我仍然可以使用画布?
因此,例如,为了在位图上显示任何类型的视图(当然没有父视图),我可以调用下一个函数:
public void drawViewToBitmap(Bitmap b, View v, Rect rect) {
Canvas c = new Canvas(b);
// <= use rect to let the view to draw only into this boundary inside the bitmap
view.draw(c);
}
Run Code Online (Sandbox Code Playgroud)
这样的事情可能吗?也许这就是它在幕后工作的方式?
我应该在绘图和画布创作之间的部分写什么?
编辑:我已经尝试了下一个代码,但它没有工作:
public void drawFromViewToCanvas(final View view, final Rect rect, final Canvas canvas) {
final int widthSpec = View.MeasureSpec.makeMeasureSpec(rect.width(), View.MeasureSpec.EXACTLY);
final int heightSpec = View.MeasureSpec.makeMeasureSpec(rect.height(), View.MeasureSpec.EXACTLY);
view.measure(widthSpec, heightSpec);
// Lay the view out with the known dimensions
view.layout(0, 0, rect.width(), rect.height());
// …Run Code Online (Sandbox Code Playgroud) 到目前为止我尝试过的:
将每个帧转换为位图,使用库将其模糊并将其放入ImageView相机预览前面.显然太慢了 - 比如1 fps.
然后我开始使用RenderScript,它会模糊每一帧,处理结果应放在TextureView封面相机预览中.
该方法的基本代码和平:
使用BlurFilter
ScriptIntrinsicBlur.create(rs, Element.RGBA_8888(rs)).apply {
setRadius(BLUR_RADIUS)
}
private val yuvToRgb = ScriptIntrinsicYuvToRGB.create(rs, Element.RGBA_8888(rs))
private var surface: SurfaceTexture? = null
private fun setupSurface() {
if (surface != null) {
aBlurOut?.surface = Surface(surface)
}
}
fun reset(width: Int, height: Int) {
aBlurOut?.destroy()
this.width = width
this.height = height
val tbConvIn = Type.Builder(rs, Element.U8(rs))
.setX(width)
.setY(height)
.setYuvFormat(android.graphics.ImageFormat.NV21)
aConvIn = Allocation.createTyped(rs, tbConvIn.create(), Allocation.USAGE_SCRIPT)
val tbConvOut = Type.Builder(rs, Element.RGBA_8888(rs)) …Run Code Online (Sandbox Code Playgroud) 我试图让图像的底部部分为顶部的视图,就像在图像中一样.
我尝试使用Rendenscript模糊它,但我无法模糊视图背后的部分.:(
我见过很多库,但几乎所有库都模糊了整个图像,但不是它的一部分.
另外,一个重要的部分是我在ViewPager中使用它,因此在IOS中需要像这样快速和动态的东西,它在它背后的图像改变时重绘自己.
任何帮助表示赞赏.谢谢你的到来!
我的xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="@+id/image"
android:src="@drawable/broadstairs"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:layout_centerInParent="true"/>
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="200dp"
android:text="Hello World"
android:gravity="center"
android:layout_alignParentBottom="true"
android:textColor="@android:color/white"
android:textStyle="bold"
android:textSize="36sp"/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
我的代码:
BlurBuilder.java
public class BlurBuilder {
private static final float BITMAP_SCALE = 0.1f;
private static final float BLUR_RADIUS = 7.5f;
public static Bitmap blur(Context context, Bitmap image) {
int width = Math.round(image.getWidth() * BITMAP_SCALE);
int height = Math.round(image.getHeight() * …Run Code Online (Sandbox Code Playgroud)