一秒游戏
Posted cxj1821
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一秒游戏相关的知识,希望对你有一定的参考价值。
activity:
package com.example.onesecond;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView tvTime;
TextView tvNormal;
TextView tvGod;
TextView tvAction;
TextView tvDes;
LinearLayout llBtn;
TextView tvIndex;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findView();
initListener();
}
private void findView(){
tvTime = (TextView)findViewById(R.id.tv_time);
tvAction = (TextView)findViewById(R.id.tv_action);
tvNormal = (TextView)findViewById(R.id.tv_normal);
tvGod = (TextView)findViewById(R.id.tv_god);
tvDes = (TextView)findViewById(R.id.tv_des);
llBtn = (LinearLayout)findViewById(R.id.ll_btn);
tvIndex = (TextView)findViewById(R.id.tv_index);
}
long downTime;
long upTime;
int godTime;
int normalTime;
int index;
private void initListener(){
llBtn.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
//因为时间有多种,用于区分什么事件
if(event.getAction() == MotionEvent.ACTION_DOWN){index++;
tvIndex.setText("运行了"+index+"次");
downTime = System.currentTimeMillis();
setVisibleByDownToBtn();
}else if(event.getAction() == MotionEvent.ACTION_UP){
upTime = System.currentTimeMillis();
double time = calculateTime();
tvTime.setText(""+time);
//需要转换为字符串
if(Math.abs(time - 1 )> 0.1){
normalTime++;
tvNormal.setText("凡人"+normalTime);
setVisibleToBtn();
}else{
godTime++;
tvGod.setText("大神"+godTime);
setVisibleToBtn();
}
}else if(event.getAction() == MotionEvent.ACTION_MOVE){
}
return false;
}
});
}
private void setVisibleToBtn() {
tvAction.setVisibility(View.GONE);
tvDes.setVisibility(View.VISIBLE);
tvTime.setVisibility(View.VISIBLE);
}
private void setVisibleByDownToBtn() {
tvAction.setVisibility(View.VISIBLE);
tvDes.setVisibility(View.GONE);
tvTime.setVisibility(View.GONE);
}
private double calculateTime() {
return (double)(upTime - downTime)/1000;
}
}
layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="#0096fa">
<TextView
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="#000000"
android:gravity="center"
android:textSize="26dp"
android:text="你的一秒有多长呢?"
android:layout_alignParentTop="true"/>
<LinearLayout
android:id="@+id/ll_btn"
android:layout_width="200dp"
android:layout_height="100dp"
android:background="@color/colorPrimary"
android:clickable="true"
android:layout_centerInParent="true"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="@+id/tv_action"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="现在开始"
android:textSize="26dp"/>
<TextView
android:id="@+id/tv_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="10.92"
android:visibility="gone"
android:textSize="20dp"
/>
<TextView
android:id="@+id/tv_des"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="大侠六六六"
android:visibility="gone"
android:textSize="20dp"/>
</LinearLayout>
<TextView
android:id="@+id/tv_index"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="运行 0 次"
android:layout_below="@id/ll_btn"
android:layout_centerInParent="true"
android:layout_margin="20dp"/>
<ImageView
android:id="@+id/iv_common"
android:layout_width="20dp"
android:layout_height="20dp"
android:background="@drawable/ic_launcher_background"
android:layout_alignLeft="@id/ll_btn"
android:layout_below="@id/tv_index"
android:layout_marginTop="10dp"/>
<TextView
android:id="@+id/tv_normal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"