最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

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

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

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

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

(BitmapShader只能在onDraw中實(shí)現(xiàn),在其他外部無法實(shí)現(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中實(shí)現(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);
  }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android Webview重定向問題解決方法

    Android Webview重定向問題解決方法

    在Android開發(fā)過程中,使用過WebView的童鞋可能難免會(huì)遇到URL重定向問題。這篇文章主要介紹了Android Webview重定向問題解決方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2018-05-05
  • Android?獲取實(shí)時(shí)網(wǎng)速實(shí)現(xiàn)詳解

    Android?獲取實(shí)時(shí)網(wǎng)速實(shí)現(xiàn)詳解

    這篇文章主要為大家介紹了Android?獲取實(shí)時(shí)網(wǎng)速實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • Android自定義View——扇形統(tǒng)計(jì)圖的實(shí)現(xiàn)代碼

    Android自定義View——扇形統(tǒng)計(jì)圖的實(shí)現(xiàn)代碼

    本篇文章主要介紹了Android自定義View——扇形統(tǒng)計(jì)圖的實(shí)現(xiàn)代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-02-02
  • Android 輕松實(shí)現(xiàn)圖片倒影效果實(shí)例代碼

    Android 輕松實(shí)現(xiàn)圖片倒影效果實(shí)例代碼

    這篇文章主要介紹了Android 輕松實(shí)現(xiàn)圖片倒影效果實(shí)例代碼,有需要的朋友可以參考一下
    2014-01-01
  • Android基于PhotoView實(shí)現(xiàn)的頭像/圓形裁剪控件

    Android基于PhotoView實(shí)現(xiàn)的頭像/圓形裁剪控件

    這篇文章主要給大家介紹了關(guān)于Android基于PhotoView實(shí)現(xiàn)的頭像/圓形裁剪控件的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-07-07
  • html5在android中的使用問題及技巧解讀

    html5在android中的使用問題及技巧解讀

    本文將詳細(xì)介紹下html5在android中的使用:特效按鈕的進(jìn)展/在html5中有關(guān)于觸摸屏的事件,感興趣的你可以參考下,或許對(duì)你有所幫助
    2013-03-03
  • Android JNI 調(diào)用時(shí)緩存字段和方法ID示例

    Android JNI 調(diào)用時(shí)緩存字段和方法ID示例

    這篇文章主要介紹了Android JNI 調(diào)用時(shí)緩存字段和方法ID示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-07-07
  • Android 自定義View的使用介紹

    Android 自定義View的使用介紹

    本篇文章小編為大家介紹,Android 自定義View的使用。需要的朋友參考下
    2013-04-04
  • Android調(diào)節(jié)屏幕亮度實(shí)現(xiàn)代碼

    Android調(diào)節(jié)屏幕亮度實(shí)現(xiàn)代碼

    這篇文章主要介紹了Android調(diào)節(jié)屏幕亮度實(shí)現(xiàn)代碼,調(diào)節(jié)屏幕亮度時(shí),先設(shè)置當(dāng)前activity亮度,再并保存為系統(tǒng)亮度即可,本文分別給出兩個(gè)步驟的實(shí)現(xiàn)代碼,需要的朋友可以參考下
    2015-05-05
  • Android Studio 下載視頻到本地

    Android Studio 下載視頻到本地

    這篇文章主要介紹了Android Studio 下載視頻到本地,利用GreenDao實(shí)現(xiàn)多線程斷點(diǎn)續(xù)傳,這樣的話,下次用戶再次下載時(shí),將繼續(xù)上次數(shù)據(jù)庫(kù)的接著下載,這樣用戶體驗(yàn)就會(huì)很好,也大大節(jié)省了成本.具體實(shí)現(xiàn)代碼大家參考下本文
    2018-03-03

最新評(píng)論

泗水县| 黑龙江省| 潮安县| 澄迈县| 包头市| 蒙阴县| 仪陇县| 宁化县| 东阿县| 肥乡县| 瑞昌市| 南郑县| 类乌齐县| 湾仔区| 朝阳市| 明水县| 永丰县| 老河口市| 合山市| 桂阳县| 济宁市| 平阴县| 吉安县| 尉犁县| 乌苏市| 东兰县| 六安市| 扬州市| 灵台县| 衡阳县| 霍林郭勒市| 华坪县| 社旗县| 收藏| 鹤庆县| 井冈山市| 平山县| 阳朔县| 阳曲县| 义马市| 裕民县|