太极图

周四课余时间比较多,正好前几天为了给小学弟解决问题,回顾了一些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);     //设置画笔宽度为10pxmPaint.setAntiAlias(true);     //设置抗锯齿mPaint.setAlpha(255);        //设置画笔透明度}@Overrideprotected 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)@Overrideprotected 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之太极图相关推荐

  1. 一篇文章带你走近Android自定义view

    系列文章目录 一篇文章带你走近Android自定义view 文章目录 系列文章目录 前言 一.为什么要自定义view 二.先看看一个超级简单的自定义view(三个构造函数) 三.了解手机的坐标系 四. ...

  2. [自定义控件]android自定义view实战之太极图

    android自定义view实战之太极图 尊重原创,转载请注明出处: http://blog.csdn.net/qq137722697 自定义view是Android工程师进阶不可避免要接触的,我的学 ...

  3. Android自定义View —— TypedArray

    在上一篇中Android 自定义View Canvas -- Bitmap写到了TypedArray 这个属性 下面也简单的说一下TypedArray的使用 TypedArray 的作用: 用于从该结 ...

  4. Android 自定义View —— Canvas

    上一篇在android 自定义view Paint 里面 说了几种常见的Point 属性 绘制图形的时候下面总有一个canvas ,Canvas 是是画布 上面可以绘制点,线,正方形,圆,等等,需要和 ...

  5. android自定义view获取控件,android 自定义控件View在Activity中使用findByViewId得到结果为null...

    转载:http://blog.csdn.net/xiabing082/article/details/48781489 1.  大家常常自定义view,,然后在xml 中添加该view 组件..如果在 ...

  6. Android自定义View:ViewGroup(三)

    自定义ViewGroup本质是什么? 自定义ViewGroup本质上就干一件事--layout. layout 我们知道ViewGroup是一个组合View,它与普通的基本View(只要不是ViewG ...

  7. android 自定义图形,Android自定义View之图形图像(模仿360的刷新球自定

    概述: 360安全卫士的那个刷新球(姑且叫它刷新球,因为真的不知道叫什么好,不是dota里的刷新球!!),里面像住了水一样,生动可爱,看似简单,写起来不太简单,本例程只是实现了它的部分功能而已,说实话 ...

  8. android代码实现手机加速功能,Android自定义View实现内存清理加速球效果

    Android自定义View实现内存清理加速球效果 发布时间:2020-09-21 22:21:57 来源:脚本之家 阅读:105 作者:程序员的自我反思 前言 用过猎豹清理大师或者相类似的安全软件, ...

  9. android中仿qq最新版抽屉,Android 自定义View实现抽屉效果

    Android 自定义View实现抽屉效果 说明 这个自定义View,没有处理好多点触摸问题 View跟着手指移动,没有采用传统的scrollBy方法,而是通过不停地重新布局子View的方式,来使得子 ...

最新文章

  1. 试题 历届试题 带分数(全排列)
  2. 如何使用DNS反向映射来扫描IPv6地址?
  3. vue 字典_【开源】基于Vue的前端组件库HeyUI
  4. linux使用nc命令模拟客户端与服务器,测试连通性
  5. golang 框架_Golang:数据库ORM框架gorm详解
  6. 大数据实战之环境搭建(十)
  7. 开始用Flutter做游戏吧
  8. Qt网络应用----socket通信例子
  9. NetApp 数据存储解决方案
  10. 坐标正反算例题_坐标正算公式例题
  11. 程序员的职业危机是什么?一个12年互联网人的4点思考
  12. 仓央嘉措---不负如来不负卿---问佛--见与不见
  13. 【调参07】不平衡分类问题中分类权重计算与设置
  14. mysql column specified twice_Mysql抛出Column 'descriptions' specified twice异常解决方法
  15. 油猴脚本更改tw样式
  16. 关于ORA-12505, TNS:listener does not currently know of SID given in connect descript的一个解决思路
  17. 大华摄像头视频接入(一)
  18. 宏基因组分析步骤Linux,宏基因组分析专题研讨班
  19. 回忆过去回忆一下,也是一种幸福,一种美好
  20. 绘图计算机配置清单,主攻专业设计制图 5000元以下i5-7500独显电脑配置清单推荐...

热门文章

  1. SPAN /SPAN 这个标签有什么用 ,指的是什么?
  2. CAD制图初学入门:CAD软件中有哪些执行CAD命令的方式?
  3. 自动驾驶中车辆3Dbox检测相关论文
  4. 文件安全类产品深度剖析
  5. 永磁同步电机弱磁基本问题总结
  6. 端口映射与NAT负载均衡
  7. 怎么让计算机发出音乐的声音,如何让电脑声音更大一点
  8. TXT 文本阅读器源码
  9. iOS - 调用相机连续拍照
  10. win2003 服务器安全设置详细介绍