栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

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

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap

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;

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 animatorList;
 private frameLayout.LayoutParams rippleParams;
 private ArrayList rippleViewList=new ArrayList();
 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();
  for(int i=0;i

每个水波纹view

package com.zhifubaoxiuyixiu.view;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.View;

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;

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

布局文件:


 
  
 

使用:

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仿支付宝咻一咻功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对考高分网网站的支持!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://mshxw.com/it/160357.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号