添加依赖
implementation 'tyrant:heartlayout:1.0.1'
xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical">
<tyrantgit.widget.HeartLayout
android:id="@+id/heart_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#50FFFFFF"
android:text="1"/>
</tyrantgit.widget.HeartLayout>
</LinearLayout>
Activity中
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import tyrantgit.widget.HeartLayout;
public class MainActivity extends AppCompatActivity {
private Random mRandom = new Random();
private Timer mTimer = new Timer();
private HeartLayout mHeartLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mHeartLayout = (HeartLayout) findViewById(R.id.heart_layout);
mTimer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
mHeartLayout.post(new Runnable() {
@Override
public void run() {
mHeartLayout.addHeart(randomColor());
}
});
}
}, 500, 200);
}
@Override
protected void onDestroy() {
super.onDestroy();
mTimer.cancel();
}
private int randomColor() {
return Color.rgb(mRandom.nextInt(255), mRandom.nextInt(255), mRandom.nextInt(255));
}
}
网友评论