Android实现带圆环的圆形头像

作者:番茄炒蛋不要蛋 时间:2021-09-30 09:04:47 

在最近写的一个天气APP中用到了圆形头像这样的一个样式,中间是圆形的头像(被圆形切割的图片),周围是一个带颜色的圆环。如下图所示,今天就来说一所它的实现过程。

Android实现带圆环的圆形头像

它的实现也不是特别困难,其实就是用到了BitmapShader这个用法,然后包装成一个paint,最后画出一个圆。

1>实现一个Paint画出以圆形背景的圆。

2>以同样的圆形画出一个稍微小一点的圆,作为它的有色圆环。(此圆和上一个圆颜色不同)。

3>用BitmapShader实现一个新的圆,和第二个圆的大小圆心一致。

(BitmapShader只能在onDraw中实现,在其他外部无法实现)

具体代码如下:

1、界面代码 


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/activity_circle_weather"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.example.app_switchbutton.CircleWeatherActivity">

<com.example.app_switchbutton.CircleWeather
   android:layout_width="250dp"
   android:layout_height="wrap_content"
   android:id="@+id/circleWeather"
   android:layout_centerHorizontal="true"/>

</RelativeLayout>

2、逻辑java代码:


package com.example.app_switchbutton;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;

/**
* Created by 尽途 on 2017/5/12.
*/

public class CircleWeather extends View {
 private int widthSize;
 private int heightSize;
 private Paint mpaint1,mpaint2,mpaint3;
 private Bitmap mbitmap;
 private BitmapShader mbitmapshader;

public CircleWeather(Context context){
   super(context);
   initView();
 }
 public CircleWeather(Context context, AttributeSet attributeSet){
   super(context,attributeSet);
   initView();
 }
 private void initView(){
   mpaint1=new Paint();
   mpaint2=new Paint();
   mpaint3=new Paint();
   mpaint2.setStyle(Paint.Style.FILL);
   mpaint3.setStyle(Paint.Style.FILL);
   mpaint2.setAntiAlias(true);
   mpaint3.setAntiAlias(true);
   mpaint2.setColor(getResources().getColor(R.color.colorPrimary));
   mpaint3.setColor(getResources().getColor(R.color.colorGray));
 }

@Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
   widthSize=MeasureSpec.getSize(widthMeasureSpec);
   heightSize=widthSize;
   setMeasuredDimension(widthSize,heightSize);
 }

@Override
 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
   super.onSizeChanged(w, h, oldw, oldh);
 }

@Override
 protected void onDraw(Canvas canvas) {
   mbitmap= BitmapFactory.decodeResource(getResources(),R.drawable.hehua);//bitmapshader只能在onDraw中实现在外部不可以
   int BitmapWidthSize=mbitmap.getWidth();
   int BitmapHeightSize=mbitmap.getHeight();
   float scale=(float)widthSize/Math.min(BitmapHeightSize,BitmapWidthSize);//获取最为合适的尺寸
   Matrix matrix=new Matrix();
   matrix.setScale(scale,scale);
   Bitmap bitmap=Bitmap.createBitmap(mbitmap,0,0,BitmapWidthSize,BitmapHeightSize,matrix,true);
   mbitmapshader=new BitmapShader(bitmap, Shader.TileMode.CLAMP,Shader.TileMode.CLAMP);
   mpaint1.setShader(mbitmapshader);
   canvas.drawCircle((float)widthSize*0.5f,(float)heightSize*0.5f,(float)heightSize*0.5f,mpaint2);
   canvas.drawCircle((float)widthSize*0.5f,(float)heightSize*0.5f,(float)heightSize*0.47f,mpaint3);
   canvas.drawCircle((float)widthSize*0.5f,(float)heightSize*0.5f,(float)heightSize*0.47f,mpaint1);
   super.onDraw(canvas);
 }
}

来源:https://blog.csdn.net/ZQL2288/article/details/72675490

标签:Android,圆形头像
0
投稿

猜你喜欢

  • Java原子操作CAS原理解析

    2021-10-20 01:07:05
  • java base64编码、解码的三种方式总结

    2022-12-29 22:20:51
  • Springboot 在普通类型注入Service或mapper

    2023-11-29 15:26:21
  • C# List<T> Contains<T>()的用法小结

    2021-05-29 11:44:56
  • JAVA实现对阿里云DNS的解析管理

    2023-11-24 13:25:15
  • Java 关键字break和continue的使用说明

    2023-07-22 03:50:59
  • 详解SpringMVC如何进行数据回显

    2023-09-12 08:48:15
  • java操作mongodb示例分享

    2023-09-07 19:00:02
  • java selenium使用浏览器调试工具实现方法

    2023-07-27 04:04:22
  • Java实现二分搜索树的示例代码

    2023-08-05 10:43:40
  • springboot如何使用AOP做访问请求日志

    2023-11-11 10:12:19
  • JDBC增删改查和查唯一的完整代码解析

    2023-12-22 15:53:30
  • 阿里、华为、腾讯Java技术面试题精选

    2023-11-25 02:29:39
  • Spring纯注解配置实现代码示例解析

    2021-06-05 18:54:28
  • Java 8中日期和时间的处理方法

    2021-09-07 15:37:58
  • 详解Spring Bean的配置方式与实例化

    2022-01-13 05:47:51
  • Java 分割字符串详解及实例代码

    2023-11-29 13:18:42
  • c# 编写的简单飞行棋游戏

    2022-12-05 05:10:59
  • 浅谈在springboot中使用定时任务的方式

    2023-03-02 19:14:09
  • SpringBoot异常处理器的使用与添加员工功能实现流程介绍

    2021-10-21 19:24:23
  • asp之家 软件编程 m.aspxhome.com