日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

【Android开发】毛玻璃效果

發布時間:2024/4/17 Android 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Android开发】毛玻璃效果 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

使用一:靜態控件上使用

  • 先附上自定義view-BlurringView
  • public class BlurringView extends View {private int mDownsampleFactor;private int mOverlayColor;private View mBlurredView;private int mBlurredViewWidth, mBlurredViewHeight;private boolean mDownsampleFactorChanged;private Bitmap mBitmapToBlur, mBlurredBitmap;private Canvas mBlurringCanvas;private RenderScript mRenderScript;private ScriptIntrinsicBlur mBlurScript;private Allocation mBlurInput, mBlurOutput;public BlurringView(Context context) {this(context, null);}public BlurringView(Context context, AttributeSet attrs) {super(context, attrs);final Resources res = getResources();final int defaultBlurRadius = res.getInteger(R.integer.default_blur_radius);final int defaultDownsampleFactor = res.getInteger(R.integer.default_downsample_factor);final int defaultOverlayColor = res.getColor(R.color.default_overlay_color);initializeRenderScript(context);TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PxBlurringView);setBlurRadius(a.getInt(R.styleable.PxBlurringView_blurRadius, defaultBlurRadius));setDownsampleFactor(a.getInt(R.styleable.PxBlurringView_downsampleFactor,defaultDownsampleFactor));setOverlayColor(a.getColor(R.styleable.PxBlurringView_overlayColor, defaultOverlayColor));a.recycle();}public void setBlurredView(View blurredView) {mBlurredView = blurredView;}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);if (mBlurredView != null) {if (prepare()) {// If the background of the blurred view is a color drawable, we use it to clear// the blurring canvas, which ensures that edges of the child views are blurred// as well; otherwise we clear the blurring canvas with a transparent color.if (mBlurredView.getBackground() != null && mBlurredView.getBackground() instanceof ColorDrawable) {mBitmapToBlur.eraseColor(((ColorDrawable) mBlurredView.getBackground()).getColor());} else {mBitmapToBlur.eraseColor(Color.TRANSPARENT);}mBlurredView.draw(mBlurringCanvas);blur();canvas.save();canvas.translate(mBlurredView.getX() - getX(), mBlurredView.getY() - getY());canvas.scale(mDownsampleFactor, mDownsampleFactor);canvas.drawBitmap(mBlurredBitmap, 0, 0, null);canvas.restore();}canvas.drawColor(mOverlayColor);}}public void setBlurRadius(int radius) {mBlurScript.setRadius(radius);}public void setDownsampleFactor(int factor) {if (factor <= 0) {throw new IllegalArgumentException("Downsample factor must be greater than 0.");}if (mDownsampleFactor != factor) {mDownsampleFactor = factor;mDownsampleFactorChanged = true;}}public void setOverlayColor(int color) {mOverlayColor = color;}private void initializeRenderScript(Context context) {mRenderScript = RenderScript.create(context);mBlurScript = ScriptIntrinsicBlur.create(mRenderScript, Element.U8_4(mRenderScript));}protected boolean prepare() {final int width = mBlurredView.getWidth();final int height = mBlurredView.getHeight();if (mBlurringCanvas == null || mDownsampleFactorChanged|| mBlurredViewWidth != width || mBlurredViewHeight != height) {mDownsampleFactorChanged = false;mBlurredViewWidth = width;mBlurredViewHeight = height;int scaledWidth = width / mDownsampleFactor;int scaledHeight = height / mDownsampleFactor;// The following manipulation is to avoid some RenderScript artifacts at the edge.scaledWidth = scaledWidth - scaledWidth % 4 + 4;scaledHeight = scaledHeight - scaledHeight % 4 + 4;if (mBlurredBitmap == null|| mBlurredBitmap.getWidth() != scaledWidth|| mBlurredBitmap.getHeight() != scaledHeight) {mBitmapToBlur = Bitmap.createBitmap(scaledWidth, scaledHeight,Bitmap.Config.ARGB_8888);if (mBitmapToBlur == null) {return false;}mBlurredBitmap = Bitmap.createBitmap(scaledWidth, scaledHeight,Bitmap.Config.ARGB_8888);if (mBlurredBitmap == null) {return false;}}mBlurringCanvas = new Canvas(mBitmapToBlur);mBlurringCanvas.scale(1f / mDownsampleFactor, 1f / mDownsampleFactor);mBlurInput = Allocation.createFromBitmap(mRenderScript, mBitmapToBlur,Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);mBlurOutput = Allocation.createTyped(mRenderScript, mBlurInput.getType());}return true;}protected void blur() {mBlurInput.copyFrom(mBitmapToBlur);mBlurScript.setInput(mBlurInput);mBlurScript.forEach(mBlurOutput);mBlurOutput.copyTo(mBlurredBitmap);}@Overrideprotected void onDetachedFromWindow() {super.onDetachedFromWindow();if (mRenderScript != null) {mRenderScript.destroy();}} }
  • 調用自定義view
  • <?xml version="1.0" encoding="utf-8"?> <FrameLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"xmlns:app="http://schemas.android.com/apk/res-auto"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#FFFFFFFF"tools:context=".SecondActivity"><ImageView android:id="@+id/iv_blur"android:src="@mipmap/image5"android:layout_width="match_parent"android:layout_height="match_parent"android:scaleType="fitXY"/><com.example.maoboli.maobolidemo.BlurringViewandroid:id="@+id/blurring_view"android:layout_width="260dp"android:layout_height="200dp"android:layout_gravity="center"android:layout_marginBottom="80dip"app:blurRadius="20"app:downsampleFactor="6"app:overlayColor="#26FFFFFF"/><Buttonandroid:id="@+id/shuffle_button"android:layout_width="100dp"android:layout_height="wrap_content"android:layout_gravity="center"android:layout_marginBottom="120dp"android:text="模糊頭像"/><Buttonandroid:id="@+id/suibian"android:layout_width="100dp"android:layout_height="wrap_content"android:layout_below="@id/shuffle_button"android:layout_gravity="center"android:layout_marginTop="30dp"android:layout_marginBottom="80dip"android:text="隨便點"/></FrameLayout>
  • 編輯activity
  • BlurringView mBlurringView = (BlurringView) findViewById(R.id.blurring_view);View blurredView = findViewById(R.id.iv_blur);// Give the blurring view a reference to the blurred view.mBlurringView.setBlurredView(blurredView);

    使用二:模糊頭像效果

  • 布局文件
  • <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"><ImageView android:id="@+id/iv_blur"android:layout_width="match_parent"android:layout_height="200dp" /><ImageView android:id="@+id/iv_avatar"android:layout_width="60dp"android:layout_height="60dp"android:scaleType="fitCenter"android:layout_centerInParent="true"/></RelativeLayout></LinearLayout>
  • 編輯activity
  • public class MainActivity extends AppCompatActivity {private ImageView blurImageView;private ImageView avatarImageView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);findViews();initData();}private void findViews(){blurImageView = (ImageView) findViewById(R.id.iv_blur);avatarImageView = (ImageView) findViewById(R.id.iv_avatar);}private void initData(){Glide.with(this).load(R.mipmap.image).bitmapTransform(new BlurTransformation(this, 30), new CenterCrop(this)).into(blurImageView);Glide.with(this).load(R.mipmap.image).bitmapTransform(new CropCircleTransformation(this)).into(avatarImageView);} }
  • 導包
  • compile 'com.github.bumptech.glide:glide:3.7.0'compile 'jp.wasabeef:glide-transformations:2.0.1'

    使用三:彈出模糊對話框

    具體代碼參考git上的 Demo

    轉載于:https://www.cnblogs.com/neo-java/p/10185076.html

    總結

    以上是生活随笔為你收集整理的【Android开发】毛玻璃效果的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。