Android 仿qq 点赞功能

闲来无事研究了下qq的点赞功能,qq点赞后会随机生成不同颜色的图片,生成方式如下:

 //生成不同颜色的点赞图片
    private Bitmap generateRandomColorBitmap(){
        int w,h;
        w=h=dip2px(this,20);
        Bitmap dst= Bitmap.createBitmap(w,h, Bitmap.Config.ARGB_8888);
        Bitmap src=BitmapFactory.decodeResource(getResources(),R.mipmap.pql).copy(Bitmap.Config.ARGB_8888, true);;

        Canvas dstCanvas=new Canvas(dst);
        Canvas srcCanvas=new Canvas(src);

        Paint rectPaint=new Paint();
        rectPaint.setStyle(Paint.Style.FILL);
        rectPaint.setColor(randomColors[getRandom(0,5)]);
        dstCanvas.drawRect(0,0,w,h,rectPaint);

        Paint p=new Paint();
        p.setStyle(Paint.Style.FILL);
        p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        srcCanvas.drawBitmap(dst,0,0,p);
        dst.recycle();
        return src;
    }

然后运动的轨迹是曲线的,可以用三次贝塞尔曲线生成轨迹路线代码如下:

/**
         * 三次贝塞尔曲线计算公式
         * B(t) = P0 * (1-t)^3 + 3 * P1 * t * (1-t)^2 + 3 * P2 * t^2 * (1-t) + P3 * t^3, t ∈ [0,1]
         * @param t  曲线长度比例
         * @param p0 起始点
         * @param p1 控制点1
         * @param p2 控制点2
         * @param p3 终止点
         * @return t对应的点
         */
        private  PointF CalculateBezierPointForCubic(float t, PointF p0, PointF p1, PointF p2, PointF p3) {
            PointF point = new PointF();
            float temp = 1 - t;
            point.x = p0.x * temp * temp * temp + 3 * p1.x * t * temp * temp + 3 * p2.x * t * t * temp + p3.x * t * t * t;
            point.y = p0.y * temp * temp * temp + 3 * p1.y * t * temp * temp + 3 * p2.y * t * t * temp + p3.y * t * t * t;
            return point;
        }

运行动过程当中的变化可以用属性动画实现代码如下:

 public void setScale(float v){
            img.setScaleY(v);
            img.setScaleX(v);
        }

        public void setPosition(float v){

            //计算位置的变化
            PointF p=CalculateBezierPointForCubic(v,p0,p1,p2,p3);
            img.setX(p.x);
            img.setY(p.y);
        }

        public void setAlpha(float v){
            img.setAlpha(v);
        }

        public void startAnimator(){
            set.playTogether(ObjectAnimator.ofFloat(this,"scale",0.4f,1.3f,1.0f));
            set.playTogether(ObjectAnimator.ofFloat(this,"position",0.0f,1.0f));
            set.playTogether(ObjectAnimator.ofFloat(this,"alpha",0.0f,1.0f,0.0f));
            set.setInterpolator(new LinearInterpolator());
            set.addListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animation) {}
                @Override
                public void onAnimationEnd(Animator animation) {
                    parentView.removeView(img);
                    moveImages.remove(AnimaImageView.this);
                }
                @Override
                public void onAnimationCancel(Animator animation) {}
                @Override
                public void onAnimationRepeat(Animator animation) {}
            });
            set.setDuration(2000);
            set.start();
        }

最终效果:



完整代码:

https://github.com/XIAIBIANCHENG/qqthumb

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值