123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- package com.xunao.effectdemo.view;
- import android.animation.ValueAnimator;
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.Canvas;
- import android.graphics.Paint;
- import android.os.Handler;
- import android.os.Message;
- import android.util.AttributeSet;
- import android.view.View;
- import android.view.animation.LinearInterpolator;
- import androidx.annotation.NonNull;
- import androidx.annotation.Nullable;
- /**
- * author : 程中强
- * e-mail : 740479946@qq.com
- * date : 2022/8/2615:10
- * desc :
- * version: 1.0
- */
- public class MyLikeView extends View {
- private Context context;
- private int ivResore;
- private int dotNum;
- private Paint mCircle;
- private Paint mBitmap;
- private int centerX;
- private int centerY;
- private Bitmap bitmap;
- private Float mBimt;
- private String canvasType;
- private Float circleR;
- private Float circleR0;
- private Float dotR;
- private int circleColor;
- private int[] dotColors;
- private int width;
- private int height;
- private Handler handler;
- public MyLikeView(@NonNull Context context) {
- this(context, (AttributeSet)null);
- }
- public MyLikeView(@NonNull Context context, @Nullable AttributeSet attrs) {
- this(context, attrs, -1);
- }
- public MyLikeView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
- super(context, attrs, defStyleAttr);
- this.dotNum = 7;
- this.mBimt = 0.0F;
- this.canvasType = "Bitmap";
- this.circleR = 0.0F;
- this.circleR0 = 0.0F;
- this.dotR = 10.0F;
- this.dotColors = new int[]{-2446854, -868533, -1852250, -13460755, -5117031, -9975379, -2200660};
- this.handler = new Handler() {
- public void handleMessage(Message msg) {
- super.handleMessage(msg);
- if (msg.what == 0) {
- MyLikeView.this.postInvalidate();
- }
- }
- };
- // TypedArray typedArray = context.obtainStyledAttributes(attrs, styleable.MyLikeView);
- // if (typedArray != null) {
- // this.ivResore = typedArray.getResourceId(styleable.MyLikeView_bitmap, mipmap.heart);
- // this.circleColor = typedArray.getColor(styleable.MyLikeView_circleColor, ContextCompat.getColor(context, color.colorPrimary));
- // this.dotNum = typedArray.getInteger(styleable.MyLikeView_dotNum, 7);
- // }
- //
- // typedArray.recycle();
- this.init(context);
- this.setOnClickListener(new OnClickListener() {
- public void onClick(View view) {
- MyLikeView.this.start();
- }
- });
- }
- protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
- super.onMeasure(widthMeasureSpec, heightMeasureSpec);
- if (this.bitmap != null) {
- this.width = this.bitmap.getWidth() * 4;
- this.height = this.bitmap.getWidth() * 4;
- this.setMeasuredDimension(this.width, this.height);
- }
- }
- private void init(Context context) {
- this.context = context;
- this.mCircle = new Paint();
- this.mCircle.setAntiAlias(true);
- this.mBitmap = new Paint();
- this.mBitmap.setAntiAlias(true);
- BitmapFactory.Options options = new BitmapFactory.Options();
- options.inScaled = false;
- this.bitmap = BitmapFactory.decodeResource(context.getResources(), this.ivResore, options);
- }
- protected void onDraw(Canvas canvas) {
- super.onDraw(canvas);
- this.centerX = this.getWidth() / 2;
- this.centerY = this.getHeight() / 2;
- String var2 = this.canvasType;
- byte var3 = -1;
- switch(var2.hashCode()) {
- case 68905:
- if (var2.equals("Dot")) {
- var3 = 2;
- }
- break;
- case 1990057295:
- if (var2.equals("Bitmap")) {
- var3 = 0;
- }
- break;
- case 2018617584:
- if (var2.equals("Circle")) {
- var3 = 1;
- }
- }
- switch(var3) {
- case 0:
- this.canvasHeart(canvas);
- break;
- case 1:
- this.canvasCircle(canvas);
- break;
- case 2:
- this.canvasDot(canvas);
- }
- }
- private void canvasDot(Canvas canvas) {
- this.canvasHeart(canvas);
- if (this.mBimt == 36.0F) {
- float angleA = 0.0F;
- float angleB = -0.15707964F;
- for(int i = 0; i < this.dotNum; ++i) {
- this.mCircle.setColor(this.dotColors[i]);
- canvas.drawCircle((float)((double)(this.circleR + 5.0F) * Math.sin((double)angleB)), (float)((double)(this.circleR + 5.0F) * Math.cos((double)angleB)), this.dotR, this.mCircle);
- angleA = (float)((double)angleA + 6.283185307179586D / (double)this.dotNum);
- canvas.drawCircle((float)((double)this.circleR * Math.sin((double)angleA)), (float)((double)this.circleR * Math.cos((double)angleA)), this.dotR - 2.0F, this.mCircle);
- angleB = (float)((double)angleB + 6.283185307179586D / (double)this.dotNum);
- }
- }
- }
- private void canvasCircle(Canvas canvas) {
- this.mCircle.setColor(this.circleColor);
- canvas.drawCircle((float)this.centerX, (float)this.centerY, this.circleR0, this.mCircle);
- }
- private void canvasHeart(Canvas canvas) {
- canvas.translate((float)this.centerX, (float)this.centerY);
- canvas.drawBitmap(this.bitmap, (float)(-this.bitmap.getWidth() / 2), (float)(-this.bitmap.getWidth() / 2), this.mBitmap);
- }
- public void start() {
- this.circleR = (float)this.bitmap.getWidth();
- this.circleR0 = 0.0F;
- this.dotR = 6.0F;
- ValueAnimator animator = ValueAnimator.ofFloat(new float[]{0.0F, 72.0F});
- animator.setDuration(500L);
- animator.setInterpolator(new LinearInterpolator());
- animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
- public void onAnimationUpdate(ValueAnimator animation) {
- Float value = (Float)animation.getAnimatedValue();
- if (value <= 36.0F) {
- MyLikeView.this.mBimt = value;
- MyLikeView.this.canvasType = "Circle";
- } else {
- MyLikeView.this.canvasType = "Dot";
- MyLikeView.this.circleR = MyLikeView.this.circleR + 1.0F;
- MyLikeView.this.dotR = MyLikeView.this.dotR - 0.2F;
- }
- MyLikeView.this.circleR0 = MyLikeView.this.circleR0 + 1.0F;
- if (value == 72.0F) {
- MyLikeView.this.mBimt = value / 2.0F;
- try {
- Thread.sleep(50L);
- (new Thread(MyLikeView.this.new progrssThread())).start();
- } catch (InterruptedException var4) {
- var4.printStackTrace();
- }
- }
- MyLikeView.this.invalidate();
- }
- });
- animator.start();
- }
- public void setIvResore(int ivResore) {
- this.ivResore = ivResore;
- }
- public void setCircleColor(int circleColor) {
- this.circleColor = circleColor;
- }
- public void setDotNum(int dotNum, int[] dotColors) {
- this.dotNum = dotNum;
- this.dotColors = dotColors;
- }
- public class progrssThread implements Runnable {
- public progrssThread() {
- }
- public void run() {
- while(MyLikeView.this.dotR > 0.0F) {
- try {
- Thread.sleep(9L);
- Message msg = Message.obtain();
- msg.what = 0;
- MyLikeView.this.dotR = MyLikeView.this.dotR - 0.3F;
- if (MyLikeView.this.circleR < 36.0F) {
- MyLikeView.this.circleR = MyLikeView.this.circleR + 1.0F;
- }
- MyLikeView.this.handler.sendMessage(msg);
- } catch (InterruptedException var2) {
- var2.printStackTrace();
- }
- }
- }
- }
- }
|