Android's imitation live broadcast like function special effects?

First of all, I also refer to other people's code for the code, but there are still some modifications, because when calling other people's code, some problems and bugs were also found. Not much nonsense, just go to the code.
The old rules, the picture above: the
Insert picture description here
first step, import the required framework:

           implementation 'com.github.kaisengao:KsgLikeView:1.0.1'

The second step is to set the interface UI. The overall UI is relatively simple. com.kaisengao.likeview.like.KsgLikeView needs to be implemented after the first step:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.kaisengao.likeview.like.KsgLikeView
        android:id="@+id/live_view"
        android:layout_width="120dp"
        android:layout_height="0dp"
        android:layout_marginTop="100dp"
        app:ksg_default_image="@drawable/heart0"
        app:ksg_enter_duration="1500"
        app:ksg_curve_duration="4500"
        app:layout_constraintDimensionRatio="H,1:4"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">

    </com.kaisengao.likeview.like.KsgLikeView>

    <Button
        android:id="@+id/live_view_single"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="发送一条"
        android:textColor="@android:color/black"
        android:textSize="14sp"
        app:layout_constraintLeft_toLeftOf="@id/live_view"
        app:layout_constraintRight_toRightOf="@id/live_view"
        app:layout_constraintTop_toBottomOf="@id/live_view"/>

    <Button
        android:id="@+id/live_view_more"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="发送一堆"
        android:textColor="@android:color/black"
        android:textSize="14sp"
        app:layout_constraintLeft_toLeftOf="@id/live_view"
        app:layout_constraintRight_toRightOf="@id/live_view"
        app:layout_constraintTop_toBottomOf="@id/live_view_single"/>

</androidx.constraintlayout.widget.ConstraintLayout>

Step 3: The main java code. The overall code is relatively simple and easy to understand. If you don't understand, you can ask me. The code is as follows:

import android.os.Handler;
import android.os.Looper;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

import com.kaisengao.likeview.like.KsgLikeView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    
    


    private KsgLikeView mLikeView;

    private Button mMore;
    static  int i=0;


    Handler mhandler=new Handler(Looper.getMainLooper());

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }

    private void initView() {
    
    

        mLikeView = findViewById(R.id.live_view);
        findViewById(R.id.live_view_single).setOnClickListener(this);
        mMore=findViewById(R.id.live_view_more);
        mMore .setOnClickListener(this);

        mLikeView.addLikeImage(R.drawable.heart0);
        mLikeView.addLikeImage(R.drawable.heart1);
        mLikeView.addLikeImage(R.drawable.heart2);
        mLikeView.addLikeImage(R.drawable.heart3);
        mLikeView.addLikeImage(R.drawable.heart4);
        mLikeView.addLikeImage(R.drawable.heart5);
        mLikeView.addLikeImage(R.drawable.heart6);
        mLikeView.addLikeImage(R.drawable.heart7);
        mLikeView.addLikeImage(R.drawable.heart8);
    }
    
//选择点赞方式
    @Override
    public void onClick(View v) {
    
    
        switch (v.getId()){
    
    
            case R.id.live_view_single:
                mLikeView.addFavor();
                break;
            case R.id.live_view_more:
                i=0;
                boolean selected = mMore.isSelected();
                if (selected){
    
    
                    mhandler.removeCallbacks(runnable);
                }else {
    
    
                    mhandler.postDelayed(runnable,100);
                }
                mMore.setSelected(!selected);
                break;

            default:
                break;
        }
    }
//通过设置i的数量控制爱心个数,我这里设置的是20个。
    private Runnable runnable=new Runnable() {
    
    
        @Override
        public void run() {
    
    
            if(i<20) {
    
    
                mLikeView.addFavor();
                i++;
            }
            Log.i("MainActivity.class","ieo="+i);
            mhandler.postDelayed(runnable,100);
        }
    };

    @Override
    protected void onDestroy() {
    
    
        super.onDestroy();
        mhandler.removeCallbacks(runnable);
    }
}

The fourth step is to import the drawable picture, and download the picture directly to the Baidu network disk. Importing it in it will be invalid:
Link: https://pan.baidu.com/s/19VA_y_yMSIBGyiRgvyvSzQ Extraction code: sx3j
Finally, you will be very simple To get this special effect, if you have any questions, you can go to GitHub to download
GitHub and click here . If you don’t know GitHub, you can also download it directly on the csdn. Anyway, I didn’t set the points CSDN and click here .
Writing a blog is time-consuming, you can give it a thumbs-up and encouragement. The next blog will write the SMS login verification function. If you want to learn, you can take a look.

Guess you like

Origin blog.csdn.net/qq_45137584/article/details/107836334