Android 仿qq 点赞功能
生活随笔
收集整理的這篇文章主要介紹了
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() {@Overridepublic void onAnimationStart(Animator animation) {}@Overridepublic void onAnimationEnd(Animator animation) {parentView.removeView(img);moveImages.remove(AnimaImageView.this);}@Overridepublic void onAnimationCancel(Animator animation) {}@Overridepublic void onAnimationRepeat(Animator animation) {}});set.setDuration(2000);set.start();}最終效果:
完整代碼:
https://github.com/XIAIBIANCHENG/qqthumb
總結
以上是生活随笔為你收集整理的Android 仿qq 点赞功能的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 我来谈谈为啥企业QQ下线
- 下一篇: 服务器系统安装蓝牙驱动,Atheros