Fresco-高斯模糊

首先效果圖

 

MainActivity.java

public class MainActivity extends AppCompatActivity {

    public String url="http://img5.duitang.com/uploads/item/201312/03/20131203153823_Y4y8F.jpeg";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SimpleDraweeView simpleDraweeView = findViewById(R.id.main_sdv);  //加載頭像
        Uri uri = Uri.parse("http://ww3.sinaimg.cn/large/610dc034jw1fasakfvqe1j20u00mhgn2.jpg");
        simpleDraweeView.setImageURI(uri);

        SimpleDraweeView simpleDraweeView2 = findViewById(R.id.main_sdv2);  //加載背景圖
        //Uri uri2 = Uri.parse("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1545714226657&di=9e6d1b27a78f92b07a62504be95cc995&imgtype=0&src=http%3A%2F%2Fimg.alicdn.com%2Fimgextra%2Fi1%2F828655885%2FTB2j0_1bXXXXXa_XXXXXXXXXXXX_%2521%2521828655885.png");
        showUrlBlur(simpleDraweeView2,url,1,10);  //iterations越大越模糊
        //simpleDraweeView2.setImageURI(uri2);
    }

    //高斯模糊
    public static void showUrlBlur(SimpleDraweeView draweeView, String url, int iterations, int blurRadius) {
        try {
            Uri uri = Uri.parse(url);
            ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri)
                    .setPostprocessor(new IterativeBoxBlurPostProcessor(iterations, blurRadius))
                    .build();
            AbstractDraweeController controller = Fresco.newDraweeControllerBuilder()
                    .setOldController(draweeView.getController())
                    .setImageRequest(request)
                    .build();
            draweeView.setController(controller);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:fresco="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    >
    <com.facebook.drawee.view.SimpleDraweeView
        android:id="@+id/main_sdv2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        fresco:roundedCornerRadius="20dp"
        android:src="@color/colorAccent"
        />
    <com.facebook.drawee.view.SimpleDraweeView
        android:id="@+id/main_sdv"
        android:layout_width="200dp"
        android:layout_height="200dp"
        fresco:roundAsCircle="true"
        android:layout_centerInParent="true"
        app:roundingBorderColor="#fff"
        app:roundingBorderWidth="2dp"
        />
</RelativeLayout>
MyApplication.java
public class MyApplication extends Application{
    @Override
    public void onCreate() {
        super.onCreate();
        Fresco.initialize(this);
    }
}

有關的屬性

    <com.facebook.drawee.view.SimpleDraweeView
        android:id="@+id/id_main_sdv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        fresco:actualImageScaleType="focusCrop"             //加載得到的圖片的縮放類型
        fresco:fadeDuration="1000"                          //進度條,佔位圖片消失,加載圖片展現的時間間隔
        fresco:failureImage="@drawable/imgbg"               //加載失敗之後顯示的圖片
        fresco:failureImageScaleType="centerInside"         //圖片縮放類型
        fresco:placeholderImage="@drawable/imgbg"           //佔位圖片(未加載之前顯示的圖片)
        fresco:placeholderImageScaleType="fitCenter"      
        fresco:progressBarAutoRotateInterval="1000"         //加載進度條圖片旋轉週期
        fresco:progressBarImage="@drawable/progress_bar"    //加載進度條圖片
        fresco:progressBarImageScaleType="centerInside"
        fresco:retryImage="@mipmap/ic_launcher"             //提示重新加載的圖片資源
        fresco:retryImageScaleType="centerCrop"
        fresco:backgroundImage="@color/colorWhite"          //背景圖片
        fresco:roundAsCircle="false"                        //是否要將圖片剪切成圓形
        fresco:viewAspectRatio="1"                          //圖片寬高比
        fresco:overlayImage="@drawable/overlay"             //在圖片上方覆蓋一個圖片資源
        fresco:pressedStateOverlayImage="@color/colorBlack"
        fresco:roundedCornerRadius="20dp"                   //圓角角度,
        fresco:roundTopLeft="true"                          //設置哪個角需要變成圓角
        fresco:roundTopRight="false"
        fresco:roundBottomLeft="false"
        fresco:roundBottomRight="true"
        fresco:roundWithOverlayColor="@color/colorWhite"    //圓角部分填充色
        fresco:roundingBorderWidth="2dp"                    //邊框寬度
        fresco:roundingBorderColor="@color/colorBlack"      //邊框填充色
        />

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章