码迷,mamicode.com
首页 > 其他好文 > 详细

一秒游戏

时间:2019-12-08 17:35:02      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:ali   src   example   err   ack   long   ontouch   center   stat   

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"
android:text="凡人:0次"
android:layout_alignRight="@id/ll_btn"
android:layout_below="@id/tv_index"/>
<ImageView
android:id="@+id/iv_god"
android:layout_width="20dp"
android:layout_height="20dp"
android:background="@drawable/ic_launcher_background"
android:layout_alignLeft="@id/ll_btn"
android:layout_below="@id/iv_common"
android:layout_marginTop="10dp"/>
<TextView
android:id="@+id/tv_god"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="大神:0次"
android:layout_alignRight="@id/ll_btn"
android:layout_below="@id/tv_normal"
android:layout_marginTop="10dp"/>

</RelativeLayout>

 

运行效果:

技术图片

 

技术图片

 

 

 

一秒游戏

标签:ali   src   example   err   ack   long   ontouch   center   stat   

原文地址:https://www.cnblogs.com/cxj1821/p/12006262.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!