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

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

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

太極圖

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

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

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

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

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

先介紹一下定義的東西:

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

1.第一步,畫(huà)一個(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.第二步,畫(huà)黑白兩個(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.第三步,畫(huà)黑白兩個(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)畫(huà)
 private int animaltime;//動(dòng)畫(huà)的旋轉(zhuǎn)時(shí)間(速度)

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

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


  }

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

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

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

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

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

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();

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

  @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)畫(huà),旋轉(zhuǎn)中心默認(rèn)為控件中點(diǎn)
      objectAnimator.setDuration(animaltime);//設(shè)置動(dòng)畫(huà)時(shí)間
      objectAnimator.setInterpolator(new LinearInterpolator());//動(dòng)畫(huà)時(shí)間線(xiàn)性漸變
      objectAnimator.setRepeatCount(ObjectAnimator.INFINITE);
      objectAnimator.setRepeatMode(ObjectAnimator.RESTART);
      objectAnimator.start();//動(dòng)畫(huà)開(kāi)始
    }else{
      objectAnimator.resume();//動(dòng)畫(huà)繼續(xù)開(kāi)始
    }


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

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ū)δ兴鶐椭?。如有疑?wèn)可留言。歡迎各位前輩點(diǎn)評(píng)

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

相關(guān)文章

最新評(píng)論

安宁市| 漠河县| 定兴县| 栾川县| 遂溪县| 芷江| 囊谦县| 昭平县| 周宁县| 绥化市| 彭泽县| 丰台区| 芦山县| 桐乡市| 亚东县| 会理县| 镇赉县| 邻水| 正镶白旗| 介休市| 云浮市| 崇文区| 桃源县| 吐鲁番市| 常德市| 张掖市| 德庆县| 始兴县| 庆城县| 成安县| 金山区| 沾化县| 天全县| 永仁县| 墨玉县| 廉江市| 昭觉县| 石泉县| 屏东市| 南宫市| 潞城市|