Android自定义view之太极图的实现教程

太极图

周四课余时间比较多,正好前几天为了给小学弟解决问题,回顾了一些Android的知识,(上学还是不能把以前上班学到的东西丢掉)于是写一篇关于自定义view的文章。

最后完成的样子(可旋转)

这篇文章主要内容为使用Canvas画简单图案,自定义属性,以及属性动画ObjectAnimator中的旋转动画

提示:以下是本篇文章正文内容

一、先画一个太极

先介绍一下定义的东西:

  private int useWidth; //最后测量得到的值
  private int leftcolor; //左太极的颜色(黑)
  private int rightcolor;//右太极的颜色(白)

1.第一步,画一个半边黑半边白的圆

    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.第二步,画黑白两个小圆

    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.第三步,画黑白两个更小的圆

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

效果

二、让太极旋转

先简单介绍一下定义的东西

 private ObjectAnimator objectAnimator;//属性动画
 private int animaltime;//动画的旋转时间(速度)

1.旋转的开始方法

//旋转动画开始的方法
  public void createAnimation() {
    if (objectAnimator==null){
      objectAnimator = ObjectAnimator.ofFloat(this, "rotation", 0f, 360f);//添加旋转动画,旋转中心默认为控件中点
      objectAnimator.setDuration(animaltime);//设置动画时间
      objectAnimator.setInterpolator(new LinearInterpolator());//动画时间线性渐变
      objectAnimator.setRepeatCount(ObjectAnimator.INFINITE);
      objectAnimator.setRepeatMode(ObjectAnimator.RESTART);
      objectAnimator.start();//动画开始
    }else{
      objectAnimator.resume();//动画继续开始
    }

  }

2.旋转的暂停方法(可继续旋转)

  public void stopAnimation(){
    if (objectAnimator!=null){
      objectAnimator.pause();//动画暂停 .end()结束动画
    }
  }

3.旋转的停止方法(不可继续旋转)

  public void cleanAnimation(){
    if (objectAnimator!=null){
      objectAnimator.end(); //结束动画
    }
  }

三、自定义属性(颜色,动画速度)

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);
    //时间
    animaltime=ta.getInt(R.styleable.TaiJiView_animaltime,1000);

    //回收
    ta.recycle();

  }

最后运行一下看一下效果

四、源码

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();    //创建画笔对象
    mPaint.setColor(Color.BLACK);  //设置画笔颜色
    mPaint.setStyle(Paint.Style.FILL); //设置画笔模式为填充
    mPaint.setStrokeWidth(10f);   //设置画笔宽度为10px
    mPaint.setAntiAlias(true);   //设置抗锯齿
    mPaint.setAlpha(255);    //设置画笔透明度
  }

  @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);//添加旋转动画,旋转中心默认为控件中点
      objectAnimator.setDuration(animaltime);//设置动画时间
      objectAnimator.setInterpolator(new LinearInterpolator());//动画时间线性渐变
      objectAnimator.setRepeatCount(ObjectAnimator.INFINITE);
      objectAnimator.setRepeatMode(ObjectAnimator.RESTART);
      objectAnimator.start();//动画开始
    }else{
      objectAnimator.resume();//动画继续开始
    }

  }
  @RequiresApi(api = Build.VERSION_CODES.KITKAT)
  public void stopAnimation(){
    if (objectAnimator!=null){
      objectAnimator.pause();//动画暂停 .end()结束动画
    }
  }
  public void cleanAnimation(){
    if (objectAnimator!=null){
      objectAnimator.end(); //结束动画
    }
  }
}

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>

总结

希望能够对您有所帮助。如有疑问可留言。欢迎各位前辈点评

到此这篇关于Android自定义view之太极图的文章就介绍到这了,更多相关Android自定义view之太极图内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • Android自定义View仿大众点评星星评分控件

    本文实例为大家分享了Android仿大众点评星星评分控件的具体代码,供大家参考,具体内容如下 话不多说,直接上代码,这里采用的是自定View public class RatingBar extends View { // 正常.半个和选中的星星 private Bitmap mStarNormal, mStarHalf, mStarSelected; //星星的总数 private int mStartTotalNumber = 5; //选中的星星个数 private float mSele

  • Android 滑动Scrollview标题栏渐变效果(仿京东toolbar)

    Scrollview标题栏滑动渐变 仿京东样式(上滑显示下滑渐变消失) /** * @ClassName MyScrollView * @Author Rex * @Date 2021/1/27 17:38 */ public class MyScrollView extends ScrollView { private TranslucentListener mTranslucentListener; public void setTranslucentListener(Translucent

  • 源码详解Android中View.post()用法

    emmm,大伙都知道,子线程是不能进行 UI 操作的,或者很多场景下,一些操作需要延迟执行,这些都可以通过 Handler 来解决.但说实话,实在是太懒了,总感觉写 Handler 太麻烦了,一不小心又很容易写出内存泄漏的代码来,所以为了偷懒,我就经常用 View.post() or View.postDelay() 来代替 Handler 使用. 但用多了,总有点心虚,View.post() 会不会有什么隐藏的问题?所以趁有点空余时间,这段时间就来梳理一下,View.post() 原理到底是什

  • Android自定义view之围棋动画效果的实现

    前言 废话不多说直接开始 老规矩,文章最后有源码 完成效果图 棋子加渐变色 棋子不加渐变色 一.测量 1.获取宽高 @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 =

  • Android使用setContentView实现页面的转换效果

    一提到Android中页面的切换,你是不是只想到了startActivity启动另一个Activity? 其实在Android中,可以直接利用setContentView达到类似页面转换效果的!实现思路如下: 在第一个Activity的布局中添加一个Button,实现点击事件 点击该Button,调用setContentView,传入第二个页面的Layout,第二个页面就显示出来了 第二个页面的布局中仍然有一个Button,仍然实现其点击事件 点击该Button,调用setContentView

  • Android自定义View实现分段选择按钮的实现代码

    首先演示下效果,分段选择按钮,支持点击和滑动切换. 视图绘制过程中,要执行onMeasure.onLayout.onDraw等方法,这也是自定义控件最常用到的几个方法. onMeasure:测量视图的大小,可以根据MeasureSpec的Mode确定父视图和子视图的大小. onLayout:确定视图的位置 onDraw:绘制视图 这里就不做过多的介绍,主要介绍本控件涉及的到的部分. 1.1 获取item大小.起始位置 @Override protected void onMeasure(int

  • Android View.Post 的原理及缺陷

    很多开发者都了解这么一个知识点:在 Activity 的 onCreate 方法里我们无法直接获取到 View 的宽高信息,但通过 View.post(Runnable)这种方式就可以,那背后的具体原因你是否有了解过呢? 读者可以尝试以下操作.可以发现,除了通过 View.post(Runnable)这种方式可以获得 View 的真实宽高外,其它方式取得的值都是 0 /** * 作者:leavesC * 时间:2020/03/14 11:05 * 描述: * GitHub:https://git

  • Android使用ScrollView实现滚动效果

    本文实例为大家分享了ScrollView实现滚动效果的具体代码,供大家参考,具体内容如下 如果长文本的内容超过一屏幕 则只能显示一屏幕的内容 设置ScrollView 通过滚动浏览下面的内容 若将标签更改为<HorizontalScrollView></HorizontalScrollView>则为水平滚动效果 xml文件: <?xml version="1.0" encoding="utf-8"?> <android.su

  • Android 中 WebView 的基本用法详解

    加载 URL (网络或者本地 assets 文件夹下的 html 文件) 加载 html 代码 Native 和 JavaScript 相互调用 加载网络 URL webview.loadUrl(https://www.baidu.com/); 加载 assets 下的 html 文件 webview.loadUrl(file:///android_asset/test.html); 加载 html 代码 // 两个代码差不多 // 偶尔出现乱码 webview.loadData(); // 比

  • Android使用TypeFace设置TextView的文字字体

    在Android里面设置一个TextView的文字颜色和文字大小,都很简单,也是一个常用的基本功能.但很少有设置文字字体的,今天要分享的是通过TypeFace去设置TextView的文字字体,布局里面有两个Button,总共包含两个小功能:换字体和变大. 功能的核心部分主要是两点: 创建assets外部资源文件夹,将ttf格式的字体文件放在该目录下 通过TypeFace类的createFromAsset方法,让TextView通过setTypeFace来改变字体 完整源码如下: 1.主Activ

随机推荐