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

最终效果:

Android 仿qq 点赞功能_第1张图片


完整代码:

https://github.com/XIAIBIANCHENG/qqthumb

你可能感兴趣的:(Android)