Android开发实现圆形头像

1.在 res/drawable 文件夹下把你的头像图片放里面,我这里使用的头像文件是 head.jpg

2.布局 xml 文件里使用 ImageView 组件把头像显示出来

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/head_portrait"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_weight="1"
        tools:srcCompat="@drawable/head" />
</LinearLayout>

3.新建Tool类来确保头像图片长宽是正方形

package com.example.tools;

import android.graphics.Bitmap;
import android.graphics.Matrix;

public class Tools {
    //确保头像的是正方形
    public static Bitmap makeBitmapSquare(Bitmap oldbitmap, int newWidth){
        Bitmap newbitmap=null;
        if (oldbitmap.getWidth()>oldbitmap.getHeight()){
            newbitmap=Bitmap.createBitmap(oldbitmap,oldbitmap.getWidth()/2-oldbitmap.getHeight()/2,0,oldbitmap.getHeight(),oldbitmap.getHeight());
        }else{
            newbitmap=Bitmap.createBitmap(oldbitmap,0,oldbitmap.getHeight()/2-oldbitmap.getWidth()/2,oldbitmap.getWidth(),oldbitmap.getWidth());
        }
        int width=newbitmap.getWidth();
        float scaleWidth=((float)newWidth)/width;
        Matrix matrix=new Matrix();
        matrix.postScale(scaleWidth,scaleWidth);
        newbitmap= Bitmap.createBitmap(newbitmap,0,0,width,width,matrix,true);
        return newbitmap;
    }
}

4.在应用启动类里增加圆形头像框设置

package com.example.myapplicationjava;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.drawable.RoundedBitmapDrawable;
import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory;
import com.example.tools.Tools;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //设置圆形头像框
        ImageView imageView_tx=(ImageView)findViewById(R.id.head_portrait);
        Bitmap bitmap1= BitmapFactory.decodeResource(getResources(), R.drawable.head);
        Bitmap bitmap2= Tools.makeBitmapSquare(bitmap1,120);
        RoundedBitmapDrawable roundImg1= RoundedBitmapDrawableFactory.create(getResources(),bitmap2);
        roundImg1.setAntiAlias(true);
        roundImg1.setCornerRadius(bitmap2.getWidth()/2);
        imageView_tx.setImageDrawable(roundImg1);
    }
}

效果如下

在这里插入图片描述

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android Studio实现圆形头像的方法有以下几种: 1. 使用ImageView的圆形裁剪功能 在布局文件中添加一个ImageView,并设置其src属性为头像图片,然后设置其scaleType为centerCrop,最后使用android:background属性设置圆形背景即可实现圆形头像。 ``` <ImageView android:id="@+id/avatar" android:layout_width="100dp" android:layout_height="100dp" android:scaleType="centerCrop" android:src="@drawable/avatar" android:background="@drawable/circle"/> ``` 其中,circle.xml是一个圆形背景的drawable文件,代码如下: ``` <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <solid android:color="#FFFFFF" /> </shape> ``` 2. 使用Glide库实现圆形头像 Glide是一款强大的图片加载库,可以方便地加载网络或本地图片,并提供了很多图片处理的功能,包括圆形裁剪。 在布局文件中添加一个ImageView,并设置其src属性为空,然后在Java代码中使用Glide加载头像图片,并使用Glide提供的Transformations.circleCrop()方法进行圆形裁剪,最后设置到ImageView中即可。 ``` <ImageView android:id="@+id/avatar" android:layout_width="100dp" android:layout_height="100dp"/> Glide.with(this) .load("http://xxx.com/avatar.png") .apply(RequestOptions.circleCropTransform()) .into(imageView); ``` 3. 使用自定义View实现圆形头像 自定义View是一种更灵活、更可定制化的实现方式。可以继承View或其子类,然后在onDraw方法中绘制头像图片,并使用canvas.drawCircle()方法绘制圆形背景。 ``` public class CircleImageView extends ImageView { private Paint mPaint; public CircleImageView(Context context) { super(context); init(); } public CircleImageView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public CircleImageView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init() { mPaint = new Paint(); mPaint.setAntiAlias(true); mPaint.setColor(Color.WHITE); } @Override protected void onDraw(Canvas canvas) { Drawable drawable = getDrawable(); if (drawable == null) { super.onDraw(canvas); return; } Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap(); Bitmap circleBitmap = getCircleBitmap(bitmap); canvas.drawBitmap(circleBitmap, 0, 0, null); } private Bitmap getCircleBitmap(Bitmap bitmap) { Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(output); int width = bitmap.getWidth(); int height = bitmap.getHeight(); int radius = Math.min(width, height) / 2; canvas.drawCircle(width / 2, height / 2, radius, mPaint); mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(bitmap, 0, 0, mPaint); return output; } } ``` 在布局文件中使用自定义View即可。 ``` <com.example.CircleImageView android:id="@+id/avatar" android:layout_width="100dp" android:layout_height="100dp" android:src="@drawable/avatar"/> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值