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

Android 自定义view仿支付宝咻一咻功能

程序员文章站 2024-03-31 11:17:16
支付宝上有一个咻一咻的功能,就是点击图片后四周有水波纹的这种效果,今天也写一个类似的功能。 效果如下所示: 思路: 就是几个圆的半径不断在变大,这个可以使用动画缩...

支付宝上有一个咻一咻的功能,就是点击图片后四周有水波纹的这种效果,今天也写一个类似的功能。

效果如下所示:

Android 自定义view仿支付宝咻一咻功能

思路:

就是几个圆的半径不断在变大,这个可以使用动画缩放实现,还有透明动画

还有就是这是好几个圆,然后执行的动画有个延迟效果,其实这些动画是放在一起执行的,熟悉属性动画的知道已经给我们提供了同步执行动画和顺序执行动画的实现api,也会会有人说这几个view就是在ondraw()方法中画几个圆,可能会说我还要继承容器view去onlayout()方法中这些子view添加在某个特定的区域,当然这也是可以的,其实简单的就是以图片为中心(图片imageview在父view的中心),然后画圆,指定每个字view的宽和高就行了,具体看代码:

package com.zhifubaoxiuyixiu.view;
import android.animation.animator;
import android.animation.animatorset;
import android.animation.objectanimator;
import android.content.context;
import android.graphics.color;
import android.graphics.paint;
import android.util.attributeset;
import android.view.gravity;
import android.view.view;
import android.view.animation.acceleratedecelerateinterpolator;
import android.widget.framelayout;
import java.util.arraylist;
/**
 * created by admin on 2016/12/29.
 */
public class zhifubaoframelayout extends framelayout {
 private int ripplecolor = color.parsecolor("#0099cc");//水波纹的颜色
 private int radius = 0;//水波纹圆的半径
 private long anim_duration = 3000;//动画执行的时间
 private int water_ripple_count = 6;
 private int scale = 6;//动画缩放比例
 private long animdelay;//动画延迟的时间
 private paint paint;
 private animatorset animatorset;
 private arraylist<animator> animatorlist;
 private framelayout.layoutparams rippleparams;
 private arraylist<wateripple> rippleviewlist=new arraylist<wateripple>();
 private boolean isanimrunning = false;
 public zhifubaoframelayout(context context) {
  this(context,null);
 }
 public zhifubaoframelayout(context context, attributeset attrs) {
  this(context,attrs,0);
 }
 public zhifubaoframelayout(context context, attributeset attrs, int defstyleattr) {
  super(context, attrs, defstyleattr);
  initpaint();
  addchildview();
  initanim();
 }
 /**
  * 初始化动画
  */
 private void initanim() {
  animatorset = new animatorset();
  animatorset.setinterpolator(new acceleratedecelerateinterpolator());
  animatorlist=new arraylist<animator>();
  for(int i=0;i<rippleviewlist.size();i++){//几个水波纹
   final objectanimator scalexanimator = objectanimator.offloat(rippleviewlist.get(i), "scalex", 1.0f, scale);
   scalexanimator.setrepeatcount(objectanimator.infinite);
   scalexanimator.setrepeatmode(objectanimator.restart);
   scalexanimator.setstartdelay(i * animdelay);
   scalexanimator.setduration(anim_duration);
   animatorlist.add(scalexanimator);
   final objectanimator scaleyanimator = objectanimator.offloat(rippleviewlist.get(i), "scaley", 1.0f, scale);
   scaleyanimator.setrepeatcount(objectanimator.infinite);
   scaleyanimator.setrepeatmode(objectanimator.restart);
   scaleyanimator.setstartdelay(i * animdelay);
   scaleyanimator.setduration(anim_duration);
   animatorlist.add(scaleyanimator);
   final objectanimator alphaanimator = objectanimator.offloat(rippleviewlist.get(i), "alpha", 1.0f, 0f);
   alphaanimator.setrepeatcount(objectanimator.infinite);
   alphaanimator.setrepeatmode(objectanimator.restart);
   alphaanimator.setstartdelay(i * animdelay);
   alphaanimator.setduration(anim_duration);
   animatorlist.add(alphaanimator);
  }
  animatorset.playtogether(animatorlist);
 }
 /**
  * 添加水波纹子view
  */
 private void addchildview() {
  radius = 32;
  animdelay=350;
  rippleparams=new framelayout.layoutparams((int)(2*(radius)),(int)(2*(radius)));
  rippleparams.gravity = gravity.center;
  for(int i=0;i<water_ripple_count;i++){//几个水波纹
   wateripple rippleview=new wateripple(getcontext(),paint);
   addview(rippleview,rippleparams);
   rippleviewlist.add(rippleview);
  }
 }

 /**
  * 初始化画笔
  */
 private void initpaint() {
  paint = new paint();
  paint.setantialias(true);
  paint.setcolor(ripplecolor);
  paint.setstyle(paint.style.fill);
 }
 /**
  * 开启动画
  */
 public void startrippleanimation(){
  if(!isrunning()){
   for(wateripple wateripple:rippleviewlist){
    wateripple.setvisibility(visible);
   }
   animatorset.start();
   isanimrunning=true;
  }
 }
 /**
  * 动画停止运行
  */
 public void stopanimation(){
  if(isrunning()){
   animatorset.cancel();
   isanimrunning=false;
  }
 }
 /**
  * 判断是否动画在运行
  * @return
  */
 public boolean isrunning(){
  return isanimrunning;
 }
 /**
  * ui不可见时关闭动画
  * @param visibility
  */
 @override
 protected void onwindowvisibilitychanged(int visibility) {
  super.onwindowvisibilitychanged(visibility);
  if(visibility==view.invisible||visibility ==view.gone){
   stopanimation();
  }
 }
}

每个水波纹view

package com.zhifubaoxiuyixiu.view;
import android.content.context;
import android.graphics.canvas;
import android.graphics.paint;
import android.view.view;
/**
 * created by admin on 2016/12/29.
 */
public class wateripple extends view {
 private paint mpaint;
 public wateripple(context context, paint paint){
  super(context);
  if(paint==null){
   this.mpaint = new paint();
  }else{
   this.mpaint = paint;
  }
  setvisibility(view.invisible);//刚开始设置不可见
 }
 public wateripple(context context) {
  super(context);
 }
 @override
 protected void ondraw(canvas canvas) {
  super.ondraw(canvas);
  int radius=(math.min(getwidth(),getheight()))/2;
  canvas.drawcircle(radius,radius,radius,mpaint);
 }
}

圆形图片:

package com.zhifubaoxiuyixiu.view;
import android.content.context;
import android.graphics.bitmap;
import android.graphics.bitmapfactory;
import android.graphics.bitmapshader;
import android.graphics.canvas;
import android.graphics.paint;
import android.graphics.shader;
import android.graphics.drawable.shapedrawable;
import android.graphics.drawable.shapes.ovalshape;
import android.util.attributeset;
import android.view.view;
import com.zhifubaoxiuyixiu.r;
/**
 * created by admin on 2016/12/29.
 */
public class circleview extends view {
 private bitmapshader bitmapshaderp ;
 private shapedrawable shapedrawable;
 public circleview(context context) {
  this(context,null);
 }
 public circleview(context context, attributeset attrs) {
  this(context, attrs,0);
 }
 public circleview(context context, attributeset attrs, int defstyleattr) {
  super(context, attrs, defstyleattr);
  initbitmap();
 }
 private void initbitmap() {
  bitmap bitmap = bitmapfactory.decoderesource(getresources(), r.mipmap.grid);
  shapedrawable = new shapedrawable(new ovalshape());
  bitmapshaderp = new bitmapshader(bitmap, shader.tilemode.clamp,shader.tilemode.clamp);
  shapedrawable.getpaint().setshader(bitmapshaderp);
 }
 @override
 protected void ondraw(canvas canvas) {
  super.ondraw(canvas);
  shapedrawable.setbounds(0,0,getwidth(),getheight());
  shapedrawable.draw(canvas);
 }
}

布局文件:

<relativelayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 >
 <com.zhifubaoxiuyixiu.view.zhifubaoframelayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:id="@+id/root"
  >
  <com.zhifubaoxiuyixiu.view.circleview
   android:layout_width="64dp"
   android:layout_height="64dp"
   android:layout_centerinparent="true"
   android:id="@+id/imageview"
   android:layout_gravity="center"
   android:src="@mipmap/grid"/>
 </com.zhifubaoxiuyixiu.view.zhifubaoframelayout>
</relativelayout>

使用:

package com.zhifubaoxiuyixiu;
import android.app.activity;
import android.os.bundle;
import android.view.view;
import com.zhifubaoxiuyixiu.view.circleview;
import com.zhifubaoxiuyixiu.view.zhifubaoframelayout;
public class mainactivity extends activity {
 private circleview imageview;
 @override
 protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_main);
  imageview = (circleview) findviewbyid(r.id.imageview);
  final zhifubaoframelayout root = (zhifubaoframelayout) findviewbyid(r.id.root);
  imageview.setonclicklistener(new view.onclicklistener() {
   @override
   public void onclick(view view) {
    root.startrippleanimation();
   }
  });
 }
}

以上所述是小编给大家介绍的android 自定义view仿支付宝咻一咻功能,希望对大家有所帮助