程序源代码模板 (2).doc

上传人:rrsccc 文档编号:11045962 上传时间:2021-06-21 格式:DOC 页数:40 大小:139.50KB
返回 下载 相关 举报
程序源代码模板 (2).doc_第1页
第1页 / 共40页
程序源代码模板 (2).doc_第2页
第2页 / 共40页
程序源代码模板 (2).doc_第3页
第3页 / 共40页
程序源代码模板 (2).doc_第4页
第4页 / 共40页
程序源代码模板 (2).doc_第5页
第5页 / 共40页
点击查看更多>>
资源描述

《程序源代码模板 (2).doc》由会员分享,可在线阅读,更多相关《程序源代码模板 (2).doc(40页珍藏版)》请在三一文库上搜索。

1、页面布局模块程序代码MainActivity.javapackage com.my.llkangame;/第一个页面import android.app.ListActivity;import android.app.ProgressDialog;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.ViewGroup;import android.widget.ImageView;import android.widget.ListView;imp

2、ort android.widget.TextView;import com.plter.lib.android.java.controls.ArrayAdapter;import com.plter.linkgame.R;public class MainActivity extends ListActivity private ArrayAdapter adapter;/定义适配器private ProgressDialog dialog=null;/dialog/savedInstanceStateprotected void onCreate(Bundle savedInstanceS

3、tate) super.onCreate(savedInstanceState);setContentView(R.layout.main_activity);/main_activity.xml/设置适配器adapter=new ArrayAdapter(this,R.layout.game_list_cell) Overridepublic void initListCell(int position, View listCell, ViewGroup parent) ImageView iconIv = (ImageView) listCell.findViewById(R.id.ico

4、nIv);TextView labelTv=(TextView) listCell.findViewById(R.id.labelTv);GameListCellData data = getItem(position);iconIv.setImageResource(data.iconResId);labelTv.setText(data.label);setListAdapter(adapter);/适配器集合adapter.add(new GameListCellData(水果连连看, R.drawable.sg_icon, sg_config.json);adapter.add(new

5、 GameListCellData(蔬菜连连看, R.drawable.sc_icon, sc_config.json);adapter.add(new GameListCellData(动物连连看, R.drawable.dw_icon, dw_config.json);adapter.add(new GameListCellData(爱心连连看, R.drawable.love_icon, love_config.json);adapter.add(new GameListCellData(宝石连连看, R.drawable.coin_icon, coin_config.json);Ove

6、rrideprotected void onPause() if (dialog!=null) dialog.dismiss();dialog=null;super.onPause();Overrideprotected void onListItemClick(ListView l, View v, int position, long id) dialog=ProgressDialog.show(this, 请稍候, 正在加载游戏资源);GameListCellData data = adapter.getItem(position);Intent i = new Intent(this,

7、 LinkGameActivity.class);i.putExtra(configFile, data.gameConfigFile);startActivity(i);super.onListItemClick(l, v, position, id);public static class GameListCellDatapublic String label=null;public int iconResId=0;public String gameConfigFile=null;public GameListCellData(String label,int iconResId,Str

8、ing gameConfigFile) this.label=label;this.iconResId=iconResId;this.gameConfigFile=gameConfigFile;LinkGameActivity.javapackage com.my.llkangame;import android.app.Activity;import android.os.Bundle;import android.text.TextUtils;import android.view.Display;import android.widget.Button;import android.wi

9、dget.TextView;import com.my.cord.Config;import com.my.cord.GameViewhhxx;import com.my.reader.InnerGameReader;import com.plter.linkgame.R;/游戏开始界面宽高、布局等且开始游戏public class LinkGameActivity extends Activity private GameViewhhxx gameView;/* Called when the activity is first created. */ SuppressWarnings(de

10、precation)public void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); String configFile = getIntent().getStringExtra(configFile); if (TextUtils.isEmpty(configFile) finish();return; /获得屏幕宽高 Display display = getWindowManager().getDefaultDisplay(); Config.setScreenWidth(display

11、.getWidth(); Config.setScreenHeight(display.getHeight(); /设置内容布局 setContentView(R.layout.link_game_activity); gameView=(GameViewhhxx) findViewById(R.id.gameView); gameView.setTimeTv(TextView) findViewById(R.id.timeTv); gameView.setLevelTv(TextView) findViewById(R.id.levelTv); gameView.setBreakCardsB

12、tn(Button) findViewById(R.id.breakCardsBtn); gameView.setNoteBtn(Button) findViewById(R.id.noteBtn); gameView.setPauseBtn(Button) findViewById(R.id.pauseBtn); /根据游戏资源包初始化游戏 gameView.initWithGamePkg(InnerGameReader.readGame(this, configFile); /开始启动游戏 gameView.showStartGameAlert(); protected void onPa

13、use() gameView.pause(); super.onPause(); protected void onResume() gameView.resume(); super.onResume(); LinesContainer.javapackage com.my.cord;import java.util.List;import android.content.Context;import android.graphics.Canvas;import android.graphics.Paint;import android.graphics.Paint.Style;import

14、android.graphics.Path;import android.graphics.PointF;import android.view.View;import android.view.animation.AlphaAnimation;import android.view.animation.Animation;import android.view.animation.Animation.AnimationListener;/* * 设置对图片进行连接的线的宽度和颜色 */public class LinesContainer extends View implements An

15、imationListenerprivate List points=null;private final Paint paint=new Paint();private final Path path = new Path();private final AlphaAnimation aa = new AlphaAnimation(1, 0);public LinesContainer(Context context) super(context);paint.setStyle(Style.STROKE);paint.setStrokeWidth(5);paint.setColor(0xFF

16、FF0000);aa.setDuration(500);aa.setAnimationListener(this);setVisibility(View.GONE);public void showLines(List points)if (points.size()2) throw new RuntimeException(点的个数不能小于2);elsesetVisibility(View.VISIBLE);this.points=points;invalidate();startAnimation(aa);protected void onDraw(Canvas canvas) if (p

17、oints=null|points.size()2) return;path.reset();PointF p=points.get(0);path.moveTo(p.x, p.y);for (int i = 1; i points.size(); i+) p=points.get(i);path.lineTo(p.x, p.y);canvas.drawPath(path, paint);super.onDraw(canvas);public void onAnimationStart(Animation animation) public void onAnimationEnd(Animat

18、ion animation) setVisibility(View.GONE);public void onAnimationRepeat(Animation animation) Config.javapackage com.my.cord;public class Config private static float cardWidth=0;private static float cardHeight=0;private static float screenWidth=0;private static float screenHeight=0;private static float

19、 cardsOffsetX=0;private static float cardsOffsetY=0;/* * 游戏区域的宽度 */private static float gameCardsAreaWidth=0;/* * 游戏区域的高度 */private static float gameCardsAreaHeight=0;/* * 卡片的上边距 */public static final float GAME_CARDS_AREA_TOP=80;/* * 卡片的下边距 */public static final float GAME_CARDS_AREA_BOTTOM=80;/* *

20、 卡片区域左边距 */public static final float GAME_CARDS_AREA_LEFT=0;/* * 卡片区域右边距 */public static final float GAME_CARDS_AREA_RIGHT=0;/* * return the screenWidth */public static float getScreenWidth() return screenWidth;/* * param screenWidth the screenWidth to set */public static void setScreenWidth(float s

21、creenWidth) Config.screenWidth = screenWidth;Config.setGameCardsAreaWidth(Config.getScreenWidth()-Config.GAME_CARDS_AREA_LEFT-Config.GAME_CARDS_AREA_RIGHT);computeCardWidthAndHeight();/* * return the screenHeight */public static float getScreenHeight() return screenHeight;/* * param screenHeight the

22、 screenHeight to set */public static void setScreenHeight(float screenHeight) Config.screenHeight = screenHeight;Config.setGameCardsAreaHeight(Config.getScreenHeight()-Config.GAME_CARDS_AREA_BOTTOM-Config.GAME_CARDS_AREA_TOP);computeCardWidthAndHeight();private static void computeCardWidthAndHeight(

23、)float cardWidth=Config.getGameCardsAreaWidth()/Level.MAX_H_CARDS_COUNT;float cardHeight=Config.getGameCardsAreaHeight()/Level.MAX_V_CARDS_COUNT;float min = Math.min(cardWidth, cardHeight);Config.setCardWidth(min);/卡片最小宽Config.setCardHeight(min);/卡片最小高/* * return the cardWidth */public static float

24、getCardWidth() return cardWidth;/* * param cardWidth the cardWidth to set */private static void setCardWidth(float cardWidth) Config.cardWidth = cardWidth;/* * return the cardHeight */public static float getCardHeight() return cardHeight;/* * param cardHeight the cardHeight to set */private static v

25、oid setCardHeight(float cardHeight) Config.cardHeight = cardHeight;/* * return the cardsOffsetX */public static float getCardsOffsetX() return cardsOffsetX;/* * param cardsOffsetX the cardsOffsetX to set */public static void setCardsOffsetX(float cardsOffsetX) Config.cardsOffsetX = cardsOffsetX;/* *

26、 return the cardsOffsetY */public static float getCardsOffsetY() return cardsOffsetY;/* * param cardsOffsetY the cardsOffsetY to set */public static void setCardsOffsetY(float cardsOffsetY) Config.cardsOffsetY = cardsOffsetY;/* * return the gameCardsAreaWidth */public static float getGameCardsAreaWi

27、dth() return gameCardsAreaWidth;/* * param gameCardsAreaWidth the gameCardsAreaWidth to set */private static void setGameCardsAreaWidth(float gameCardsAreaWidth) Config.gameCardsAreaWidth = gameCardsAreaWidth;/* * return the gameCardsAreaHeight */public static float getGameCardsAreaHeight() return g

28、ameCardsAreaHeight;/* * param gameCardsAreaHeight the gameCardsAreaHeight to set */private static void setGameCardsAreaHeight(float gameCardsAreaHeight) Config.gameCardsAreaHeight = gameCardsAreaHeight;Picture.javapackage com.my.reader;import android.graphics.Bitmap;/* * Bitmap 面板 * */public class P

29、icture public Picture(Bitmap bitmap) this.bitmap=bitmap;id=_getId();public Bitmap getBitmap() return bitmap;private int id=0;public int getId() return id;private Bitmap bitmap=null;private static int _id=0;private static int _getId()_id+;return _id;算法分析模块程序代码Cardmm.javapackage com.my.cord;/*重排:先遍历当前

30、页面还有的图片,调用随机算法调换页面中图片的位置 * 先选中一张图片,获取此图片在垂直和水平方向上的的位置,判断是否被选中,判断当前已选中的图片个数 *若选中图片个数小于2张,再选择一张图片,重复上一个动作,若选中图片个数达到2张,判断是否满足消除条件 *若满足,则消除,若不满足,则不发生任何变化。 */import android.content.Context;import android.graphics.Canvas;import android.graphics.Paint;import android.graphics.Paint.Style;import android.grap

31、hics.PointF;import android.view.View;import android.view.animation.Animation;import android.view.animation.Animation.AnimationListener;import android.widget.FrameLayout;import android.widget.ImageView;import android.widget.RelativeLayout;import com.my.reader.Picture;public class Cardmm extends Frame

32、Layout private Picture picture=null;private boolean checked=false;private int indexI=0;private int indexJ=0;private final PointF center=new PointF();private final float lineWidth=3;private final float halfWidth=lineWidth/2;private RelativeLayout.LayoutParams lp;private ImageView imageView=null;priva

33、te View checkedRect=null;private View noteRect=null;private final CardNoteAnim cna = new CardNoteAnim();public Cardmm(Context context,Picture pic) super(context);picture=pic;imageView=new ImageView(getContext();/图像集合imageView.setImageBitmap(getPicture().getBitmap();addView(imageView, -1, -1);noteRec

34、t=createRectLine(0xFF00FF00);addView(noteRect, -1, -1);noteRect.setVisibility(View.INVISIBLE);checkedRect=createRectLine(0xFFFF0000);addView(checkedRect, -1, -1);setChecked(false);/config note animationcna.setDuration(3000);cna.setAnimationListener(cardNoteAnimListener);public void setX(float x) lp=

35、getLayoutParams();lp.leftMargin=(int) x;setLayoutParams(lp);resetCenter();public void setY(float y) lp=getLayoutParams();lp.topMargin=(int) y;setLayoutParams(lp);resetCenter();public void setXY(float x,float y)lp=getLayoutParams();lp.topMargin=(int) y;lp.leftMargin=(int) x;setLayoutParams(lp);resetC

36、enter();/* * 根据index i j重置图片的位置 * 重排图片 */public void resetPositionByIndexIJ()setX(GameUtilmm.getXByIndexi(getIndexI();/定位获取x方向setY(GameUtilmm.getYByIndexJ(getIndexJ();/定位获取y方向/* * 返回图片 */public Picture getPicture() return picture;/* * 判断是否选中 */public boolean isChecked() return checked;/* * 根据是否选中图片进

37、行消除操作 */public void setChecked(boolean checked) this.checked = checked;if (checked) checkedRect.setVisibility(View.VISIBLE);stopNoteAnim();/停止提示动画elsecheckedRect.setVisibility(View.INVISIBLE);/* * return the indexI */public int getIndexI() return indexI;/* * param indexI the indexI to set */public v

38、oid setIndexI(int indexI) this.indexI = indexI;/* * return the indexJ */public int getIndexJ() return indexJ;/* * param indexJ the indexJ to set */public void setIndexJ(int indexJ) this.indexJ = indexJ;public float getCenterX() return getCenter().x;public float getCenterY() return getCenter().y;publ

39、ic float getX()return getLayoutParams().leftMargin;public float getY()return getLayoutParams().topMargin;public RelativeLayout.LayoutParams getLayoutParams() return (android.widget.RelativeLayout.LayoutParams) super.getLayoutParams();private void resetCenter()getCenter().x=getX()+Config.getCardWidth

40、()/2;getCenter().y=getY()+Config.getCardHeight()/2;/* * return the center */public PointF getCenter() return center;/* * 开始提示动画 */public void startNoteAnim()noteRect.setVisibility(View.VISIBLE);noteRect.startAnimation(cna);/* * 停止提示动画 */public void stopNoteAnim()if (noteRect.getVisibility()!=View.VI

41、SIBLE) noteRect.setAnimation(null);noteRect.setVisibility(View.INVISIBLE);private View createRectLine(int color)final Paint frontShapePaint=new Paint();frontShapePaint.setColor(color);frontShapePaint.setStyle(Style.STROKE);frontShapePaint.setStrokeWidth(lineWidth);/行宽return new View(getContext()prot

42、ected void onDraw(Canvas canvas) canvas.drawRect(halfWidth, halfWidth, Config.getCardWidth()-lineWidth, Config.getCardHeight()-lineWidth, frontShapePaint);super.onDraw(canvas);private final AnimationListener cardNoteAnimListener=new AnimationListener() public void onAnimationStart(Animation animation) / TODO Auto-generated method stubpublic void onAnimationRepeat(Anim

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 社会民生


经营许可证编号:宁ICP备18001539号-1