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

Android自定義view之太極圖的實(shí)現(xiàn)教程

 更新時(shí)間:2021年01月05日 10:54:59   作者:計(jì)蒙不吃魚  
這篇文章主要給大家介紹了關(guān)于Android自定義view之太極圖的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

太極圖

周四課余時(shí)間比較多,正好前幾天為了給小學(xué)弟解決問題,回顧了一些Android的知識(shí),(上學(xué)還是不能把以前上班學(xué)到的東西丟掉)于是寫一篇關(guān)于自定義view的文章。

最后完成的樣子(可旋轉(zhuǎn))

這篇文章主要內(nèi)容為使用Canvas畫簡(jiǎn)單圖案,自定義屬性,以及屬性動(dòng)畫ObjectAnimator中的旋轉(zhuǎn)動(dòng)畫

提示:以下是本篇文章正文內(nèi)容

一、先畫一個(gè)太極

先介紹一下定義的東西:

  private int useWidth; //最后測(cè)量得到的值
  private int leftcolor; //左太極的顏色(黑)
  private int rightcolor;//右太極的顏色(白)

1.第一步,畫一個(gè)半邊黑半邊白的圓

    mPaint.setColor(leftcolor);
    canvas.drawArc(new RectF(0, 0, useWidth, useWidth), 270, -180, true, mPaint);

    mPaint.setColor(rightcolor);
    canvas.drawArc(new RectF(0, 0, useWidth, useWidth), 270, 180, true, mPaint);

效果:


2.第二步,畫黑白兩個(gè)小圓

    mPaint.setColor(leftcolor);
    canvas.drawArc(new RectF(useWidth / 4, 0, useWidth / 2 +useWidth / 4, useWidth / 2),
        270, 360, true, mPaint);

    mPaint.setColor(rightcolor);
    canvas.drawArc(new RectF(useWidth / 4, useWidth / 2, useWidth / 2 + useWidth / 4,useWidth),
        270, 360, true, mPaint);

效果:

3.第三步,畫黑白兩個(gè)更小的圓

    mPaint.setColor(leftcolor);
    canvas.drawCircle(useWidth/ 2, useWidth * 3 / 4, useWidth/16, mPaint);

    mPaint.setColor(rightcolor);
    canvas.drawCircle(useWidth / 2, useWidth / 4, useWidth/16, mPaint);

效果

二、讓太極旋轉(zhuǎn)

先簡(jiǎn)單介紹一下定義的東西

 private ObjectAnimator objectAnimator;//屬性動(dòng)畫
 private int animaltime;//動(dòng)畫的旋轉(zhuǎn)時(shí)間(速度)

1.旋轉(zhuǎn)的開始方法

//旋轉(zhuǎn)動(dòng)畫開始的方法
  public void createAnimation() {
    if (objectAnimator==null){
      objectAnimator = ObjectAnimator.ofFloat(this, "rotation", 0f, 360f);//添加旋轉(zhuǎn)動(dòng)畫,旋轉(zhuǎn)中心默認(rèn)為控件中點(diǎn)
      objectAnimator.setDuration(animaltime);//設(shè)置動(dòng)畫時(shí)間
      objectAnimator.setInterpolator(new LinearInterpolator());//動(dòng)畫時(shí)間線性漸變
      objectAnimator.setRepeatCount(ObjectAnimator.INFINITE);
      objectAnimator.setRepeatMode(ObjectAnimator.RESTART);
      objectAnimator.start();//動(dòng)畫開始
    }else{
      objectAnimator.resume();//動(dòng)畫繼續(xù)開始
    }


  }

2.旋轉(zhuǎn)的暫停方法(可繼續(xù)旋轉(zhuǎn))

  public void stopAnimation(){
    if (objectAnimator!=null){
      objectAnimator.pause();//動(dòng)畫暫停 .end()結(jié)束動(dòng)畫
    }
  }

3.旋轉(zhuǎn)的停止方法(不可繼續(xù)旋轉(zhuǎn))

  public void cleanAnimation(){
    if (objectAnimator!=null){
      objectAnimator.end(); //結(jié)束動(dòng)畫
    }
  }

三、自定義屬性(顏色,動(dòng)畫速度)

1.新建attrs文件

2.定義屬性

  <declare-styleable name="TaiJiView">
    <attr name="leftcolor" format="reference|color"/>
    <attr name="rightcolor" format="reference|color"/>
    <attr name="animaltime" format="integer"/>
  </declare-styleable>

3.布局中使用

    app:leftcolor="@color/colorAccent"
    app:rightcolor="@color/colorPrimaryDark"
    app:animaltime="3000"

4.java文件中獲取在布局中定義的值

 /**
   獲取自定義屬性
   */
  private void initCustomAttrs(Context context, AttributeSet attrs) {
    //獲取自定義屬性。
    TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TaiJiView);
    //獲取太極顏色
    leftcolor = ta.getColor(R.styleable.TaiJiView_leftcolor, Color.BLACK);
    rightcolor=ta.getColor(R.styleable.TaiJiView_rightcolor, Color.WHITE);
    //時(shí)間
    animaltime=ta.getInt(R.styleable.TaiJiView_animaltime,1000);

    //回收
    ta.recycle();

  }

最后運(yùn)行一下看一下效果

四、源碼

1.TaiJiView.java

public class TaiJiView extends View{
  private Paint mPaint;
  private int mWidth;
  private int mHeight;
  private int useWidth;
  private int leftcolor;
  private int rightcolor;
  private ObjectAnimator objectAnimator;
  private int animaltime;
  @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
  public TaiJiView(Context context) {
    this(context,null);
  }

  @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
  public TaiJiView(Context context, @Nullable AttributeSet attrs) {
    this(context, attrs,0);
  }

  @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
  public TaiJiView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    this(context, attrs, defStyleAttr,0);
    initCustomAttrs(context,attrs);
  }

  @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
  public TaiJiView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
    init();

  }
  private void init() {
    initPaint();
  }


  /**
   獲取自定義屬性
   */
  private void initCustomAttrs(Context context, AttributeSet attrs) {
    //獲取自定義屬性。
    TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TaiJiView);
    //獲取太極顏色
    leftcolor = ta.getColor(R.styleable.TaiJiView_leftcolor, Color.BLACK);
    rightcolor=ta.getColor(R.styleable.TaiJiView_rightcolor, Color.WHITE);
    animaltime=ta.getInt(R.styleable.TaiJiView_animaltime,1000);

    //回收
    ta.recycle();

  }
  /**
   * 初始化畫筆
   */
  private void initPaint() {
    mPaint = new Paint();    //創(chuàng)建畫筆對(duì)象
    mPaint.setColor(Color.BLACK);  //設(shè)置畫筆顏色
    mPaint.setStyle(Paint.Style.FILL); //設(shè)置畫筆模式為填充
    mPaint.setStrokeWidth(10f);   //設(shè)置畫筆寬度為10px
    mPaint.setAntiAlias(true);   //設(shè)置抗鋸齒
    mPaint.setAlpha(255);    //設(shè)置畫筆透明度
  }

  @Override
  protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    mWidth = w;
    mHeight = h;
    useWidth=mWidth;
    if (mWidth>mHeight){
      useWidth=mHeight;
    }
  }


  @RequiresApi(api = Build.VERSION_CODES.KITKAT)
  @Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    mPaint.setColor(leftcolor);
    canvas.drawArc(new RectF(0, 0, useWidth, useWidth), 270, -180, true, mPaint);

    mPaint.setColor(rightcolor);
    canvas.drawArc(new RectF(0, 0, useWidth, useWidth), 270, 180, true, mPaint);

    mPaint.setColor(leftcolor);
    canvas.drawArc(new RectF(useWidth / 4, 0, useWidth / 2 +useWidth / 4, useWidth / 2),
        270, 360, true, mPaint);

    mPaint.setColor(rightcolor);
    canvas.drawArc(new RectF(useWidth / 4, useWidth / 2, useWidth / 2 + useWidth / 4,useWidth),
        270, 360, true, mPaint);
    mPaint.setColor(leftcolor);
    canvas.drawCircle(useWidth/ 2, useWidth * 3 / 4, useWidth/16, mPaint);

    mPaint.setColor(rightcolor);
    canvas.drawCircle(useWidth / 2, useWidth / 4, useWidth/16, mPaint);

  }

  @RequiresApi(api = Build.VERSION_CODES.KITKAT)
  public void createAnimation() {
    if (objectAnimator==null){
      objectAnimator = ObjectAnimator.ofFloat(this, "rotation", 0f, 360f);//添加旋轉(zhuǎn)動(dòng)畫,旋轉(zhuǎn)中心默認(rèn)為控件中點(diǎn)
      objectAnimator.setDuration(animaltime);//設(shè)置動(dòng)畫時(shí)間
      objectAnimator.setInterpolator(new LinearInterpolator());//動(dòng)畫時(shí)間線性漸變
      objectAnimator.setRepeatCount(ObjectAnimator.INFINITE);
      objectAnimator.setRepeatMode(ObjectAnimator.RESTART);
      objectAnimator.start();//動(dòng)畫開始
    }else{
      objectAnimator.resume();//動(dòng)畫繼續(xù)開始
    }


  }
  @RequiresApi(api = Build.VERSION_CODES.KITKAT)
  public void stopAnimation(){
    if (objectAnimator!=null){
      objectAnimator.pause();//動(dòng)畫暫停 .end()結(jié)束動(dòng)畫
    }
  }
  public void cleanAnimation(){
    if (objectAnimator!=null){
      objectAnimator.end(); //結(jié)束動(dòng)畫
    }
  }
}

2.attrs文件

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <declare-styleable name="TaiJiView">
    <attr name="leftcolor" format="reference|color"/>
    <attr name="rightcolor" format="reference|color"/>
    <attr name="animaltime" format="integer"/>
  </declare-styleable>
</resources>

總結(jié)

希望能夠?qū)δ兴鶐椭?。如有疑問可留言。歡迎各位前輩點(diǎn)評(píng)

到此這篇關(guān)于Android自定義view之太極圖的文章就介紹到這了,更多相關(guān)Android自定義view之太極圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Android系統(tǒng)音量條實(shí)例代碼

    Android系統(tǒng)音量條實(shí)例代碼

    這篇文章主要介紹了Android系統(tǒng)音量條實(shí)例代碼,分享了相關(guān)代碼示例,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2018-02-02
  • android基礎(chǔ)總結(jié)篇之二:Activity的四種launchMode

    android基礎(chǔ)總結(jié)篇之二:Activity的四種launchMode

    這篇文章主要介紹了android基礎(chǔ)總結(jié)篇之二:Activity的四種launchMode,有需要的可以了解一下。
    2016-11-11
  • flutter InkWell實(shí)現(xiàn)水波紋點(diǎn)擊效果

    flutter InkWell實(shí)現(xiàn)水波紋點(diǎn)擊效果

    這篇文章主要為大家詳細(xì)介紹了flutter InkWell實(shí)現(xiàn)水波紋點(diǎn)擊效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-07-07
  • Android Beam 文件傳輸失敗分析與解決方法

    Android Beam 文件傳輸失敗分析與解決方法

    最近在修改Android7.0原生平臺(tái)的一些bug,其中有關(guān)Android Beam傳輸文件的一些問題還是挺多的。下面小編給大家?guī)砹薬ndroid beam 文件傳輸失敗的解決方法,一起看看吧
    2017-09-09
  • pp列表之分組ListView詳解

    pp列表之分組ListView詳解

    本篇文章是對(duì)Android中的ListView進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06
  • Android NDK 開發(fā)教程

    Android NDK 開發(fā)教程

    這篇文章主要介紹了Android NDK 開發(fā)教程的相關(guān)資料,需要的朋友可以參考下
    2015-11-11
  • 圖文詳解Flutter單例的實(shí)現(xiàn)

    圖文詳解Flutter單例的實(shí)現(xiàn)

    一個(gè)類只允許創(chuàng)建一個(gè)實(shí)例,那這個(gè)類就是一個(gè)單例類,這種設(shè)計(jì)模式就叫作單例設(shè)計(jì)模式,簡(jiǎn)稱單例模式,下面這篇文章主要給大家介紹了關(guān)于Flutter單例的實(shí)現(xiàn)方法,需要的朋友可以參考下
    2021-12-12
  • 淺談Android輕量級(jí)的數(shù)據(jù)緩存框架RxCache

    淺談Android輕量級(jí)的數(shù)據(jù)緩存框架RxCache

    本篇文章主要介紹了淺談Android輕量級(jí)的數(shù)據(jù)緩存框架RxCache,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • Kotlin中的高階函數(shù)深入講解

    Kotlin中的高階函數(shù)深入講解

    這篇文章主要給大家介紹了關(guān)于Kotlin中高階函數(shù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-11-11
  • Android實(shí)現(xiàn)將View轉(zhuǎn)化為圖片并保存到本地

    Android實(shí)現(xiàn)將View轉(zhuǎn)化為圖片并保存到本地

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)將View轉(zhuǎn)化為圖片并保存到本地,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-02-02

最新評(píng)論