天天看點

Android實作使用者圓形頭像和模糊背景設計

1、效果展示

Android實作使用者圓形頭像和模糊背景設計

2、在build.gradle(Module)中的dependencies裡面加入下面依賴

注意:glide依賴的版本

//圖檔加載架構
implementation 'jp.wasabeef:glide-transformations:2.0.2' //圖檔模糊效果
compile 'com.github.bumptech.glide:glide:3.7.0'
//圓形頭像
implementation 'de.hdodenhof:circleimageview:2.2.0'
           

3、布局實作,activity_head.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context=".charttest.HeadActivity">

    <ImageView
        android:id="@+id/mImage"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:scaleType="centerCrop"
        />
    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/civ_head"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:src="@drawable/head"
        android:layout_gravity="center"
        />

</FrameLayout>
           

4、核心類實作HeadActivity.java

package com.example.crab_breeding.charttest;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.example.crab_breeding.R;

import jp.wasabeef.glide.transformations.BlurTransformation;

public class HeadActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_head);

        //擷取圓形頭像和背景的imageview執行個體
        ImageView mImage=findViewById(R.id.mImage);
        ImageView civ_head=findViewById(R.id.civ_head);

        //背景模糊實作
        // 參數20 表示模糊背景圖檔的放大參數 越大背景圖檔越模糊
        Glide.with(HeadActivity.this)
                .load(R.drawable.head)
                .bitmapTransform(new BlurTransformation(HeadActivity.this,20,2))
                .into(mImage);
        //頭像圓形實作
        Glide.with(HeadActivity.this)
                .load(R.drawable.head)
                .into(civ_head);
    }
}
           

5、完成,nice!