欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android實現(xiàn)帶圓環(huán)的圓形頭像

 更新時間:2020年08月20日 09:52:04   作者:番茄炒蛋不要蛋  
這篇文章主要為大家詳細介紹了Android實現(xiàn)帶圓環(huán)的圓形頭像,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

在最近寫的一個天氣APP中用到了圓形頭像這樣的一個樣式,中間是圓形的頭像(被圓形切割的圖片),周圍是一個帶顏色的圓環(huán)。如下圖所示,今天就來說一所它的實現(xiàn)過程。

它的實現(xiàn)也不是特別困難,其實就是用到了BitmapShader這個用法,然后包裝成一個paint,最后畫出一個圓。

1>實現(xiàn)一個Paint畫出以圓形背景的圓。

2>以同樣的圓形畫出一個稍微小一點的圓,作為它的有色圓環(huán)。(此圓和上一個圓顏色不同)。

3>用BitmapShader實現(xiàn)一個新的圓,和第二個圓的大小圓心一致。

(BitmapShader只能在onDraw中實現(xiàn),在其他外部無法實現(xiàn))

具體代碼如下:

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中實現(xiàn)在外部不可以
    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);
  }
}

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • 在Android項目中使用AspectJ的方法

    在Android項目中使用AspectJ的方法

    這篇文章主要介紹了在Android項目中使用AspectJ的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-04-04
  • Kotlin Service服務組件開發(fā)詳解

    Kotlin Service服務組件開發(fā)詳解

    這幾天分析了一下的啟動過程,于是乎,今天寫一下Service使用; 給我的感覺是它并不復雜,千萬不要被一坨一坨的代碼嚇住了,雖然彎彎繞繞不少,重載函數(shù)一個接著一個,就向走迷宮一樣,但只要抓住主線閱讀,很快就能找到出口
    2022-12-12
  • Android編程輸入事件流程詳解

    Android編程輸入事件流程詳解

    這篇文章主要介紹了Android編程輸入事件流程,較為詳細的分析了Android輸入事件原理、相關概念與具體操作流程,需要的朋友可以參考下
    2016-10-10
  • Android懸浮窗的實現(xiàn)(易錯點)

    Android懸浮窗的實現(xiàn)(易錯點)

    現(xiàn)在很多應用都使用到懸浮窗,例如微信在視頻的時候,點擊Home鍵,視頻小窗口仍然會在屏幕上顯示。下面小編來實現(xiàn)一下android 懸浮窗,感興趣的朋友跟隨小編一起看看吧
    2019-10-10
  • android仿直播圓點加載效果

    android仿直播圓點加載效果

    這篇文章主要為大家詳細介紹了android仿直播圓點加載效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • 避免 Android中Context引起的內(nèi)存泄露

    避免 Android中Context引起的內(nèi)存泄露

    本文主要介紹Android中Context引起的內(nèi)存泄露的問題,這里對Context的知識做了詳細講解,說明如何避免內(nèi)存泄漏的問題,有興趣的小伙伴可以參考下
    2016-08-08
  • Android手勢ImageView三部曲 第一部

    Android手勢ImageView三部曲 第一部

    這篇文章主要為大家詳細介紹了Android手勢ImageView三部曲的第一部,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • Android ARouter路由框架解析

    Android ARouter路由框架解析

    這篇文章主要介紹了Android ARouter路由框架解析,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-04-04
  • Android自定義轉(zhuǎn)盤菜單效果

    Android自定義轉(zhuǎn)盤菜單效果

    這篇文章主要為大家詳細介紹了Android自定義轉(zhuǎn)盤菜單效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-07-07
  • Android無障礙自動化結(jié)合opencv實現(xiàn)支付寶能量自動收集操作方法

    Android無障礙自動化結(jié)合opencv實現(xiàn)支付寶能量自動收集操作方法

    opencv可以進行圖像識別,兩者結(jié)合在一起即可實現(xiàn)支付寶能量自動收集,opencv用于識別能量,無障礙服務用于模擬手勢,即點擊能量,這篇文章主要介紹了Android無障礙自動化結(jié)合opencv實現(xiàn)支付寶能量自動收集,需要的朋友可以參考下
    2024-07-07

最新評論