【文件属性】:
文件名称:android 五子棋源码
文件大小:8.95MB
文件格式:ZIP
更新时间:2016-11-17 04:11:30
android 五子棋源码
package com.example.wzq;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import com.example.wzq.MyInfo.MyInfoActivity;
import com.example.wzq.gameview.WzqGame;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Parcelable;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ImageView.ScaleType;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener{
private ViewPager viewPager;
private List imageViews; //滑动的图片集合
private String[] titles;//图片标题
private int[] imageResId;//图片ID
private List dots; //图片标题正文的那些点集合
private TextView tv_title;
private int currentItem = 0;//当前图片的索引号
Intent intent = new Intent();
private ScheduledExecutorService scheduledExecutorService;
//切换到当前显示的图片
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
viewPager.setCurrentItem(currentItem);
}
};
//定义两种颜色设置
static final int COLOR1 = Color.parseColor("#787878");
static final int COLOR2 = Color.parseColor("#ffffff");
//定义四个按钮切换按钮
private Button start_btn=null;
private Button bluetooth_btn=null;
private Button myinfo_btn=null;
private Button finish_btn=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//生成滚动图片
initImageFlow();
View view_below=findViewById(R.id.below_view);
Const.currentid=1;
new Change(MainActivity.this,view_below);
initBtns();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
/*
* 初始化按钮
* */
public void initBtns(){
start_btn=(Button)findViewById(R.id.main_start_btn);
start_btn.setOnClickListener(this);
bluetooth_btn=(Button)findViewById(R.id.main_bluegame_btn);
bluetooth_btn.setOnClickListener(this);
myinfo_btn=(Button)findViewById(R.id.main_mynotes_btn);
myinfo_btn.setOnClickListener(this);
finish_btn=(Button)findViewById(R.id.main_finish_btn);
finish_btn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.main_start_btn:
Toast.makeText(this, "main_start_btn", Toast.LENGTH_SHORT).show();
intent.setClass(getApplicationContext(), WzqGame.class);
startActivity(intent);
break;
case R.id.main_bluegame_btn:
Toast.makeText(this, "main_bluegame_btn", Toast.LENGTH_SHORT).show();
break;
case R.id.main_mynotes_btn:
Toast.makeText(this, "main_mynotes_btn", Toast.LENGTH_SHORT).show();
intent.setClass(getApplicationContext(),MyInfoActivity.class);
startActivity(intent);
break;
case R.id.main_finish_btn:
Toast.makeText(this, "main_finish_btn", Toast.LENGTH_SHORT).show();
finish();
break;
}
}
/*
* 图片切换功能
* */
private void initImageFlow() {
//放置图片资源
imageResId = new int[] { R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.e };
titles = new String[imageResId.length];
titles[0] = "第一个";
titles[1] = "第二个";
titles[2] = "第三个";
titles[3] = "第四个";
titles[4] = "第五个";
imageViews = new ArrayList();
//初始化图片资源
for(int i = 0; i();
dots.add(findViewById(R.id.v_dot0));
dots.add(findViewById(R.id.v_dot1));
dots.add(findViewById(R.id.v_dot2));
dots.add(findViewById(R.id.v_dot3));
dots.add(findViewById(R.id.v_dot4));
tv_title = (TextView)findViewById(R.id.tv_title);
tv_title.setText(titles[0]);
//填充viewPager页面的适配器
viewPager = (ViewPager)findViewById(R.id.vp);
viewPager.setAdapter(new MyAdapter());
//设置监听器,当ViewPager中的页面改变时调用
viewPager.setOnPageChangeListener(new MyPageChangeListener());
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
//图片自动进行切换,每隔两秒进行一次切换
scheduledExecutorService.scheduleAtFixedRate(new ScrollTask(), 1,2, TimeUnit.SECONDS);
super.onStart();
}
@Override
protected void onStop() {
//当Activity不可见的时候停止切换
scheduledExecutorService.shutdown();
super.onStop();
}
@Override
protected void onDestroy() {
super.onDestroy();
}
/**
* 换行切换任务
* @author user36
*
*/
private class ScrollTask implements Runnable{
@Override
public void run() {
synchronized(viewPager){
System.out.println("currentItem: " + currentItem);
currentItem = (currentItem + 1) % imageViews.size();
handler.obtainMessage().sendToTarget();
}
}
}
/***
* 当viewPager页面中的状态发生改变时调用
* @author user36
*
*/
private class MyPageChangeListener implements OnPageChangeListener{
private int oldPosition = 0;
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageSelected(int position) {
currentItem = position;
tv_title.setText(titles[position]);
dots.get(oldPosition).setBackgroundResource(R.drawable.dot_normal);
dots.get(position).setBackgroundResource(R.drawable.dot_focused);
oldPosition = position;
}
}
/**
* 填充ViewPager页面的适配器
* @author user36
*
*/
private class MyAdapter extends PagerAdapter{
@Override
public int getCount() {
return imageResId.length;
}
@Override
public Object instantiateItem(View arg0, int arg1) {
((ViewPager) arg0).addView(imageViews.get(arg1));
return imageViews.get(arg1);
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {
}
@Override
public Parcelable saveState() {
return null;
}
@Override
public void startUpdate(View arg0) {
}
@Override
public void finishUpdate(View arg0) {
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK
&& event.getRepeatCount() == 0) {
event.startTracking();
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.isTracking()
&& !event.isCanceled()) {
// *** DO ACTION HERE ***
return true;
}
return super.onKeyUp(keyCode, event);
}
}