大家可能不知道《奇怪的大冒险》这款游戏,这款游戏界面非常简单,设计方面特别无厘头,而且非常搞笑。在此目的不是要向大家推荐这个游戏,而是利用自己的Android知识来模仿这款游戏的界面,界面很简单,但是其中的一个关键点让我感触颇深。
针对游戏中头像不断变化的设计我提出自己的观点,在主UI线程中最好不用for循环来一直刷新,因为这样会堵塞主线程。我使用了定时器Timer和TimeTask来每隔500毫秒刷新界面改变背景头像。
具体代码如下:
1、界面XML代码:
<?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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/startup_bkg_gray"
tools:context="com.example.app_damaoxian.MainActivity">
<ImageView
android:src="@drawable/startup_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
android:clickable="true"
android:id="@+id/iv_begaingame"
android:layout_width="150dp"
android:layout_height="75dp"
android:background="@drawable/background_begaingame"
android:layout_centerVertical="true"
android:layout_alignEnd="@+id/iv_title" />
<ImageView
android:clickable="true"
android:id="@+id/iv_title"
android:scaleType="centerInside"
android:layout_centerHorizontal="true"
android:layout_above="@id/iv_begaingame"
android:padding="10dp"
android:src="@drawable/startup_lbl_title_zh"
android:layout_width="350dp"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/iv_option"
android:scaleType="fitCenter"
android:clickable="true"
android:background="@drawable/background_option"
android:layout_width="140dp"
android:layout_height="67dp"
android:layout_below="@+id/iv_begaingame"
android:layout_toStartOf="@+id/iv_about"
android:layout_marginTop="20dp" />
<ImageView
android:id="@+id/iv_about"
android:scaleType="fitCenter"
android:clickable="true"
android:background="@drawable/background_about"
android:layout_width="100dp"
android:layout_height="66dp"
android:layout_alignTop="@+id/iv_option"
android:layout_alignEnd="@+id/iv_begaingame" />
<ImageView
android:id="@+id/iv_photo_background"
android:layout_width="140dp"
android:layout_height="150dp"
android:src="@drawable/startup_photo_border"
android:layout_below="@+id/iv_title"
android:layout_toStartOf="@+id/iv_option" />
<ImageView
android:clickable="true"
android:id="@+id/iv_photo_broder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/background_photobroder"
android:layout_alignBottom="@+id/iv_photo_background"
android:layout_alignStart="@+id/iv_title"
android:layout_marginBottom="26dp" />
</RelativeLayout>
2、界面Activity的java代码:注意在使用Timer时不能直接改变UI,需要使用Hander来简洁改变UI
package com.example.app_damaoxian;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
private ImageView iv_photo_broder,iv_begaingame,iv_option;
private RelativeLayout Relayout_StartMain;
private Timer timer0;
private TimerTask timerTask0;
private Handler hander0;
private int count0=0;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Relayout_StartMain=(RelativeLayout)findViewById(R.id.activity_main);
Relayout_StartMain.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
initView();
// textView=(TextView)findViewById(R.id.tv_number);
}
private void initView(){
/**
* 利用Timer和TimerTask实现hander对UI线程的更新,每500毫秒一次
*/
iv_photo_broder=(ImageView) findViewById(R.id.iv_photo_broder);
timer0=new Timer();
hander0=new Handler(){
@Override
public void handleMessage(Message msg) {
//textView.setText(""+msg.what);
switch (msg.what){
case 1:
iv_photo_broder.setImageResource(R.drawable.startup_herface1);
break;
case 2:
iv_photo_broder.setImageResource(R.drawable.startup_herface2);
break;
case 3:
iv_photo_broder.setImageResource(R.drawable.startup_herface3);
break;
default:
break;
}
super.handleMessage(msg);
}
};
timerTask0=new TimerTask() {
@Override
public void run() {
count0++;
Message message=new Message();
message.what=count0;
hander0.sendMessage(message);
if (count0==3){
count0=0;
}
}
};
timer0.schedule(timerTask0,1000,500);
/**
* 定义开始按钮接口
*/
iv_begaingame=(ImageView)findViewById(R.id.iv_begaingame);
iv_begaingame.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent mintent=new Intent(MainActivity.this,Fragment_GameMenuActivity.class);
startActivity(mintent);
overridePendingTransition(R.animator.fragment_in,R.animator.fragment_out);
}
});
/**
* 定义选项按钮接口
*/
iv_option=(ImageView)findViewById(R.id.iv_option);
iv_option.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent mintent=new Intent(MainActivity.this,Activity_option.class);
startActivity(mintent);
overridePendingTransition(R.animator.fragment_in,R.animator.fragment_out);
}
});
}
/**
* 初始化一些用户设定,以及读取部分数据
*/
void initialization(){
}
@Override
protected void onDestroy() {
if (timer0!=null){
timer0.cancel();
timer0=null;
}
super.onDestroy();
}
}
3、AndroidManifest.xml代码,其中实现了全屏和横屏
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.app_damaoxian">
<application
android:name=".App_Data"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.NoActionBar">
<activity
android:name=".MainActivity"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Fragment_GameMenuActivity"
android:screenOrientation="landscape" />
<activity
android:name=".Activity_option"
android:screenOrientation="landscape" />
<activity android:name=".Activity_game_0_0"
android:screenOrientation="landscape"></activity>
</application>
</manifest>
我已经在Github中上传了源代码,欢迎star https://github.com/zqljintu/qiguaidedamaoxian 点击打开链接