EnergyTree

Introduction: 仿蚂蚁森林收集能量(Collect forest energy animation)
More: Author   ReportBugs   
Tags:

仿蚂蚁森林收集能量

这是一张图片

使用方式

取出所需文件:module 文件夹下的 BallModel 和 TipsModel 实体类,以及 EnergyTree 控件类,还有所需 item 布局和资源文件

中心点说明

根据 EnergyTree 控件类的 collectAnimator(final View view,boolean isRun)方法,可知道球移动的终点是此布局的中心点,即 mHeight/2 andr mWidth/2-60 可以根据自己的需求改变中心点。

其他说明

isCollectBall 和 isCollectTips 分别可以控制 点击能量球或者 Tips 提示,是否可以移动消失

能力球最多可以有七颗

Tips 提示框最多 4 个

EnergyTree 类的 107 行(X 轴)以及 108 行(Y 轴),可以调节 Tips 提示框的 XY 轴位置

主要代码

public class MainActivity extends AppCompatActivity {

    private EnergyTree mWaterFlake;
    private List<BallModel> mBallList;
    private List<TipsModel> mTipsList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initData();
        mWaterFlake = findViewById(R.id.custom_view);
        Button mBtn = findViewById(R.id.btn);
        mBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mWaterFlake.setModelList(mBallList,mTipsList);
            }
        });
        mBtn.post(new Runnable() {
            @Override
            public void run() {
                mWaterFlake.setModelList(mBallList,mTipsList);
            }
        });

        mWaterFlake.isCollectTips(false);
        mWaterFlake.setOnBallItemListener(new EnergyTree.OnBallItemListener() {
            @Override
            public void onItemClick(BallModel ballModel) {
                Toast.makeText(MainActivity.this,"收取了"+ballModel.getValue()+"能量",Toast.LENGTH_SHORT).show();
            }
        });

        mWaterFlake.setOnTipsItemListener(new EnergyTree.OnTipsItemListener() {
            @Override
            public void onItemClick(TipsModel tipsModel) {
                Toast.makeText(MainActivity.this,tipsModel.getContent(),Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void initData() {
        mBallList = new ArrayList<>();
        mBallList.add(new BallModel("能量","5g"));
        mBallList.add(new BallModel("能量","7g"));
        mBallList.add(new BallModel("能量","15g"));
        mBallList.add(new BallModel("能量","1g"));
        mBallList.add(new BallModel("能量","2g"));
        mBallList.add(new BallModel("能量","9g"));
        mBallList.add(new BallModel("能量","9g"));
        mTipsList = new ArrayList<>();
        mTipsList.add(new TipsModel("Tips:缺水"));
        mTipsList.add(new TipsModel("Tips:风大"));
        mTipsList.add(new TipsModel("Tips:暴雨"));
        mTipsList.add(new TipsModel("Tips:干燥"));
    }

}

主布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@mipmap/background">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        >
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@mipmap/tree" />
    </LinearLayout>
    <com.bryant.EnergyTree
        android:id="@+id/custom_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="bottom"
        android:padding="10dp"
        >
        <Button
           android:id="@+id/btn"
           android:text="重置"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
        />
    </LinearLayout>
</RelativeLayout>

联系 QQ:961606042

Apps
About Me
GitHub: Trinea
Facebook: Dev Tools