189 8069 5689

Android实现水波纹特效的方法

这篇文章将为大家详细讲解有关Android实现水波纹特效的方法,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

创新互联公司网站设计,为客户量身定制各类网站建设业务,包括企业型、电子商务型、响应式网站、行业门户型等各类网站,实战经验丰富,成功案例众多。以客户利益为出发点,创新互联公司网站制作为客户规划、按需网站制作符合企业需求、带有营销价值的网络建站方案认真对待每一个客户,我们不用口头的语言来吹擂我们的优秀,千余家的成功案例见证着我们的成长。

Android原生的技术利用动画或者自定义控件都可以实现,下面上个图类似于这样的效果

Android实现水波纹特效的方法

下面请看第一种动画实现,这种方式较为简单些,就是利用3个ImageView不断地做缩放和渐变的动画。

布局文件定义一下


  
  
  
  
  
  

接下来中间的ImageView保持不变,通过操作另外两个ImageView达到效果

private void setAnim1() {
  AnimationSet as = new AnimationSet(true);
  //缩放动画,以中心从原始放大到1.4倍
  ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 1.4f, 1.0f, 1.4f,
      ScaleAnimation.RELATIVE_TO_SELF, 0.5f,
      ScaleAnimation.RELATIVE_TO_SELF, 0.5f);
  //渐变动画
  AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.5f);
  scaleAnimation.setDuration(800);
  scaleAnimation.setRepeatCount(Animation.INFINITE);
  alphaAnimation.setRepeatCount(Animation.INFINITE);
  as.setDuration(800);
  as.addAnimation(scaleAnimation);
  as.addAnimation(alphaAnimation);
  iv1.startAnimation(as);
}
private void setAnim2() {
  AnimationSet as = new AnimationSet(true);
  //缩放动画,以中心从1.4倍放大到1.8倍
  ScaleAnimation scaleAnimation = new ScaleAnimation(1.4f, 1.8f, 1.4f, 1.8f,
      ScaleAnimation.RELATIVE_TO_SELF, 0.5f,
      ScaleAnimation.RELATIVE_TO_SELF, 0.5f);
  //渐变动画
  AlphaAnimation alphaAnimation = new AlphaAnimation(0.5f, 0.1f);
  scaleAnimation.setDuration(800);
  scaleAnimation.setRepeatCount(Animation.INFINITE);
  alphaAnimation.setRepeatCount(Animation.INFINITE);
  as.setDuration(800);
  as.addAnimation(scaleAnimation);
  as.addAnimation(alphaAnimation);
  iv2.startAnimation(as);
}

接下来就是第二种自定义动画实现

首先定义style文件自定义属性--在values下创建attrs.xml文件


  
  
  
  
  
  
  
  
  
  
  
  

接下来创建SpreadView继承view,初始化构造方法

public SpreadView(Context context) {
  this(context,null,0);
}
 
public SpreadView(Context context, @Nullable AttributeSet attrs) {
  this(context, attrs,0);
}
 
public SpreadView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
  super(context, attrs, defStyleAttr);
  TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SpreadView, defStyleAttr, 0);
  radius = a.getInt(R.styleable.SpreadView_spread_radius, radius);
  maxRadius = a.getInt(R.styleable.SpreadView_spread_max_radius, maxRadius);
  int centerColor = a.getColor(R.styleable.SpreadView_spread_center_color, ContextCompat.getColor(context, R.color.colorAccent));
  int spreadColor = a.getColor(R.styleable.SpreadView_spread_spread_color, ContextCompat.getColor(context, R.color.colorAccent));
  distance = a.getInt(R.styleable.SpreadView_spread_distance, distance);
  a.recycle();
  centerPaint = new Paint();
  centerPaint.setColor(centerColor);
  centerPaint.setAntiAlias(true);
  //最开始不透明且扩散距离为0
  alphas.add(255);
  spreadRadius.add(0);
  spreadPaint = new Paint();
  spreadPaint.setAntiAlias(true);
  spreadPaint.setAlpha(255);
  spreadPaint.setColor(spreadColor);
}

自定义View的绘制:

@Override
protected void onDraw(Canvas canvas) {
  super.onDraw(canvas);
  for (int i = 0; i < spreadRadius.size(); i++) {
    int alpha = alphas.get(i);
    spreadPaint.setAlpha(alpha);
    int width = spreadRadius.get(i);
    //绘制扩散的圆
    canvas.drawCircle(centerX, centerY, radius + width, spreadPaint);
    //每次扩散圆半径递增,圆透明度递减
    if (alpha > 0 && width < 300) {
      alpha = alpha - distance > 0 ? alpha - distance : 1;
      alphas.set(i, alpha);
      spreadRadius.set(i, width + distance);
    }
  }
  //当最外层扩散圆半径达到最大半径时添加新扩散圆
  if (spreadRadius.get(spreadRadius.size() - 1) > maxRadius) {
    spreadRadius.add(0);
    alphas.add(255);
  }
  //超过8个扩散圆,删除最先绘制的圆,即最外层的圆
  if (spreadRadius.size() >= 8) {
    alphas.remove(0);
    spreadRadius.remove(0);
  }
  //中间的圆
  canvas.drawCircle(centerX, centerY, radius, centerPaint);
  //延迟更新,达到扩散视觉差效果
  postInvalidateDelayed(delayMilliseconds);
}

最后在activity的布局文件中引用自定义属性:

关于“Android实现水波纹特效的方法”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。


网站题目:Android实现水波纹特效的方法
网站URL:http://cdxtjz.cn/article/jjiggs.html

其他资讯