欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2022-09-20 11:51:16
前言废话不多说直接开始老规矩,文章最后有源码完成效果图棋子加渐变色棋子不加渐变色一、测量1.获取宽高 @override protected void onsizechanged(int w, int...

前言

废话不多说直接开始

老规矩,文章最后有源码

完成效果图

棋子加渐变色

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

棋子不加渐变色

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 = mheight;
  }
 }

2.定义测量最小长度

将布局分为10份。以minwidth的1,3,5,7,9的倍数为标准点。

minwidth = usewidth / 10;

二、绘制背景(棋盘)

1.初始化画笔

 mpaint = new paint();  //创建画笔对象
  mpaint.setcolor(color.black); //设置画笔颜色
  mpaint.setstyle(paint.style.fill); //设置画笔模式为填充
  mpaint.setstrokewidth(4f);  //设置画笔宽度为10px
  mpaint.setantialias(true);  //设置抗锯齿
  mpaint.setalpha(255);  //设置画笔透明度

2.画棋盘

 //细的x轴
  canvas.drawline(minwidth, 3 * minwidth, 9 * minwidth, 3 * minwidth, mpaint);// 斜线
  canvas.drawline(minwidth, 5 * minwidth, 9 * minwidth, 5 * minwidth, mpaint);// 斜线
  canvas.drawline(minwidth, 7 * minwidth, 9 * minwidth, 7 * minwidth, mpaint);// 斜线
  //细的y轴
  canvas.drawline(3 * minwidth, minwidth, 3 * minwidth, 9 * minwidth, mpaint);// 斜线
  canvas.drawline(5 * minwidth, minwidth, 5 * minwidth, 9 * minwidth, mpaint);// 斜线
  canvas.drawline(7 * minwidth, minwidth, 7 * minwidth, 9 * minwidth, mpaint);// 斜线
  mpaint.setstrokewidth(8f);
  //粗的x轴(边框)
  canvas.drawline(minwidth, minwidth, 9 * minwidth, minwidth, mpaint);// 斜线
  canvas.drawline(minwidth, 9 * minwidth, 9 * minwidth, 9 * minwidth, mpaint);// 斜线
  //粗的y轴(边框)
  canvas.drawline(minwidth, minwidth, minwidth, 9 * minwidth, mpaint);// 斜线
  canvas.drawline(9 * minwidth, minwidth, 9 * minwidth, 9 * minwidth, mpaint);// 斜线

绘制完后,发现有点小瑕疵
效果图:

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

3.补棋盘瑕疵

canvas.drawpoint(minwidth, minwidth, mpaint);
  canvas.drawpoint(9 * minwidth, minwidth, mpaint);
  canvas.drawpoint(minwidth, 9 * minwidth, mpaint);
  canvas.drawpoint(9 * minwidth, 9 * minwidth, mpaint);

效果图:

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

三.画个不可改变的棋子(以便于了解动画移动位置)

位置比例
(3,3)(3,5)(3,7)
(5,3)(5,5)(5,7)
(7,3)(7,5)(7,7)

 //画围棋
  canvas.drawcircle(3*minwidth, 3*minwidth, usewidth/16, mpaint);
  canvas.drawcircle(3*minwidth, 7*minwidth, usewidth/16, mpaint);
  canvas.drawcircle(5*minwidth, 5*minwidth, usewidth/16, mpaint);
  canvas.drawcircle(7*minwidth, 3*minwidth, usewidth/16, mpaint);
  canvas.drawcircle(7*minwidth, 7*minwidth, usewidth/16, mpaint);
  mpaint.setcolor(rightcolor);
  canvas.drawcircle(3*minwidth, 5*minwidth, usewidth/16, mpaint);
  canvas.drawcircle(5*minwidth, 3*minwidth, usewidth/16, mpaint);
  canvas.drawcircle(5*minwidth, 7*minwidth, usewidth/16, mpaint);
  canvas.drawcircle(7*minwidth, 5*minwidth, usewidth/16, mpaint);

效果图:

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

四.为动画开始做准备以及动画

1.三个辅助类为动画做准备(参数模仿android官方demo)

主要为get set构造,代码会贴到最后

2.自定义该接口实例来控制动画的更新计算表达式

public class xyevaluator implements typeevaluator {
 public object evaluate(float fraction, object startvalue, object endvalue) {
  xyholder startxy = (xyholder) startvalue;
  xyholder endxy = (xyholder) endvalue;
  return new xyholder(startxy.getx() + fraction * (endxy.getx() - startxy.getx()),
    startxy.gety() + fraction * (endxy.gety() - startxy.gety()));
 }
}

3.棋子的创建

private shapeholder createball(float x, float y, int color) {
  ovalshape circle = new ovalshape();
  circle.resize(usewidth / 8f, usewidth / 8f);
  shapedrawable drawable = new shapedrawable(circle);
  shapeholder shapeholder = new shapeholder(drawable);
  shapeholder.setx(x - usewidth / 16f);
  shapeholder.sety(y - usewidth / 16f);
  paint paint = drawable.getpaint();
  paint.setcolor(color);
  return shapeholder;
 }

4.动画的创建

 private void createanimation() {
  if (bounceanim == null) {
   xyholder lstartxy = new xyholder(3 * minwidth - usewidth / 16f, 3 * minwidth - usewidth / 16f);
   xyholder processxy = new xyholder(7 * minwidth - usewidth / 16f, 3 * minwidth - usewidth / 16f);
   xyholder lendxy = new xyholder(7 * minwidth - usewidth / 16f, 7 * minwidth - usewidth / 16f);
   bounceanim = objectanimator.ofobject(ballholder, "xy",
     new xyevaluator(), lstartxy, processxy, lendxy, lstartxy);
   bounceanim.setduration(animaltime);
   bounceanim.setrepeatcount(objectanimator.infinite);
   bounceanim.setrepeatmode(objectanimator.restart);
   bounceanim.addupdatelistener(this);
  }
  if (bounceanim1 == null) {
   xyholder lstartxy = new xyholder(7 * minwidth - usewidth / 16f, 7 * minwidth - usewidth / 16f);
   xyholder processxy = new xyholder(3 * minwidth - usewidth / 16f, 7 * minwidth - usewidth / 16f);
   xyholder lendxy = new xyholder(3 * minwidth - usewidth / 16f, 3 * minwidth - usewidth / 16f);
   bounceanim1 = objectanimator.ofobject(ballholder1, "xy",
     new xyevaluator(), lstartxy, processxy, lendxy, lstartxy);
   bounceanim1.setduration(animaltime);
   bounceanim1.setrepeatcount(objectanimator.infinite);
   bounceanim1.setrepeatmode(objectanimator.restart);
   bounceanim1.addupdatelistener(this);
  }
 }

5.两个动画的同步执行

animatorset animatorset = new animatorset();
  animatorset.play(bounceanim).with(bounceanim1);
  animatorset.start();

6.效果图

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

视觉效果:感觉白子不太明显

7.解决第6步问题

在棋子的创建方法中添加渐变色

 radialgradient gradient = new radialgradient(usewidth / 16f, usewidth / 16f,
    usewidth / 8f, color, color.gray, shader.tilemode.clamp);
  paint.setshader(gradient);
  shapeholder.setpaint(paint);

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

五.自定义属性

attrs文件:

 <declare-styleable name="weiqiview">
<!--  黑子颜色-->
  <attr name="leftscolor" format="reference|color"/>
<!--  白子颜色-->
  <attr name="rightscolor" format="reference|color"/>
<!--  棋盘颜色-->
  <attr name="qipancolor" format="reference|color"/>
<!--  动画时间-->
  <attr name="animalstime" format="integer"/>
 </declare-styleable>

java文件中获取

 /**
  * 获取自定义属性
  */
 private void initcustomattrs(context context, attributeset attrs) {
  //获取自定义属性
  typedarray ta = context.obtainstyledattributes(attrs, r.styleable.weiqiview);
  //获取颜色
  leftcolor = ta.getcolor(r.styleable.weiqiview_leftscolor, color.black);
  rightcolor = ta.getcolor(r.styleable.weiqiview_rightscolor, color.white);
  qipancolor = ta.getcolor(r.styleable.weiqiview_qipancolor, color.black);
  //获取动画时间
  animaltime = ta.getint(r.styleable.weiqiview_animalstime, 2000);
  //回收
  ta.recycle();

 }

六.自定义属性设置后运行效果

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

七.小改变,视觉效果就不一样了!

然后,把背景注释,像不像那些等待动画?

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

八.源码

weiqiview.java

public class weiqiview extends view implements valueanimator.animatorupdatelistener {
 private paint mpaint;
 private int mwidth;
 private int mheight;
 private int usewidth, minwidth;
 private int leftcolor;
 private int rightcolor;
 private int qipancolor;
 private int animaltime;
 //画一个圆(棋子)
 valueanimator bounceanim, bounceanim1 = null;
 shapeholder ball, ball1 = null;
 qizixyholder ballholder, ballholder1 = null;

 @requiresapi(api = build.version_codes.lollipop)
 public weiqiview(context context) {
  this(context, null);
 }

 @requiresapi(api = build.version_codes.lollipop)
 public weiqiview(context context, @nullable attributeset attrs) {
  this(context, attrs, 0);
 }

 @requiresapi(api = build.version_codes.lollipop)
 public weiqiview(context context, @nullable attributeset attrs, int defstyleattr) {
  this(context, attrs, defstyleattr, 0);
  initcustomattrs(context, attrs);
 }

 @requiresapi(api = build.version_codes.lollipop)
 public weiqiview(context context, @nullable attributeset attrs, int defstyleattr, int defstyleres) {
  super(context, attrs, defstyleattr, defstyleres);


 }

 private void init() {
  initpaint();
 }

 /**
  * 获取自定义属性
  */
 private void initcustomattrs(context context, attributeset attrs) {
  //获取自定义属性。
  typedarray ta = context.obtainstyledattributes(attrs, r.styleable.weiqiview);
  //获取颜色
  leftcolor = ta.getcolor(r.styleable.weiqiview_leftscolor, color.black);
  rightcolor = ta.getcolor(r.styleable.weiqiview_rightscolor, color.white);
  qipancolor = ta.getcolor(r.styleable.weiqiview_qipancolor, color.black);
  animaltime = ta.getint(r.styleable.weiqiview_animalstime, 2000);
  //回收
  ta.recycle();

 }

 /**
  * 初始化画笔
  */
 private void initpaint() {
  mpaint = new paint();  //创建画笔对象
  mpaint.setcolor(color.black); //设置画笔颜色
  mpaint.setstyle(paint.style.fill); //设置画笔模式为填充
  mpaint.setstrokewidth(4f);  //设置画笔宽度为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);
  init();
  minwidth = usewidth / 10;
  mpaint.setcolor(qipancolor);
  if (ball == null) {
   ball = createball(3 * minwidth, 3 * minwidth, leftcolor);
   ballholder = new qizixyholder(ball);
  }
  if (ball1 == null) {
   ball1 = createball(7 * minwidth, 7 * minwidth, rightcolor);
   ballholder1 = new qizixyholder(ball1);
  }
  //细的x轴
  canvas.drawline(minwidth, 3 * minwidth, 9 * minwidth, 3 * minwidth, mpaint);// 斜线
  canvas.drawline(minwidth, 5 * minwidth, 9 * minwidth, 5 * minwidth, mpaint);// 斜线
  canvas.drawline(minwidth, 7 * minwidth, 9 * minwidth, 7 * minwidth, mpaint);// 斜线
  //细的y轴
  canvas.drawline(3 * minwidth, minwidth, 3 * minwidth, 9 * minwidth, mpaint);// 斜线
  canvas.drawline(5 * minwidth, minwidth, 5 * minwidth, 9 * minwidth, mpaint);// 斜线
  canvas.drawline(7 * minwidth, minwidth, 7 * minwidth, 9 * minwidth, mpaint);// 斜线
  mpaint.setstrokewidth(8f);
  //粗的x轴(边框)
  canvas.drawline(minwidth, minwidth, 9 * minwidth, minwidth, mpaint);// 斜线
  canvas.drawline(minwidth, 9 * minwidth, 9 * minwidth, 9 * minwidth, mpaint);// 斜线
  //粗的y轴(边框)
  canvas.drawline(minwidth, minwidth, minwidth, 9 * minwidth, mpaint);// 斜线
  canvas.drawline(9 * minwidth, minwidth, 9 * minwidth, 9 * minwidth, mpaint);// 斜线
  //补瑕疵
  canvas.drawpoint(minwidth, minwidth, mpaint);
  canvas.drawpoint(9 * minwidth, minwidth, mpaint);
  canvas.drawpoint(minwidth, 9 * minwidth, mpaint);
  canvas.drawpoint(9 * minwidth, 9 * minwidth, mpaint);
//  //画围棋
//  canvas.drawcircle(3*minwidth, 3*minwidth, usewidth/16, mpaint);
//  canvas.drawcircle(3*minwidth, 7*minwidth, usewidth/16, mpaint);
//  canvas.drawcircle(5*minwidth, 5*minwidth, usewidth/16, mpaint);
//  canvas.drawcircle(7*minwidth, 3*minwidth, usewidth/16, mpaint);
//  canvas.drawcircle(7*minwidth, 7*minwidth, usewidth/16, mpaint);
//  mpaint.setcolor(rightcolor);
//  canvas.drawcircle(3*minwidth, 5*minwidth, usewidth/16, mpaint);
//  canvas.drawcircle(5*minwidth, 3*minwidth, usewidth/16, mpaint);
//  canvas.drawcircle(5*minwidth, 7*minwidth, usewidth/16, mpaint);
//  canvas.drawcircle(7*minwidth, 5*minwidth, usewidth/16, mpaint);

  canvas.save();
  canvas.translate(ball.getx(), ball.gety());
  ball.getshape().draw(canvas);
  canvas.restore();

  canvas.save();
  canvas.translate(ball1.getx(), ball1.gety());
  ball1.getshape().draw(canvas);
  canvas.restore();
 }

 private shapeholder createball(float x, float y, int color) {
  ovalshape circle = new ovalshape();
  circle.resize(usewidth / 8f, usewidth / 8f);
  shapedrawable drawable = new shapedrawable(circle);
  shapeholder shapeholder = new shapeholder(drawable);
  shapeholder.setx(x - usewidth / 16f);
  shapeholder.sety(y - usewidth / 16f);
  paint paint = drawable.getpaint();
  paint.setcolor(color);
  radialgradient gradient = new radialgradient(usewidth / 16f, usewidth / 16f,
    usewidth / 8f, color, color.gray, shader.tilemode.clamp);
  paint.setshader(gradient);
  shapeholder.setpaint(paint);
  return shapeholder;
 }

 private void createanimation() {
  if (bounceanim == null) {
   xyholder lstartxy = new xyholder(3 * minwidth - usewidth / 16f, 3 * minwidth - usewidth / 16f);
   xyholder processxy = new xyholder(7 * minwidth - usewidth / 16f, 3 * minwidth - usewidth / 16f);
   xyholder lendxy = new xyholder(7 * minwidth - usewidth / 16f, 7 * minwidth - usewidth / 16f);
   bounceanim = objectanimator.ofobject(ballholder, "xy",
     new xyevaluator(), lstartxy, processxy, lendxy, lstartxy);
   bounceanim.setduration(animaltime);
   bounceanim.setrepeatcount(objectanimator.infinite);
   bounceanim.setrepeatmode(objectanimator.restart);
   bounceanim.addupdatelistener(this);
  }
  if (bounceanim1 == null) {
   xyholder lstartxy = new xyholder(7 * minwidth - usewidth / 16f, 7 * minwidth - usewidth / 16f);
   xyholder processxy = new xyholder(3 * minwidth - usewidth / 16f, 7 * minwidth - usewidth / 16f);
   xyholder lendxy = new xyholder(3 * minwidth - usewidth / 16f, 3 * minwidth - usewidth / 16f);
   bounceanim1 = objectanimator.ofobject(ballholder1, "xy",
     new xyevaluator(), lstartxy, processxy, lendxy, lstartxy);
   bounceanim1.setduration(animaltime);
   bounceanim1.setrepeatcount(objectanimator.infinite);
   bounceanim1.setrepeatmode(objectanimator.restart);
   bounceanim1.addupdatelistener(this);
  }
 }

 public void startanimation() {
  createanimation();
  animatorset animatorset = new animatorset();
  animatorset.play(bounceanim).with(bounceanim1);
  animatorset.start();
 }

 @override
 public void onanimationupdate(valueanimator animation) {
  invalidate();
 }
}

qizixyholder.java

public class qizixyholder {

 private shapeholder mball;

 public qizixyholder(shapeholder ball) {
  mball = ball;
 }

 public void setxy(xyholder xyholder) {
  mball.setx(xyholder.getx());
  mball.sety(xyholder.gety());
 }

 public xyholder getxy() {
  return new xyholder(mball.getx(), mball.gety());
 }
}

shapeholder.java

public class shapeholder {
 private float x = 0, y = 0;
 private shapedrawable shape;
 private int color;
 private radialgradient gradient;
 private float alpha = 1f;
 private paint paint;

 public void setpaint(paint value) {
  paint = value;
 }
 public paint getpaint() {
  return paint;
 }

 public void setx(float value) {
  x = value;
 }
 public float getx() {
  return x;
 }
 public void sety(float value) {
  y = value;
 }
 public float gety() {
  return y;
 }
 public void setshape(shapedrawable value) {
  shape = value;
 }
 public shapedrawable getshape() {
  return shape;
 }
 public int getcolor() {
  return color;
 }
 public void setcolor(int value) {
  shape.getpaint().setcolor(value);
  color = value;
 }
 public void setgradient(radialgradient value) {
  gradient = value;
 }
 public radialgradient getgradient() {
  return gradient;
 }

 public void setalpha(float alpha) {
  this.alpha = alpha;
  shape.setalpha((int)((alpha * 255f) + .5f));
 }

 public float getwidth() {
  return shape.getshape().getwidth();
 }
 public void setwidth(float width) {
  shape s = shape.getshape();
  s.resize(width, s.getheight());
 }

 public float getheight() {
  return shape.getshape().getheight();
 }
 public void setheight(float height) {
  shape s = shape.getshape();
  s.resize(s.getwidth(), height);
 }

 public shapeholder(shapedrawable s) {
  shape = s;
 }
}

xyevaluator.java

public class xyevaluator implements typeevaluator {
 public object evaluate(float fraction, object startvalue, object endvalue) {
  xyholder startxy = (xyholder) startvalue;
  xyholder endxy = (xyholder) endvalue;
  return new xyholder(startxy.getx() + fraction * (endxy.getx() - startxy.getx()),
    startxy.gety() + fraction * (endxy.gety() - startxy.gety()));
 }
}

xyholder.java

public class xyholder {
 private float mx;
 private float my;

 public xyholder(float x, float y) {
  mx = x;
  my = y;
 }

 public float getx() {
  return mx;
 }

 public void setx(float x) {
  mx = x;
 }

 public float gety() {
  return my;
 }

 public void sety(float y) {
  my = y;
 }
}

attrs.xml

<resources>
 <declare-styleable name="weiqiview">
<!--  黑子颜色-->
  <attr name="leftscolor" format="reference|color"/>
<!--  白子颜色-->
  <attr name="rightscolor" format="reference|color"/>
<!--  棋盘颜色-->
  <attr name="qipancolor" format="reference|color"/>
<!--  动画时间-->
  <attr name="animalstime" format="integer"/>
 </declare-styleable>
</resources>

布局调用

<com.shenzhen.jimeng.lookui.ui.weiqiview
 android:layout_centerinparent="true"
 android:id="@+id/weiqi"
 android:layout_width="400dp"
 android:layout_height="400dp"/>

activity文件中开启动画

weiqi = (weiqiview) findviewbyid(r.id.weiqi);
  weiqi.setonclicklistener(new view.onclicklistener() {
   @override
   public void onclick(view v) {
    weiqi.startanimation();
   }
  });

到此这篇关于android自定义view之围棋动画效果的实现的文章就介绍到这了,更多相关android自定义view围棋动画内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!