本来是要做iOS开发的,因为一些世事无常和机缘巧合与测试工作还有安卓系统结下了不解之缘,前不久找到了guess who 源码,又加入了一些自己的元素最终完成了这个简单的小游戏。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
> <ImageView android:id="@+id/mainmenu_information"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginRight="50dp"
android:src="@drawable/informationhelp" /> <LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="35dp"
> <LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
>
<!--主菜单-->
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginBottom="10dp"
>
<Button
style="@style/main_menu_button"
android:id="@+id/main_menu_play"
android:text="@string/mainmenu_play"/>
<Button
style="@style/main_menu_button"
android:id="@+id/main_menu_multi_play"
android:text="@string/mainmenu_multi_play"/>
<Button
style="@style/main_menu_button"
android:id="@+id/main_menu_options"
android:text="@string/mainmenu_options"/>
<Button
style="@style/main_menu_button"
android:id="@+id/main_menu_score"
android:text="@string/mainmenu_score"/>
<Button
style="@style/main_menu_button"
android:id="@+id/main_menu_facebook"
android:paddingLeft="25dp"
android:paddingRight="25dp"
android:drawableLeft="@drawable/sinaicon"
android:text="@string/mainmenu_facebook_fan_us"/>
<Button
style="@style/main_menu_button"
android:id="@+id/main_menu_multi_exit"
android:text="@string/mainmenu_exit"/> </LinearLayout>
<LinearLayout
android:orientation="horizontal"
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:layout_gravity="center"
android:src="@drawable/big_star"/>
</LinearLayout>
</LinearLayout>
<!--android:id="@+id/buttom_layout"-->
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:layout_weight="1"
> <TextView
android:id="@+id/high_run"
style="@style/MainMenuText"
android:text="@string/mainmenu_best_score"/>
<TextView
android:id="@+id/MainMenu_player_name"
style="@style/MainMenuText"
android:layout_marginLeft="5dp"
android:text="@string/mainmenu_best_player_name"/>
<!--android:layout_marginVertical="2dp"-->
<TextView
android:id="@+id/MainMenu_payer_score"
style="@style/MainMenuText"
android:textColor="#ff0000"
android:layout_width="40dp"
android:layout_marginRight="4dp"
android:gravity="right"
android:text="100"/>
<TextView
android:id="@+id/mainmenu_points"
style="@style/MainMenuText"
android:text="@string/mainmenu_points"/>
</LinearLayout> </LinearLayout> </LinearLayout> </LinearLayout>
package guess.bobo.cn.guesswho.activity; import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast; import java.util.Map; import guess.bobo.cn.guesswho.R;
import guess.bobo.cn.guesswho.service.MediaService;
import guess.bobo.cn.guesswho.service.PrefService;
import guess.bobo.cn.guesswho.service.SoundService;
import guess.bobo.cn.guesswho.utils.Constants;
import guess.bobo.cn.guesswho.utils.HighScoreDB; /**
* Created by Leon on 2018/6/2.
* Functions: 首页-主菜单页面
*/
public class MainMenuActivity extends Activity implements View.OnClickListener{ private SoundService sound;
private Button mainMenuPlay;
private Button mainMenuMultiPlay;
private Button mainMenuOptions;
private Button mainMenuScore;
private Button mainMenuFacebook;
private Button mainMenuMultiExit;
private ImageView mainmenu_information;
private TextView high_run;
private PrefService pref;
private TextView MainMenuPlayerName;
private TextView MainMenuPayerScore;
private TextView mainmenuPoints;
private boolean isExit; /**
* Auto-created on 2018-06-02 15:35:55 by Android Layout Finder
* (http://www.buzzingandroid.com/tools/android-layout-finder)
*/
private void findViews() {
mainMenuPlay = (Button)findViewById( R.id.main_menu_play );
mainMenuMultiPlay = (Button)findViewById( R.id.main_menu_multi_play );
mainMenuOptions = (Button)findViewById( R.id.main_menu_options );
mainMenuScore = (Button)findViewById( R.id.main_menu_score );
mainMenuFacebook = (Button) findViewById( R.id.main_menu_facebook );
mainMenuMultiExit = (Button)findViewById( R.id.main_menu_multi_exit );
mainmenu_information = (ImageView)findViewById(R.id.mainmenu_information);
high_run = (TextView)findViewById(R.id.high_run); MainMenuPlayerName = (TextView)findViewById( R.id.MainMenu_player_name );
MainMenuPayerScore = (TextView)findViewById( R.id.MainMenu_payer_score );
mainmenuPoints = (TextView)findViewById( R.id.mainmenu_points ); mainMenuPlay.setOnClickListener( this );
mainMenuMultiPlay.setOnClickListener( this );
mainMenuOptions.setOnClickListener( this );
mainMenuScore.setOnClickListener( this );
mainMenuFacebook.setOnClickListener( this );
mainMenuMultiExit.setOnClickListener( this );
mainmenu_information.setOnClickListener(this);
} @Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_menu);
pref = new PrefService(this);
//在app启动的第一个activity取出上次存储的语言设置一下不然会是中文(默认)
pref.changeLocaleAccordingOptions(this,pref.LANGUAGE_OPTION[ pref.getLanguage(this)]);
findViews();
} private void recreateLanguage() { if (Constants.ISUPDATE){
//刷新button的text适配国际化
mainMenuMultiPlay.setText(R.string.mainmenu_multi_play);
mainMenuFacebook.setText(R.string.mainmenu_facebook_fan_us);
mainMenuScore.setText(R.string.mainmenu_score);
mainMenuOptions.setText(R.string.mainmenu_options);
mainMenuMultiExit.setText(R.string.mainmenu_exit);
mainMenuPlay.setText(R.string.mainmenu_play);
high_run.setText(R.string.mainmenu_best_score);
mainmenuPoints.setText(R.string.mainmenu_points);
Constants.ISUPDATE = false;
}
} //onPause:与onResume配对,表示Activity正在暂停,正常情况下,onStop接着就会被调用
@Override
protected void onPause() {
super.onPause();
//释放音响资源
sound.release();
} //onResume:与onPause配对,表示Activity已经创建完成
@Override
protected void onResume() { Map map = HighScoreDB.getHighestPoint(this);
if (map != null && !map.isEmpty() && map.size() > 0){
MainMenuPlayerName.setText((String)map.get("PLAYERNAME"));
MainMenuPayerScore.setText(String.valueOf(map.get("POINTS")));
} //创建音响服务类
sound = new SoundService(this);
recreateLanguage();
super.onResume();
} //点击事件的处理
@Override
public void onClick(View v) { //只要是用户点击了按钮就播放按钮的声音 不用每次都设置
if (v instanceof Button || v instanceof ImageView){
MediaService.play(this,R.raw.button);
} //用户点击了开始
if ( v == mainMenuPlay ) {
Intent intent = new Intent(MainMenuActivity.this,PlayerNameActivity.class);
startActivity(intent); // 用户点击了多人游戏
} else if ( v == mainMenuMultiPlay ) {
Intent intent = new Intent(MainMenuActivity.this,MultiPlayActivity.class);
startActivity(intent); // 用户点击了选项
} else if ( v == mainMenuOptions ) {
Intent intent = new Intent(this,OptionsActivity.class);
startActivity(intent); // 用户点击了分数
} else if ( v == mainMenuScore ) {
Intent intent = new Intent(MainMenuActivity.this,RankingListActivity.class);
startActivity(intent); // 用户点击了微博
} else if ( v == mainMenuFacebook ) {
Intent intent = new Intent(MainMenuActivity.this,WeiboActivity.class);
startActivity(intent); // 用户点击了退出
} else if ( v == mainMenuMultiExit ) {
finish();
}else if ( v == mainmenu_information){
Intent intent = new Intent(this,InformationActivity.class);
startActivity(intent);
}
} //像其他软件一样连续点击2次退出
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (!isExit){
//播放音效
MediaService.play(this,R.raw.button);
isExit = true;
Toast.makeText(MainMenuActivity.this,"再按一次退出",Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
isExit = false;
}
},2000);
return true;
}
return super.onKeyDown(keyCode, event);
} }
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg">
<LinearLayout
android:orientation="horizontal"
android:layout_marginTop="80dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
>
<TextView
style="@style/PlayerNameFill"
android:layout_gravity="center"
android:text="@string/player_name_fill"/>
<guess.bobo.cn.guesswho.component.DashLineExitText
android:id="@+id/player_name"
android:layout_gravity="center"
android:layout_width="236dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textColor="#ffffff"
android:textSize="20sp"
android:textStyle="bold">
</guess.bobo.cn.guesswho.component.DashLineExitText>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:gravity="center">
<Button
android:id="@+id/player_name_menu"
style="@style/small_button"
android:text="@string/player_name_menu"/>
<Button
android:id="@+id/player_name_ok"
style="@style/small_button"
android:layout_marginRight="60dp"
android:layout_marginLeft="60dp"
android:text="@string/player_name_ok" />
<Button
android:id="@+id/player_name_skip"
style="@style/small_button"
android:text="@string/player_name_skip"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="30dp"
android:layout_marginRight="30dp"
android:gravity="bottom|right">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/logo"/>
</LinearLayout>
</LinearLayout>
package guess.bobo.cn.guesswho.activity; import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.Toast; import guess.bobo.cn.guesswho.R;
import guess.bobo.cn.guesswho.component.DashLineExitText;
import guess.bobo.cn.guesswho.service.MediaService; /**
* Created by Leon on 2018/6/2.
* Functions: 用户填写名字的页面
*/
public class PlayerNameActivity extends Activity implements View.OnClickListener{ private Button playerNameMenu;
private Button playerNameOk;
private Button playerNameSkip;
private DashLineExitText playerName; /**
* Find the Views in the layout<br />
* <br />
* Auto-created on 2018-06-03 13:11:44 by Android Layout Finder
* (http://www.buzzingandroid.com/tools/android-layout-finder)
*/
private void findViews() {
playerNameMenu = (Button)findViewById( R.id.player_name_menu );
playerNameOk = (Button)findViewById( R.id.player_name_ok );
playerNameSkip = (Button)findViewById( R.id.player_name_skip );
playerName = (DashLineExitText)findViewById(R.id.player_name); playerNameMenu.setOnClickListener( this );
playerNameOk.setOnClickListener( this );
playerNameSkip.setOnClickListener( this );
playerName.setOnClickListener( this );
} @Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.player_name);
findViews();
} /**
* 用户点击事件的处理
*/
@Override
public void onClick(View v) { //只要是用户点击了按钮就播放按钮的声音 不用每次都设置
if (v instanceof Button){
MediaService.play(this,R.raw.button);
} if ( v == playerNameMenu ) {
// 用户点击了菜单返回菜单页
finish(); } else if ( v == playerNameOk ) {
// 用户点击了ok
if (playerName == null || playerName.getText().toString().equals("")){
Toast.makeText(this,"姓名不可为空",Toast.LENGTH_SHORT).show();
}else if (playerName.getText().toString().length() > 7){
Toast.makeText(this,"姓名长度不能超过7个字节",Toast.LENGTH_SHORT).show();
}else {
Intent intent = new Intent(this, TapToStartActivity.class);
intent.putExtra("PLAYER", playerName.getText().toString());
startActivity(intent);
finish();
}
} else if ( v == playerNameSkip ) {
// 用户点击了跳过
if ( playerName.getText().toString().length() > 7){
Toast.makeText(this,"姓名长度不能超过7个字节",Toast.LENGTH_SHORT).show();
}else {
Intent intent = new Intent(this, TapToStartActivity.class);
intent.putExtra("PLAYER", playerName.getText().toString());
startActivity(intent);
finish();
} }else if ( v == playerName){
//用户点击了输入框这里先不做任何事在输出框类DashLineExitText中已经处理
}
} @Override
public void onBackPressed() {
super.onBackPressed();
MediaService.play(this,R.raw.button);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg"
android:orientation="vertical" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/hi_title"
android:textSize="24sp" android:textColor="#FFFFFF"
android:id="@+id/hi_title"
android:layout_margin="6dip"/>
<TextView android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="@string/hi_tap_to_start"
android:textSize="24sp"
android:textColor="#FF0000"
android:id="@+id/hi_tap_to_start"
android:gravity="center"
android:textStyle="bold"/>
</LinearLayout> <LinearLayout android:background="@drawable/bg_bottom"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="75dp"
android:layout_marginRight="75dp"
android:orientation="vertical"
android:layout_weight="1">
<LinearLayout android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_gravity="center">
<FrameLayout android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:layout_weight="1">
<TextView android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="@string/hi_sample_question"
android:textSize="18sp"
android:textColor="#FFFFFF"
android:layout_marginLeft="22dp"
android:id="@+id/hi_sample_question"
android:gravity="center"
android:layout_gravity="center_horizontal"/>
<!--这是那个隐藏的textView-->
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/hi_tip_msg"
android:textSize="14sp"
android:textColor="#FF8C00"
android:id="@+id/hi_tip_msg"
android:gravity="center"
android:layout_gravity="center_horizontal"
android:visibility="invisible"
android:lines="2"/>
</FrameLayout> <Button android:background="@drawable/tip_button_bg"
android:clickable="false"
android:layout_height="wrap_content"
android:layout_width="30dp"
android:text="@string/hi_tip_cnt"
android:textSize="14sp"
android:textColor="#FF8C00"
android:id="@+id/hi_tip_button"
android:gravity="center"
android:layout_gravity="right"/>
</LinearLayout> <LinearLayout android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal"> <ImageView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center_vertical"
android:src="@drawable/star_bigger"
android:layout_marginRight="16dp"
/> <FrameLayout android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center_vertical">
<TextView android:background="@drawable/w_bg1"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="@string/hi_sample_answer"
android:textSize="18sp"
android:textColor="#FFFFFF"
android:gravity="center"
android:layout_gravity="center_vertical"
/>
<ImageView android:background="@drawable/bn_jt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/hi_ans_button"
android:layout_marginTop="1dp" />
</FrameLayout>
</LinearLayout>
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/small_button_bg3"
android:textColor="#ffffff"
android:textSize="16sp"
android:id="@+id/hi_skip_button"
android:layout_gravity="right|bottom"
android:layout_marginRight="10dip"
android:layout_marginTop="5dip" />
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:orientation="vertical"
android:layout_weight="1"
android:layout_marginLeft="75dp"> <TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/hi_help_1"
android:textSize="18sp"
android:textColor="#FFFFFF" /> <TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/hi_help_2"
android:textSize="18sp"
android:textColor="#FFFFFF" /> <TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/hi_help_3"
android:textSize="18sp"
android:textColor="#FFFFFF" /> <ImageView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="right"
android:src="@drawable/logo"
android:layout_marginRight="10dip"/>
</LinearLayout> </LinearLayout>
package guess.bobo.cn.guesswho.activity; import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView; import guess.bobo.cn.guesswho.R;
import guess.bobo.cn.guesswho.service.MediaService;
import guess.bobo.cn.guesswho.utils.LogUtil; /**
* Created by Leon on 2018/6/9.
* Functions:
*/
public class TapToStartActivity extends Activity { private TextView hiTitle;
private TextView tapText;
private TextView questionText;
private TextView tipText;
private Button hiTipButton;
private ImageView ansButton;
private ImageView skipButton; private static final int MSG_ID_TIP_ANIM = 1;
private static final int MSG_ID_ANS_BUTTON_ANIM = 2;
private static final int MSG_ID_SKIP_BUTTON_ANIM = 3;
private static final int MSG_ID_TAP_TO_START_ANIM = 4;
private static final int MSG_ID_START_GAME = 5; private boolean isStartedGame = false;
private boolean isMoveBack = false;
private String playName; private Handler handler = new Handler(){ @Override
public void handleMessage(Message msg) {
super.handleMessage(msg); switch (msg.what){
case MSG_ID_TIP_ANIM:
tipText.setVisibility(View.VISIBLE);
Animation tipAnim = AnimationUtils.loadAnimation(TapToStartActivity.this,R.anim.tip_animation);
tipAnim.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
tipText.setVisibility(View.VISIBLE);
questionText.setVisibility(View.GONE);
} @Override
public void onAnimationEnd(Animation animation) {
tipText.setVisibility(View.GONE);
questionText.setVisibility(View.VISIBLE);
} @Override
public void onAnimationRepeat(Animation animation) { }
});
tipText.startAnimation(tipAnim);
sendEmptyMessageDelayed(MSG_ID_TIP_ANIM,6000);
break;
case MSG_ID_ANS_BUTTON_ANIM : if (isMoveBack == false){
Animation anim = new TranslateAnimation(
TranslateAnimation.RELATIVE_TO_PARENT,0.0f,
TranslateAnimation.RELATIVE_TO_PARENT,0.828f,
TranslateAnimation.RELATIVE_TO_PARENT,0.0f,
TranslateAnimation.RELATIVE_TO_PARENT,0.0f
);
anim.setRepeatCount(0);
anim.setDuration(3000);
ansButton.startAnimation(anim);
isMoveBack = true;
}else {
Animation anim2 = new TranslateAnimation(
TranslateAnimation.RELATIVE_TO_PARENT,0.828f,
TranslateAnimation.RELATIVE_TO_PARENT,0.0f,
TranslateAnimation.RELATIVE_TO_PARENT,0.0f,
TranslateAnimation.RELATIVE_TO_PARENT,0.0f
);
anim2.setRepeatCount(0);
anim2.setDuration(3000);
ansButton.startAnimation(anim2);
isMoveBack = false;
} sendEmptyMessageDelayed(MSG_ID_ANS_BUTTON_ANIM,3000);
break;
case MSG_ID_SKIP_BUTTON_ANIM:
Animation skipAnim = new AlphaAnimation(0.3f,1.0f);
skipAnim.setDuration(2000);
skipAnim.setRepeatMode(AlphaAnimation.REVERSE);
skipButton.startAnimation(skipAnim);
sendEmptyMessageDelayed(MSG_ID_SKIP_BUTTON_ANIM,3000);
break;
case MSG_ID_TAP_TO_START_ANIM:
Animation tapAnim = new AlphaAnimation(0.5f,1.0f);
tapAnim.setDuration(1600);
tapText.startAnimation(tapAnim);
sendEmptyMessageDelayed(MSG_ID_TAP_TO_START_ANIM,2000);
break;
case MSG_ID_START_GAME:
startGame();
finish();
}
}
}; /**
* (http://www.buzzingandroid.com/tools/android-layout-finder)
*/
private void findViews() {
hiTitle = (TextView)findViewById( R.id.hi_title );
tapText = (TextView)findViewById( R.id.hi_tap_to_start );
questionText = (TextView)findViewById( R.id.hi_sample_question );
tipText = (TextView)findViewById( R.id.hi_tip_msg );
hiTipButton = (Button)findViewById( R.id.hi_tip_button );
ansButton = (ImageView)findViewById( R.id.hi_ans_button );
skipButton = (ImageView)findViewById( R.id.hi_skip_button );
} @Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tap_to_start);
findViews(); playName = getIntent().getStringExtra("PLAYER");
if (playName.length() == 0 || playName.equals("")){
playName = "没有名字的用户";
} String hi = String.format(hiTitle.getText().toString(),playName);
hiTitle.setText(hi); handler.sendEmptyMessageDelayed(MSG_ID_TIP_ANIM,3000);
handler.sendEmptyMessage(MSG_ID_ANS_BUTTON_ANIM);
handler.sendEmptyMessage(MSG_ID_SKIP_BUTTON_ANIM);
handler.sendEmptyMessage(MSG_ID_TAP_TO_START_ANIM);
} @Override
public boolean onTouchEvent(MotionEvent event) { if (isStartedGame == false){
isStartedGame = true;
MediaService.play(this,R.raw.slide);
handler.sendEmptyMessage(MSG_ID_START_GAME);
} return true;
} //等着写这里可能会用到 playName 已经设置为全局变量请放心使用
private void startGame() {
Intent intent = new Intent(this,MainViewActivity.class);
intent.putExtra("PLAYER_MAME",playName);
startActivity(intent);
} @Override
protected void onDestroy() { if (handler != null){
handler.removeMessages(MSG_ID_TIP_ANIM);
handler.removeMessages(MSG_ID_ANS_BUTTON_ANIM);
handler.removeMessages(MSG_ID_SKIP_BUTTON_ANIM);
handler.removeMessages(MSG_ID_TAP_TO_START_ANIM);
handler.removeMessages(MSG_ID_START_GAME);
handler = null;
LogUtil.e("Activity销毁的时候移除了Handler");
} super.onDestroy();
} @Override
public void onBackPressed() {
super.onBackPressed();
MediaService.play(this,R.raw.button);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg"> <!--<FrameLayout android:layout_height="wrap_content"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_gravity="center">--> <!--<ImageView android:layout_height="wrap_content"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_marginTop="12dp"-->
<!--android:src="@drawable/bg_top"-->
<!--android:paddingTop="12dp"/>--> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:layout_marginTop="12dp"
android:background="@drawable/bg_top"
android:orientation="horizontal"> <ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/logo_mainview" /> <!--这个image没有用占位image-->
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" /> <ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="43dp"
android:layout_marginTop="17dp"
android:src="@drawable/clock" /> <TextView
android:id="@+id/main_view_min"
style="@style/MainViewTimer"
android:text="0"
android:textColor="#ff0000" /> <TextView
style="@style/MainViewTimer"
android:text="@string/main_view_unit_min" /> <TextView
android:id="@+id/mian_view_sec"
style="@style/MainViewTimer"
android:text="0"
android:textColor="#ff0000" /> <TextView
style="@style/MainViewTimer"
android:layout_marginRight="5dp"
android:text="@string/main_view_unit_sec" /> </LinearLayout>
<!--↑已经搞定-->
<!--下部分又分上下结构-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!--下部分上边-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:orientation="horizontal"> <!--<ImageView android:layout_height="wrap_content"-->
<!--android:layout_width="15dp"/>-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_bottom"
android:orientation="vertical"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"> <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"> <ImageView
android:id="@+id/mainview_person_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/business" /> <TextView
android:id="@+id/mainview_person_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Business"
android:textColor="#ffffff" /> </LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal"> <FrameLayout
android:layout_width="348dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"> <TextView
android:id="@+id/mainview_sample_question"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/hi_sample_question"
android:textColor="#FFFFFF"
android:textSize="18sp" /> <TextView
android:id="@+id/mainview_tip_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:lines="2"
android:text="@string/hi_tip_msg"
android:textColor="#FF8C00"
android:textSize="14sp"
android:visibility="invisible" /> </FrameLayout> <Button
android:id="@+id/mainview_tip_button"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:background="@drawable/tip_button"
android:gravity="center"
android:textAllCaps="false"
android:text="Tip\n0/3"
android:textColor="#FF8C00"
android:textSize="14sp" /> </LinearLayout> <TextView
android:id="@+id/mainview_person_decription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:lines="2"
android:text="Description"
android:textColor="#FFFFFF"
android:textSize="14sp" /> </LinearLayout> </LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center"
android:orientation="horizontal"> <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_weight="1"
android:orientation="vertical"> <TextView
android:id="@+id/mainview_player_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="Player:Leon"
android:textColor="#FF8C00"
android:textSize="14sp" /> <TextView
android:id="@+id/mainview_total_points"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="Points:0"
android:textColor="#FFFFFF"
android:textSize="14sp" /> </LinearLayout> <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="30dp"
android:layout_marginRight="10dp"
android:orientation="vertical"> <ImageView
android:layout_width="wrap_content"
android:layout_height="28dp"
android:layout_marginBottom="7dp"
android:src="@drawable/star_bigger" /> <ImageView
android:layout_width="wrap_content"
android:layout_height="28dp"
android:layout_gravity="center_vertical"
android:layout_marginBottom="7dp"
android:src="@drawable/star_bigger" /> <ImageView
android:layout_width="wrap_content"
android:layout_height="28dp"
android:layout_gravity="center_vertical"
android:layout_marginBottom="7dp"
android:src="@drawable/star_bigger" /> </LinearLayout> <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:orientation="vertical"> <FrameLayout
android:layout_width="250dp"
android:layout_height="35dp"
android:layout_gravity="center"> <TextView
android:id="@+id/mainview_answer_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/w_bg1"
android:gravity="center"
android:paddingLeft="35dp"
android:singleLine="true"
android:ellipsize="end"
android:text="@string/hi_sample_answer"
android:textColor="#FFFFFF"
android:textSize="18sp" />
<!--加上这个LinearLayout是为了做动画-->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<guess.bobo.cn.guesswho.component.SlidingButton
android:id="@+id/mainview_answer_1_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="1dp"
android:background="@drawable/bn_jt1"
android:gravity="center" />
</LinearLayout>
</FrameLayout> <FrameLayout
android:layout_width="250dp"
android:layout_height="35dp"
android:layout_gravity="center"> <TextView
android:id="@+id/mainview_answer_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/w_bg1"
android:gravity="center"
android:paddingLeft="35dp"
android:singleLine="true"
android:ellipsize="end"
android:text="@string/hi_sample_answer"
android:textColor="#FFFFFF"
android:textSize="18sp" />
<!--加上这个LinearLayout是为了做动画-->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<guess.bobo.cn.guesswho.component.SlidingButton
android:id="@+id/mainview_answer_2_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="1dp"
android:background="@drawable/bn_jt1"
android:gravity="center" />
</LinearLayout>
</FrameLayout> <FrameLayout
android:layout_width="250dp"
android:layout_height="35dp"
android:layout_gravity="center"> <TextView
android:id="@+id/mainview_answer_3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/w_bg1"
android:gravity="center"
android:paddingLeft="35dp"
android:singleLine="true"
android:ellipsize="end"
android:text="@string/hi_sample_answer"
android:textColor="#FFFFFF"
android:textSize="18sp" />
<!--加上这个LinearLayout是为了做动画-->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<guess.bobo.cn.guesswho.component.SlidingButton
android:id="@+id/mainview_answer_3_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="1dp"
android:background="@drawable/bn_jt1"
android:gravity="center" />
</LinearLayout>
</FrameLayout> </LinearLayout> </LinearLayout> </LinearLayout> <!--<ImageView android:layout_height="wrap_content"-->
<!--android:layout_width="15dp"/>-->
</LinearLayout> <!--↓ 底部-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:orientation="horizontal"> <ImageView
android:id="@+id/mainview_star_list1"
style="@style/MainViewStar2" /> <ImageView
android:id="@+id/mainview_star_list2"
style="@style/MainViewStar2" /> <ImageView
android:id="@+id/mainview_star_list3"
style="@style/MainViewStar" /> <ImageView
android:id="@+id/mainview_star_list4"
style="@style/MainViewStar" /> <ImageView
android:id="@+id/mainview_star_list5"
style="@style/MainViewStar" /> <ImageView
android:id="@+id/mainview_star_list6"
style="@style/MainViewStar" /> <ImageView
android:id="@+id/mainview_star_list7"
style="@style/MainViewStar" /> <ImageView
android:id="@+id/mainview_star_list8"
style="@style/MainViewStar" /> <ImageView
android:id="@+id/mainview_star_list9"
style="@style/MainViewStar" /> <ImageView
android:id="@+id/mainview_star_list10"
style="@style/MainViewStar" /> <!--占位image没有用只是为了占位用-->
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" /> <Button
android:id="@+id/mainview_skip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginRight="10dp"
android:background="@drawable/small_button"
android:text="@string/just_skip"
android:textColor="#ffffff"
android:textSize="10sp" /> <Button
android:id="@+id/mainview_end"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginRight="10dp"
android:background="@drawable/small_button"
android:text="@string/main_view_end"
android:textColor="#ffffff"
android:textSize="10sp" /> </LinearLayout> </LinearLayout> </LinearLayout>
package guess.bobo.cn.guesswho.activity; import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask; import guess.bobo.cn.guesswho.R;
import guess.bobo.cn.guesswho.component.SlidingButton;
import guess.bobo.cn.guesswho.modle.OneRoundGame;
import guess.bobo.cn.guesswho.modle.Question;
import guess.bobo.cn.guesswho.service.MediaService;
import guess.bobo.cn.guesswho.service.PrefService;
import guess.bobo.cn.guesswho.service.SoundService;
import guess.bobo.cn.guesswho.utils.DBUtil;
import guess.bobo.cn.guesswho.utils.HighScoreDB;
import guess.bobo.cn.guesswho.utils.LogUtil;
import guess.bobo.cn.guesswho.utils.PixelToDP; /**
* Created by Leon on 2018/6/10.
* Functions: 游戏-答题 界面
*/
public class MainViewActivity extends Activity implements View.OnClickListener{ private static final int MSG_ID_UPDATE_TIME = 1;
private static final int MSG_ID_CLOCK_NEXT_QUESTION = 2;
private static final int MSG_ID_SHOW_TIP = 3; private TextView mainViewMin;
private TextView mianViewSec;
private ImageView mainviewPersonIcon;
private TextView mainviewPersonType;
private TextView mainviewSampleQuestion;
private TextView mainviewTipMsg;
private Button mainviewTipButton;
private TextView mainviewPersonDecription;
private TextView mainviewPlayerName;
private TextView mainviewTotalPoints;
private TextView mainviewAnswer1;
private SlidingButton mainviewAnswer1Button;
private TextView mainviewAnswer2;
private SlidingButton mainviewAnswer2Button;
private TextView mainviewAnswer3;
private SlidingButton mainviewAnswer3Button;
private ImageView mainviewStarList1;
private ImageView mainviewStarList2;
private ImageView mainviewStarList3;
private ImageView mainviewStarList4;
private ImageView mainviewStarList5;
private ImageView mainviewStarList6;
private ImageView mainviewStarList7;
private ImageView mainviewStarList8;
private ImageView mainviewStarList9;
private ImageView mainviewStarList10;
//mainviewStarList[0] = mainviewStarList1;
private ImageView[] mainviewStarLists = new ImageView[10];
private Button mainviewSkip;
private Button mainviewEnd; private OneRoundGame game;
private String playName;
//提示消息播放动画的时候用户不能点击答题按钮
private boolean isNoAnmin = true; private List<OneRoundGame> allgames = new ArrayList<OneRoundGame>(); private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg); switch (msg.what){
case MSG_ID_UPDATE_TIME:
if (!game.isPaused()) {
game.increaseTimeUsed();
updateTimerUI();
}
sendEmptyMessageDelayed(MSG_ID_UPDATE_TIME, 1000);
break;
case MSG_ID_CLOCK_NEXT_QUESTION:
updateUIAccordingGameContent();
break;
case MSG_ID_SHOW_TIP:
mainviewTipMsg.setVisibility(View.VISIBLE);
Animation tipAnim = AnimationUtils.loadAnimation(MainViewActivity.this,R.anim.tip_animation);
tipAnim.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
mainviewTipMsg.setVisibility(View.VISIBLE);
mainviewSampleQuestion.setVisibility(View.GONE);
isNoAnmin = false;
} @Override
public void onAnimationEnd(Animation animation) {
mainviewTipMsg.setVisibility(View.GONE);
mainviewSampleQuestion.setVisibility(View.VISIBLE);
game.addTipCnt();
setUITip();
isNoAnmin = true;
} @Override
public void onAnimationRepeat(Animation animation) { }
});
mainviewTipMsg.startAnimation(tipAnim);
break; }
}
}; private void updateUIAccordingGameContent() {
//设置游戏者的名字:如果游戏者名字为空,则前面已经处理
mainviewPlayerName.setText("Player"+":"+playName); //设置当前问题的提示信息
setUITip(); //设置游戏当前的分数
mainviewTotalPoints.setText("Points:"+String.valueOf(game.getScore())); //根据玩家答对题目数量设置点亮星星的个数
for (int i = 0;i < mainviewStarLists.length;i++){
ImageView starImage = mainviewStarLists[i]; switch (game.getLevel()){
case 0:
if (i < game.getCorrectAnsCntForCurrentLevel()){//答对的题亮星
starImage.setImageResource(R.drawable.small_star1);
}else {//回答错误的暗星
starImage.setImageResource(R.drawable.small_star2);
}
break;
case 1:
if (i < game.getCorrectAnsCntForCurrentLevel() - game.LEVEL_1_CNT){//答对的题亮星
starImage.setImageResource(R.drawable.small_star1);
}else {//回答错误的暗星
starImage.setImageResource(R.drawable.small_star2);
}
break;
case 2:
if (i < game.getCorrectAnsCntForCurrentLevel() - game.LEVEL_2_CNT){//答对的题亮星
starImage.setImageResource(R.drawable.small_star1);
}else {//回答错误的暗星
starImage.setImageResource(R.drawable.small_star2);
}
break;
} } //设置当前题目,包括题目,提示,答案
Question question = game.getCurrentQuestion();
List<String> allAnswers = question.getAllAnswers();
mainviewSampleQuestion.setText(question.getQuestion());
mainviewAnswer1.setText(allAnswers.get(0));
mainviewAnswer2.setText(allAnswers.get(1));
mainviewAnswer3.setText(allAnswers.get(2));
mainviewPersonDecription.setText(question.getDescription()); //根据题目中人物的类型,设置任务图片和对应的图片标题
int presonType = game.getCurrentQuestion().getPersonType();
mainviewPersonIcon.setImageResource(personTypePic.get(presonType));
mainviewPersonType.setText(personTypeName.get(presonType));
} //设置当前问题的提示信息
private void setUITip() {
mainviewTipMsg.setText(game.getCurrentTip());
setTipButtonCount();
} private void updateTimerUI() {
mainViewMin.setText(String.valueOf(game.getTimeUsedInMin()));
mianViewSec.setText(String.valueOf(game.getTimeUsedInSec()));
} @SuppressLint("WrongViewCast")
private void findViews() {
mainViewMin = (TextView)findViewById( R.id.main_view_min );
mianViewSec = (TextView)findViewById( R.id.mian_view_sec );
mainviewPersonIcon = (ImageView)findViewById( R.id.mainview_person_icon );
mainviewPersonType = (TextView)findViewById( R.id.mainview_person_type );
mainviewSampleQuestion = (TextView)findViewById( R.id.mainview_sample_question );
mainviewTipMsg = (TextView)findViewById( R.id.mainview_tip_msg );
mainviewTipButton = (Button)findViewById( R.id.mainview_tip_button );
mainviewPersonDecription = (TextView)findViewById( R.id.mainview_person_decription );
mainviewPlayerName = (TextView)findViewById( R.id.mainview_player_name );
mainviewTotalPoints = (TextView)findViewById( R.id.mainview_total_points );
mainviewAnswer1 = (TextView)findViewById( R.id.mainview_answer_1 );
mainviewAnswer1Button = (SlidingButton)findViewById( R.id.mainview_answer_1_button );
mainviewAnswer2 = (TextView)findViewById( R.id.mainview_answer_2 );
mainviewAnswer2Button = (SlidingButton)findViewById( R.id.mainview_answer_2_button );
mainviewAnswer3 = (TextView)findViewById( R.id.mainview_answer_3 );
mainviewAnswer3Button = (SlidingButton)findViewById( R.id.mainview_answer_3_button );
mainviewStarList1 = (ImageView)findViewById( R.id.mainview_star_list1 );
mainviewStarList2 = (ImageView)findViewById( R.id.mainview_star_list2 );
mainviewStarList3 = (ImageView)findViewById( R.id.mainview_star_list3 );
mainviewStarList4 = (ImageView)findViewById( R.id.mainview_star_list4 );
mainviewStarList5 = (ImageView)findViewById( R.id.mainview_star_list5 );
mainviewStarList6 = (ImageView)findViewById( R.id.mainview_star_list6 );
mainviewStarList7 = (ImageView)findViewById( R.id.mainview_star_list7 );
mainviewStarList8 = (ImageView)findViewById( R.id.mainview_star_list8 );
mainviewStarList9 = (ImageView)findViewById( R.id.mainview_star_list9 );
mainviewStarList10 = (ImageView)findViewById( R.id.mainview_star_list10 );
mainviewSkip = (Button)findViewById( R.id.mainview_skip );
mainviewEnd = (Button)findViewById( R.id.mainview_end ); mainviewTipButton.setOnClickListener( this ); mainviewSkip.setOnClickListener( this );
mainviewEnd.setOnClickListener( this ); //主界面下方的星星,保存到数组中
mainviewStarLists[0] = mainviewStarList1;
mainviewStarLists[1] = mainviewStarList2;
mainviewStarLists[2] = mainviewStarList3;
mainviewStarLists[3] = mainviewStarList4;
mainviewStarLists[4] = mainviewStarList5;
mainviewStarLists[5] = mainviewStarList6;
mainviewStarLists[6] = mainviewStarList7;
mainviewStarLists[7] = mainviewStarList8;
mainviewStarLists[8] = mainviewStarList9;
mainviewStarLists[9] = mainviewStarList10;
} @Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_view);
findViews();
playName = getIntent().getStringExtra("PLAYER_MAME"); for(int i = 0; i<4;i++) {
OneRoundGame oneGame = new OneRoundGame(playName); List<Integer> questionIDs = DBUtil.getAllQuestionIdsInOneGame(this, OneRoundGame.LEVEL_1_COUNT,
OneRoundGame.LEVEL_2_COUNT,OneRoundGame.LEVEL_3_COUNT); for (int questionID : questionIDs) {
Question question = DBUtil.getQuestion(questionID);
oneGame.fillQuestions(question);
}
allgames.add(oneGame);
} game = allgames.get(0); startTimeRecoder();//开始计时器
startGame(); } private void startGame() {
//下一题
handler.sendEmptyMessage(MSG_ID_CLOCK_NEXT_QUESTION);
} @Override
protected void onPause() {//Activity “暂停”要做程序的暂停工作
super.onPause();
//设置游戏(计时器)暂停
game.setPaused(true); // 关闭游戏的背景音乐
MediaService.pause(this, R.raw.loop);
} @Override
protected void onResume() {//Activity “开始或恢复”要做程序的恢复工作
super.onResume();
//设置游戏(计时器)暂停后的恢复
game.setPaused(false);
// 打开游戏的背景音乐-启动计时器的时候打开这里就不用打开了
MediaService.play(this, R.raw.loop); //根据玩家答对题目数量设置点暗星星的个数
for (int i = 0;i < mainviewStarLists.length;i++){
ImageView starImage = mainviewStarLists[i];
starImage.setImageResource(R.drawable.small_star2);
}
} /**用户点击提示后使用提示的次数增加*/
private void setTipButtonCount() { if (game.getTipUsedCnt() == 0){
mainviewTipButton.setText("Tip\n0/3");
}else if (game.getTipUsedCnt() == 1){
mainviewTipButton.setText("Tip\n1/3");
}else if (game.getTipUsedCnt() == 2){
mainviewTipButton.setText("Tip\n2/3");
}else if (game.getTipUsedCnt() == 3){
mainviewTipButton.setText("Tip\n3/3");
} } private static final int DIALOG_ID_ANSWER_RIHGT = 1; protected Dialog onCreateDialog(int id) {
Dialog dialog = null;
switch (id) {
case DIALOG_ID_ANSWER_RIHGT:
// do the work to define the pause Dialog
break;
default:
dialog = null;
}
return dialog;
}
//计时器
private void startTimeRecoder() {
handler.sendEmptyMessageDelayed(MSG_ID_UPDATE_TIME,1000);
} @Override
public boolean onTouchEvent(MotionEvent event) {
// 用户选择了答案1 isNoAnmin提示信息是否播放完
if (isNoAnmin && mainviewAnswer1Button.handleParentTouchEvent(this,event)) {
selectAnswer(0);
}
// 用户选择了答案2 isNoAnmin提示信息是否播放完
if (isNoAnmin && mainviewAnswer2Button.handleParentTouchEvent(this, event)) {
selectAnswer(1);
}
// 用户选择了答案3 isNoAnmin提示信息是否播放完
if (isNoAnmin && mainviewAnswer3Button.handleParentTouchEvent(this, event)) {
selectAnswer(2);
} return super.onTouchEvent(event);
} /**
*点击事件的处理
*/
@Override
public void onClick(View v) { //只要是用户点击了按钮就播放按钮的声音 不用每次都设置
if (v instanceof Button){
MediaService.play(this,R.raw.button);
} if ( v == mainviewTipButton ) {
// 用户点击了提示按钮
handler.sendEmptyMessage(MSG_ID_SHOW_TIP);
}else if ( v == mainviewSkip ) {
// 用户点击了跳过
finish(); } else if ( v == mainviewEnd ) {
// 用户点击了结束
finish(); }
} private void selectAnswer(int useSelectedAnswerIndex) {
if (game.userSelectAnswer(useSelectedAnswerIndex)) {
MediaService.play(this, R.raw.right);
} else {
MediaService.play(this, R.raw.wrong);
} boolean isAllGameFinished = true; for(OneRoundGame game : allgames) {
if (!game.isFinish()) {
isAllGameFinished = false;
}else{
//用户打错题游戏结束
endTheGameAheadOfTime();
}
} if(game.isLevelUp()) {
//传递参数,调用升级界面。返回到主界面。
Intent intent = new Intent(this, PassLevelActivity.class);
intent.putExtra("LEVEL", game.getLevel());
intent.putExtra("PLAYER_NAME", game.getPlayerName());
this.startActivity(intent);
} int count = game.nextQuestionCount();
int i = allgames.indexOf(game);
//第一个game中的问题回答完了
if (count >= game.getQuestionCount() - 1){
if (i >= allgames.size() - 1){
//这里先不做任何处理原来是判断数组越界后面在game.nextQuestion()有处理
}else {
game = allgames.get(i++);
}
}
//Leon------------------------- //下一题时判断数组是否越界问题数组越界游戏结束
if (game.nextQuestion()){
//传递参数(Intent),调用结束界面。Finish主界面,
Intent intent = new Intent(this, UselessActivity.class);
intent.putExtra("PLAYER_MAME",playName);
Timer timer = new Timer();
TimerTask saveHighScorTask = new TimerTask(){ @Override
public void run() {
String timeUsed = game.getTimeUsedInMin() + ":" + game.getTimeUsedInSec();
HighScoreDB.save(MainViewActivity.this, game.getPlayerName(), timeUsed, game.getScore());
}}; timer.schedule(saveHighScorTask, 0);
startActivity(intent);
finish();
}
} /**用户打错题游戏结束*/
private void endTheGameAheadOfTime() { //传递参数(Intent),调用结束界面。Finish主界面,
Intent intent = new Intent(this,AheadOfTimeActivity.class);
intent.putExtra("PLAYER_MAME",playName);
Timer timer = new Timer();
TimerTask saveHighScorTask = new TimerTask(){ @Override
public void run() {
String timeUsed = game.getTimeUsedInMin() + ":" + game.getTimeUsedInSec();
HighScoreDB.save(MainViewActivity.this, game.getPlayerName(), timeUsed, game.getScore());
}}; timer.schedule(saveHighScorTask, 0); startActivity(intent);
finish();
} @Override
protected void onDestroy() { if (handler != null){
handler.removeMessages(MSG_ID_UPDATE_TIME);
handler.removeMessages(MSG_ID_CLOCK_NEXT_QUESTION);
handler.removeMessages(MSG_ID_SHOW_TIP);
handler = null;
LogUtil.e("MainViewActivity销毁的时候移除了Handler");
} super.onDestroy();
}
//缓存人物类型和对应图片,字符串资源
private static Map<Integer,Integer> personTypePic = new HashMap<Integer, Integer>();
static {
personTypePic.put(1,R.drawable.music);
personTypePic.put(2,R.drawable.sports);
personTypePic.put(3,R.drawable.movies);
personTypePic.put(4,R.drawable.politics);
} private static Map<Integer,Integer> personTypeName = new HashMap<Integer, Integer>();
static {
personTypeName.put(1,R.string.persion_type_name_1);
personTypeName.put(2,R.string.persion_type_name_2);
personTypeName.put(3,R.string.persion_type_name_3);
personTypeName.put(4,R.string.persion_type_name_4);
} }
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg">
<TextView
android:id="@+id/language_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="20dp"
android:layout_marginBottom="15dp"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#ffffff"
android:text="@string/options_title"/>
<TableLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:stretchColumns="0"
android:background="@drawable/options_box_bg">
<TableRow
android:layout_weight="1"
android:layout_marginLeft="20dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/options_language"
android:paddingTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical = "true"
android:textSize="20sp"
android:textColor="#ffffff"
android:text="@string/options_language"/>
<LinearLayout
android:paddingTop="10dp"
android:layout_marginTop="20dp"
android:layout_marginRight="40dp"
android:id="@+id/options_music1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical = "true"
android:layout_alignParentRight = "true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/left_arrow_black"/>
<TextView
android:id="@+id/options_languages"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textSize="17sp"
android:textColor="#ffffff"
android:background="#ff0000"
android:text="@string/options_languagei"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/right_arrow_black"/>
</LinearLayout>
</RelativeLayout>
</TableRow>
<ImageView
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/options_line"/>
<TableRow
android:layout_weight="1"
android:layout_marginLeft="20dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/text_music"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical = "true"
android:textSize="20sp"
android:textColor="#ffffff"
android:text="@string/options_music"/>
<ToggleButton
android:layout_marginRight="40dp"
android:id="@+id/options_music"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:layout_centerVertical = "true"
android:layout_alignParentRight = "true"
android:textAllCaps="false"
android:textOn="@string/options_on"
android:textOff="@string/options_off"
android:background="@drawable/toggle_button">
</ToggleButton>
</RelativeLayout>
</TableRow>
<ImageView
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/options_line"/>
<TableRow
android:layout_weight="1"
android:layout_marginLeft="20dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/text_sound"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical = "true"
android:textSize="20sp"
android:textColor="#ffffff"
android:text="@string/options_sound"/>
<ToggleButton
android:layout_marginRight="40dp"
android:id="@+id/options_sound"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:layout_centerVertical = "true"
android:layout_alignParentRight = "true"
android:textAllCaps="false"
android:textOn="@string/options_on"
android:textOff="@string/options_off"
android:background="@drawable/toggle_button">
</ToggleButton>
</RelativeLayout>
</TableRow> </TableLayout>
<Button
android:id="@+id/options_mainmenu"
android:layout_width ="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/small_button"
android:textColor="#ffffff"
android:textSize="16sp"
android:layout_marginLeft="20dp"
android:layout_marginTop="15dp"
android:text="@string/player_name_menu"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginBottom="5dp"
android:layout_marginRight="30dp"
android:src="@drawable/logo"/> </LinearLayout>
package guess.bobo.cn.guesswho.activity; import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.ToggleButton; import guess.bobo.cn.guesswho.R;
import guess.bobo.cn.guesswho.service.MediaService;
import guess.bobo.cn.guesswho.service.PrefService;
import guess.bobo.cn.guesswho.service.SoundService; /**
* Created by Leon on 2018/6/3.
* Functions:
*/
public class OptionsActivity extends Activity implements View.OnClickListener,View.OnTouchListener,GestureDetector.OnGestureListener{ private ToggleButton optionsMusic;
private ToggleButton optionsSound;
private Button optionsMainmenu;
private PrefService pref;
//方法二选择语言的变量
private GestureDetector gestureDetector;
private TextView languageText;
//方法一选择语言的变量
private boolean isLanguageTouched;
private TextView language_title;
private TextView options_language;
private TextView options_music;
private TextView text_sound;
private int oldX;
//这里要用static不然一离开这个activity i就变成0了
// private static int i; /**
* Find the Views in the layout<br />
* <br />
* Auto-created on 2018-06-03 19:58:48 by Android Layout Finder
* (http://www.buzzingandroid.com/tools/android-layout-finder)
*/
private void findViews() {
optionsMusic = (ToggleButton)findViewById( R.id.options_music );
optionsSound = (ToggleButton)findViewById( R.id.options_sound );
optionsMainmenu = (Button)findViewById( R.id.options_mainmenu );
languageText = (TextView)findViewById(R.id.options_languages);
language_title = (TextView)findViewById(R.id.language_title);
options_language = (TextView)findViewById(R.id.options_language);
options_music = (TextView)findViewById(R.id.text_music);
text_sound = (TextView)findViewById(R.id.text_sound);
//创建保存开关状态参数的类-接下来校验
pref = new PrefService(this); //ToggleButton Checked状态的校验
optionsMusic.setChecked(pref.isGameMusic());
optionsSound.setChecked(pref.isGameSound());
optionsMusic.setOnClickListener( this );
optionsSound.setOnClickListener( this );
optionsMainmenu.setOnClickListener( this ); //方法一:
// languageText.setOnTouchListener(new View.OnTouchListener() {
// @Override
// public boolean onTouch(View v, MotionEvent event) {
// isLanguageTouched = true;
//
// //注意这里返回false返回true就代表把这个事件吃掉了
// return false;
// }
// }); //方法二
gestureDetector = new GestureDetector(this);
languageText.setOnTouchListener(this);
} @Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.options);
findViews();
} /**
*用户点击事件的处理
*/
@Override
public void onClick(View v) { //只要是用户点击了按钮就播放按钮的声音 不用每次都设置
if (v instanceof Button){
MediaService.play(this,R.raw.button);
} if ( v == optionsMusic ) {
// 用户点击了游戏音乐
pref.setGameMusic(((ToggleButton) v).isChecked(),this); } else if ( v == optionsSound ) {
// 用户点击了游戏声音(背景音乐)
pref.setGameSound(((ToggleButton) v).isChecked(),this); } else if ( v == optionsMainmenu ) {
// 用户点击了菜单(返回菜单)
finish();
}
}
//方法一
// @Override
// public boolean onTouchEvent(MotionEvent event) {
//
// if (isLanguageTouched){
// if (event.getAction() == MotionEvent.ACTION_DOWN){
// oldX = (int) event.getX();
// }else if (event.getAction() == MotionEvent.ACTION_UP){
// if (oldX > event.getX()){
// handleLanguageOption(true);
// }
// if (oldX < event.getX()){
// handleLanguageOption(false);
// }
// isLanguageTouched = false;
// }
// }
//
// return false;
// } private void handleLanguageOption(boolean isLeft) { int i = pref.getLanguage(this); if (isLeft){ i--;
//负数纠正
if (i < 0){
// i = i * -1;
i = 2;
}
// i = (i - 1) % 3;
}else { i++;
//大数纠正
if (i == 3){
// i = i * -1;
i = 0;
}
// i = (i + 1) % 3;
} // //负数纠正
// if (i < 0){
// // i = i * -1;
// i = i + 3;
// }
//languageText.setText(pref.LANGUAGE_OPTION[i]); //设置长久存储游戏语言
pref.setLanguage(i,this);
MediaService.play(this,R.raw.slide); //更新UI上的语言
updateLanguageUi();
} private void updateLanguageUi() {
pref.changeLocaleAccordingOptions(this,pref.LANGUAGE_OPTION[ pref.getLanguage(this)]);
languageText.setText(R.string.options_languagei);
language_title.setText(R.string.options_title);
optionsMainmenu.setText(R.string.player_name_menu);
options_language.setText(R.string.options_language);
options_music.setText(R.string.options_music);
text_sound.setText(R.string.options_sound);
} @Override
public void onBackPressed() {
super.onBackPressed();
MediaService.play(this,R.raw.button);
} @Override
public boolean onDown(MotionEvent e) {
return false;
} @Override
public void onShowPress(MotionEvent e) { } @Override
public boolean onSingleTapUp(MotionEvent e) {
return false;
} @Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
return false;
} @Override
public void onLongPress(MotionEvent e) { } @Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if (e1.getX() - e2.getX() > 20){
handleLanguageOption(true);
}else if (e2.getX() - e1.getX() > 20){
handleLanguageOption(false);
}
return false;
} @Override
public boolean onTouch(View v, MotionEvent event) {
//如果用户点击的是选择语言的按按钮就处理
if (v.getId() == R.id.options_languages) {
gestureDetector.onTouchEvent(event);
return true;
}
return false;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/bg">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:textColor="#ffffff"
android:textSize="20sp"
android:layout_gravity="center"
android:text="@string/ranking"/> <LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp">
<TextView
android:id="@+id/line_one_name"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:text=""/>
<TextView
android:id="@+id/line_one_score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:gravity="center"
android:layout_weight="1"
android:text=""/>
<TextView
android:id="@+id/line_one_position"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ff0000"
android:text=""/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp">
<TextView
android:id="@+id/line_two_name"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:text=""/>
<TextView
android:id="@+id/line_two_score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:gravity="center"
android:layout_weight="1"
android:text=""/>
<TextView
android:id="@+id/line_two_position"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ff0000"
android:text=""/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp">
<TextView
android:id="@+id/line_three_name"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:text=""/>
<TextView
android:id="@+id/line_three_score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#ffffff"
android:layout_weight="1"
android:text=""/>
<TextView
android:id="@+id/line_three_position"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ff0000"
android:text=""/>
</LinearLayout>
<TextView
android:id="@+id/nodataye"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="20sp"
android:textColor="#ffffff"
android:visibility="visible"
android:text="@string/nodatayet"/> </LinearLayout>
package guess.bobo.cn.guesswho.activity; import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView; import java.util.Map; import guess.bobo.cn.guesswho.R;
import guess.bobo.cn.guesswho.modle.RankingListModle;
import guess.bobo.cn.guesswho.service.MediaService;
import guess.bobo.cn.guesswho.utils.HighScoreDB;
import guess.bobo.cn.guesswho.utils.LogUtil; /**
* Created by Leon on 2018/7/3.
* Functions: 分数排行榜
*/
public class RankingListActivity extends Activity { private TextView lineOneName;
private TextView lineOneScore;
private TextView lineOnePosition;
private TextView lineTwoName;
private TextView lineTwoScore;
private TextView lineTwoPosition;
private TextView lineThreeName;
private TextView lineThreeScore;
private TextView lineThreePosition;
private TextView nodataye; /**
* Find the Views in the layout<br />
* <br />
* Auto-created on 2018-07-03 15:31:11 by Android Layout Finder
* (http://www.buzzingandroid.com/tools/android-layout-finder)
*/
private void findViews() {
lineOneName = (TextView)findViewById( R.id.line_one_name );
lineOneScore = (TextView)findViewById( R.id.line_one_score );
lineOnePosition = (TextView)findViewById( R.id.line_one_position );
lineTwoName = (TextView)findViewById( R.id.line_two_name );
lineTwoScore = (TextView)findViewById( R.id.line_two_score );
lineTwoPosition = (TextView)findViewById( R.id.line_two_position );
lineThreeName = (TextView)findViewById( R.id.line_three_name );
lineThreeScore = (TextView)findViewById( R.id.line_three_score );
lineThreePosition = (TextView)findViewById( R.id.line_three_position );
nodataye = (TextView)findViewById( R.id.nodataye );
} @Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ranking_list); findViews();
} //onResume:与onPause配对,表示Activity已经创建完成
@Override
protected void onResume() { RankingListModle rankingModle = HighScoreDB.getHighestAll(this); if (rankingModle.getPlayerNames() != null && rankingModle.getPlayerNames().size() > 0) {
//数据库中有数据隐藏提示
nodataye.setVisibility(View.GONE); //第一名处理
String playName1 = rankingModle.getPlayerNames().get(0);
if (playName1 != null && playName1.length() != 0) {
lineOneName.setText(playName1);
lineOneScore.setText(String.valueOf(rankingModle.getPlayerScores().get(0)));
lineOnePosition.setText("1");
} if ( rankingModle.getPlayerNames().size() > 1) {
//第2名处理
String playName2 = rankingModle.getPlayerNames().get(1);
if (playName2 != null && playName2.length() != 0) {
lineTwoName.setText(playName2);
lineTwoScore.setText(String.valueOf(rankingModle.getPlayerScores().get(1)));
lineTwoPosition.setText("2");
}
} if ( rankingModle.getPlayerNames().size() > 2) {
//第3名处理
String playName3 = rankingModle.getPlayerNames().get(2);
if (playName3 != null && playName3.length() != 0) {
lineThreeName.setText(playName3);
lineThreeScore.setText(String.valueOf(rankingModle.getPlayerScores().get(2)));
lineThreePosition.setText("3");
}
} }else {
//数据库中没有数据提示用户
nodataye.setVisibility(View.VISIBLE);
} super.onResume();
} @Override
public void onBackPressed() {
super.onBackPressed();
MediaService.play(this,R.raw.button);
} }
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="@+id/weibo_view"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/temp_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
android:gravity="center"
android:background="#cc000000">
<ProgressBar
android:id="@+id/progressbar"
android:layout_width="80dp"
android:layout_height="80dp"
android:max="100"
android:progress="0"
android:visibility="gone" />
</RelativeLayout>
</WebView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="@+id/goBack"
android:layout_marginLeft="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/web_leftbutton"/>
<Button
android:id="@+id/goForward"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/web_rightbutton"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:id="@+id/reload_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/renovate_button"/>
</LinearLayout> </LinearLayout>
package guess.bobo.cn.guesswho.activity; import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.RelativeLayout; import guess.bobo.cn.guesswho.R;
import guess.bobo.cn.guesswho.service.MediaService; /**
* Created by Leon on 2018/7/5.
* Functions:
*/
public class WeiboActivity extends Activity implements View.OnClickListener { private WebView weiboView;
private ProgressBar progressbar;
private RelativeLayout tempLayout;
private Button goBack;
private Button goForward;
private Button reloadBtn; private void finndView(){
weiboView = (WebView)findViewById(R.id.weibo_view);
progressbar = (ProgressBar)findViewById(R.id.progressbar);
tempLayout = (RelativeLayout)findViewById(R.id.temp_layout);
goBack = (Button)findViewById(R.id.goBack);
goForward = (Button)findViewById(R.id.goForward);
reloadBtn = (Button)findViewById(R.id.reload_btn); goBack.setOnClickListener(this);
goForward.setOnClickListener(this);
reloadBtn.setOnClickListener(this);
} @Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.weibo_view); finndView();
setWeiBoView();
} @Override
public void onClick(View v) { //只要是用户点击了按钮就播放按钮的声音 不用每次都设置
if (v instanceof Button){
MediaService.play(this,R.raw.button);
} if (v == goBack){//用户点击了返回
if (weiboView.canGoBack()){
goBack.setEnabled(true);
weiboView.goBack();
}else {
goBack.setEnabled(false);
}
}else if (v == goForward){
if (weiboView.canGoForward()){
goForward.setEnabled(true);
weiboView.goForward();
}else {
goForward.setEnabled(false);
}
}else if (v == reloadBtn){
weiboView.reload();
}
} private void setWeiBoView(){ WebSettings webSettings = weiboView.getSettings(); //设置自适应屏幕,两者合用
webSettings.setUseWideViewPort(true); //将图片调整到适合webview的大小
webSettings.setLoadWithOverviewMode(true); // 缩放至屏幕的大小 //设置WebView属性,能够执行Javascript脚本
webSettings.setJavaScriptEnabled(true);
//设置可以访问文件
//webSettings.setAllowFileAccess(true); //支持缩放,默认为true。
webSettings .setSupportZoom(false);
//调整图片至适合webview的大小
webSettings .setUseWideViewPort(true);
// 缩放至屏幕的大小
webSettings .setLoadWithOverviewMode(true);
//设置默认编码
webSettings .setDefaultTextEncodingName("utf-8");
//设置自动加载图片
webSettings .setLoadsImagesAutomatically(true); // 设置WebView的客户端
weiboView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;// 返回false
}
}); weiboView.loadUrl("http://www.cnblogs.com/henanleon/");
} @Override
public void onBackPressed() {
super.onBackPressed();
MediaService.play(this,R.raw.button);
} //WebViewClient主要帮助WebView处理各种通知、请求事件
private WebViewClient webViewClient=new WebViewClient(){
@Override
public void onPageFinished(WebView view, String url) {//页面加载完成
progressbar.setVisibility(View.GONE);
tempLayout.setVisibility(View.GONE);
} @Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {//页面开始加载
progressbar.setVisibility(View.VISIBLE);
tempLayout.setVisibility(View.VISIBLE);
} @Override
public boolean shouldOverrideUrlLoading(WebView view, String url) { return super.shouldOverrideUrlLoading(view, url);
} }; @Override
protected void onDestroy() {
if (weiboView != null){
//释放资源
weiboView.destroy();
weiboView=null;
}
super.onDestroy();
} }
还有一些模型类,工具类,和一些不重要的页面这里就不一一介绍了,有兴趣的小伙伴联系我我可以将代码分享给你。