這篇文章給大家介紹使用RenderScript怎么實(shí)現(xiàn)高斯模糊效果,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
成都創(chuàng)新互聯(lián)公司專注于湘東企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站建設(shè),商城網(wǎng)站開發(fā)。湘東網(wǎng)站建設(shè)公司,為湘東等地區(qū)提供建站服務(wù)。全流程按需設(shè)計(jì),專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,成都創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務(wù)
RenderScript實(shí)現(xiàn)
首先在app目錄下build.gradle文件中添加如下代碼:
defaultConfig { applicationId "io.github.marktony.gaussianblur" minSdkVersion 19 targetSdkVersion 25 versionCode 1 versionName "1.0" renderscriptTargetApi 19 renderscriptSupportModeEnabled true }
RenderScriptIntrinsics提供了一些可以幫助我們快速實(shí)現(xiàn)各種圖片處理的操作類,例如,ScriptIntrinsicBlur,可以簡單高效實(shí)現(xiàn) 高斯模糊效果。
package io.github.marktony.gaussianblur; import android.content.Context; import android.graphics.Bitmap; import android.support.annotation.IntRange; import android.support.annotation.NonNull; import android.support.v8.renderscript.Allocation; import android.support.v8.renderscript.Element; import android.support.v8.renderscript.RenderScript; import android.support.v8.renderscript.ScriptIntrinsicBlur; public class RenderScriptGaussianBlur { private RenderScript renderScript; public RenderScriptGaussianBlur(@NonNull Context context) { this.renderScript = RenderScript.create(context); } public Bitmap gaussianBlur(@IntRange(from = 1, to = 25) int radius, Bitmap original) { Allocation input = Allocation.createFromBitmap(renderScript, original); Allocation output = Allocation.createTyped(renderScript, input.getType()); ScriptIntrinsicBlur scriptIntrinsicBlur = ScriptIntrinsicBlur.create(renderScript, Element.U8_4(renderScript)); scriptIntrinsicBlur.setRadius(radius); scriptIntrinsicBlur.setInput(input); scriptIntrinsicBlur.forEach(output); output.copyTo(original); return original; } }
然后就可以直接使用RenderScriptGaussianBlur,愉快地根據(jù)SeekBar的值,實(shí)現(xiàn)不同程度的模糊了。
package io.github.marktony.gaussianblur; import android.content.DialogInterface; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.Window; import android.view.WindowManager; import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.SeekBar; import android.widget.TextView; public class MainActivity extends AppCompatActivity { private ImageView imageView; private ImageView container; private LinearLayout layout; private TextView textViewProgress; private RenderScriptGaussianBlur blur; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageView = (ImageView) findViewById(R.id.imageView); container = (ImageView) findViewById(R.id.container); container.setVisibility(View.GONE); layout = (LinearLayout) findViewById(R.id.layout); layout.setVisibility(View.VISIBLE); SeekBar seekBar = (SeekBar) findViewById(R.id.seekBar); textViewProgress = (TextView) findViewById(R.id.textViewProgress); TextView textViewDialog = (TextView) findViewById(R.id.textViewDialog); blur = new RenderScriptGaussianBlur(MainActivity.this); seekBar.setMax(25); seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { textViewProgress.setText(String.valueOf(progress)); } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { int radius = seekBar.getProgress(); if (radius < 1) { radius = 1; } Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image); imageView.setImageBitmap(blur.gaussianBlur(radius, bitmap)); } }); textViewDialog.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { container.setVisibility(View.VISIBLE); layout.setDrawingCacheEnabled(true); layout.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_LOW); Bitmap bitmap = layout.getDrawingCache(); container.setImageBitmap(blur.gaussianBlur(25, bitmap)); layout.setVisibility(View.INVISIBLE); AlertDialog dialog = new AlertDialog.Builder(MainActivity.this).create(); dialog.setTitle("Title"); dialog.setMessage("Message"); dialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); dialog.setOnCancelListener(new DialogInterface.OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { } }); dialog.setOnCancelListener(new DialogInterface.OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { container.setVisibility(View.GONE); layout.setVisibility(View.VISIBLE); } }); dialog.show(); } }); } }
關(guān)于使用RenderScript怎么實(shí)現(xiàn)高斯模糊效果就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。