天天看點

Android開發之高斯模糊效果三行代碼搞定附帶CSDN源碼請導入module

老套路:

我們先來看下效果圖:

Android開發之高斯模糊效果三行代碼搞定附帶CSDN源碼請導入module
Android開發之高斯模糊效果三行代碼搞定附帶CSDN源碼請導入module
Android開發之高斯模糊效果三行代碼搞定附帶CSDN源碼請導入module

效果怎麼樣,不錯吧?

我們隻需要用到兩個庫就可以輕松實作:

Glide和glide-transformations這兩個庫

我們直接上代碼:

package phonemsg.yhsh.cn.gaosimohu;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

import jp.wasabeef.glide.transformations.BlurTransformation;

/**
 * @author DELL
 */
public class ShowImageActivity extends Activity {

    private int position;
    String imageUrl1 = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1564140604531&di=866a69099c6673c21020730d60758c73&imgtype=0&src=http%3A%2F%2F5b0988e595225.cdn.sohucs.com%2Fimages%2F20171207%2Ff741cfd00d494cc8a388793351043ee4.jpeg";
    String imageUrl2 = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1564139010069&di=c6f0e79ca23a07055cc4a03430aa9eeb&imgtype=0&src=http%3A%2F%2F5b0988e595225.cdn.sohucs.com%2Fimages%2F20171207%2Fbc9df807558444c9953258164a72779e.jpeg";
    String imageUrl3 = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1564140954192&di=5e63e3e58b1862f5b47fbae003d709e6&imgtype=0&src=http%3A%2F%2F5b0988e595225.cdn.sohucs.com%2Fimages%2F20171207%2Ffd8964e3623d443880f089c5fcb7cd08.jpeg";
    String imageUrl4 = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1564140988695&di=c57ee1eea220afe89f91974d03e2b748&imgtype=0&src=http%3A%2F%2F5b0988e595225.cdn.sohucs.com%2Fimages%2F20171207%2F43710b039cbb49dd816d49b21ca679d8.jpeg";
    String imageUrl5 = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1564735743&di=724ab81be67a95b87aed1d99d58fd6a7&imgtype=jpg&er=1&src=http%3A%2F%2F5b0988e595225.cdn.sohucs.com%2Fimages%2F20171207%2F9e678555ba344ee0b1dd4c505f396f9d.jpeg";
    private ImageView ivShowImage;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //去除狀态欄的方法
//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //去除預設标題欄
//        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_image);
        ivShowImage = findViewById(R.id.iv_show_image);
        position = getIntent().getIntExtra("position", 0);
        String tittle = getIntent().getStringExtra("tittle");
        setTitle(tittle);
        initData();
    }

    private void initData() {
        if (position == 1) {
            Glide.with(this).load(imageUrl1).into(ivShowImage);

        } else if (position == 2) {
            Glide.with(this).load(imageUrl2)
                    //這裡面25代表模糊的角度,1代表模糊的程度模糊的比例,值越大越模糊,值越小越清晰,值必須大于0
                    .apply(RequestOptions.bitmapTransform(new BlurTransformation(25, 1)))
                    .into(ivShowImage);

        }

    }
}
           

再來看下XML檔案

<?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">

    <ImageView
        android:src="@mipmap/ic_launcher"
        android:id="@+id/iv_show_image"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>
           

CSDN源碼請導入module,附帶源碼:點選下載下傳

如果沒有積分給個GitHub位址可直接導入project:GitHub下載下傳

繼續閱讀