日志分类:Android游戏

RB打地鼠测试版[0.9.8版本]发布了

时间:2011年06月05日作者:ronald查看次数:168 views评论次数:0

RB打地鼠开发过程大约一个多月,是业余时间来开发的.所有有什么细节方面需要好好完善.特别是性能方面.
这个游戏没有使用游戏引擎,因为我想了解一下Android开发游戏的方法.我计划下一个游戏会使用游戏引擎来开发.现在在考虑用WiEngine或者是LGame来开发.

我的很多博文中也是RB打地鼠游戏的实际程序.

RB打地鼠的介绍在 http://blog.r-base.net/rb-hitshrew

sqlite管理工具-Dbvis

时间:2011年06月01日作者:ronald查看次数:414 views评论次数:0

之前我的博文中也已经提到dbvis的通用数据库管理工具.[通用数据库管理工具-Dbvis].上次提到的现在dbvis准备出新版本了,我关于的两个新特性是新增对h2数据库sqlite数据库的支持.
我相信开发android的人员使用的数据库绝大多数都是使用sqlite的,因为android中默认支持.
我之前想过在android中使用derby的,但是考虑这样会增大应用的大小.这对于手机应用来说根本就不能接受.
所以开发android一直都使用sqlite. 但是我发现在windows中没有发现特别好用的管理工具(之前一直使用的dbvis还没有支持)

为什么要在windows中找个sqlite的管理工具呢?我觉得有以下几点是需要的:
1, 准备一些测试数据,方便在应用中进行测试
2, 可能会将一些日志,配置等信息放在sqlite数据库中,需要导出来进行查看数据或修改配置信息

以下是如何连接sqlite的一些中的截图.希望对大家有用.

选择数据库类型为sqlite

指定sqlite数据库的文件路径:

注意:发现该版本的bug,连接后会自动转成h2数据库类型,需要手动改动后才能正常使用

在SQL Commander里执行SQL语句:
[ create table test_table (id numeric(18),name varchar(100)); ]

以下连接成功后,创建了的测试表后的视图,可以看到功能还是比较多的,已经能满足大部分的需求.

现在dbvis7.2版本还在测试中,破解版本还没有出,一般在正式版发布后两个星期左右会有破解文件放出.
现在就先用用测试版本吧
下载地址: http://www.dbvis.com/products/dbvis/eap/install.jsp

返回 : Android开发博文汇总

Android图像拉伸(背景填充)

时间:2011年05月25日作者:ronald查看次数:2,677 views评论次数:0

最近在做一个小游戏的时候,碰到图片不能兼容多种分辨率的问题.今日碰到的问题主要是背景图.

先上图,看看解决问题前的截图.

以上的背景图的大小为 320X480,当前的手机分辨率为480X800,直接使用以下代码直接画图像的话就会有以上的问题.

  public void drawBackground(canvas){
    //  游戏背景
    Bitmap playingBackground = BitmapFactory.decodeResource(
            context.getResources(), R.drawable.bg_playing_game);
    //  直接绘制背景,坐标为 0,0
    canvas.drawBitmap(playingBackground, 0,0, CommonView.paint);
  }

后来从网上查找一些资料,了解到可以使用 android.graphics.Matrix 要将背景图进行缩放或拉伸的功能.当然 android.graphics.Matrix类能实现更多的图像处理,如翻转,透明度等等,如果需要了解 android.graphics.Matrix怎么使用,可以查找相关资料.

好了,下面是处理后的效果图:

以下是实现该效果的代码:

  public void drawBackground(canvas){
    //  游戏背景
    Bitmap playingBackground = BitmapFactory.decodeResource(
            context.getResources(), R.drawable.bg_playing_game);
 
	//计算缩放率,新尺寸除原始尺寸   
        int w = CommonView.background.getWidth();
        int h = CommonView.background.getHeight();
 
        // 目标的宽度与高度根据displayMetrics 来得到手机屏幕的实际宽度与高度
	DisplayMetrics metrics = new DisplayMetrics();
	getWindowManager().getDefaultDisplay().getMetrics(metrics);
 
        int phoneWidth = metrics.widthPixels;
        int phoneHeigth = metrics.heigthPixels;        
 
        float scaleWidth = ((float) phoneWidth  / w);   
        float scaleHeight = ((float) phoneHeigth  / h);   
 
        matrix = new Matrix();
	matrix.postScale(scaleWidth, scaleHeight);
 
    //  绘制背景的时候不直接写坐标,而使用 matrix的参数
    canvas.drawBitmap(playingBackground, matrix, new Paint());
  }

通过以上的代码,就是实现图像的拉伸或背景填充.

最近开发的小游戏没有使用游戏引擎来实现.是想先好好了解Android游戏开发最原始的实现方式,之后再开发再来研究一些Android的游戏引擎.
个人觉得不使用游戏引擎,最难处理的是手机分辨率的问题.

Android使用自定义AlertDialog(退出提示框)

时间:2011年05月22日作者:ronald查看次数:2,059 views评论次数:1

有时候我们需要在游戏或应用中用一些符合我们样式的提示框(AlertDialog)
以下是我在开发一个小游戏中总结出来的.希望对大家有用.

先上效果图:

下面是用到的背景图或按钮的图片

经过查找资料和参考了一下例子后才知道,要实现这种效果很简单.就是在设置alertDialog的contentView.

以下的代码是写在Activity下的,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public boolean onKeyDown(int keyCode, KeyEvent event) {
	// 如果是返回键,直接返回到桌面
	if(keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_HOME){
           showExitGameAlert();
	}
 
	return super.onKeyDown(keyCode, event);
}
private void showExitGameAlert() {
	final AlertDialog dlg = new AlertDialog.Builder(this).create();
	dlg.show();
	Window window = dlg.getWindow();
        // *** 主要就是在这里实现这种效果的.
        // 设置窗口的内容页面,shrew_exit_dialog.xml文件中定义view内容
	window.setContentView(R.layout.shrew_exit_dialog);
        // 为确认按钮添加事件,执行退出应用操作
	ImageButton ok = (ImageButton) window.findViewById(R.id.btn_ok);
	ok.setOnClickListener(new View.OnClickListener() {
		public void onClick(View v) {
			exitApp(); // 退出应用...
		}
	});
 
        // 关闭alert对话框
        ImageButton cancel = (ImageButton) window.findViewById(R.id.btn_cancel);
        cancel.setOnClickListener(new View.OnClickListener() {
	  public void onClick(View v) {
		  dlg.cancel();
		}
	  });
}

以下的是layout文件,定义了对话框中的背景与按钮.点击事件在Activity中添加.
文件名为 : shrew_exit_dialog.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_height="wrap_content" 
	android:layout_width="wrap_content">
 
	<!-- 退出游戏的背景图 -->
	<ImageView android:id="@+id/exitGameBackground"
		android:layout_centerInParent="true" 
		android:layout_height="wrap_content"
		android:layout_width="wrap_content" 
		android:src="@drawable/bg_exit_game" />
 
	<!-- 确认按钮 -->
	<ImageButton android:layout_alignBottom="@+id/exitGameBackground"
		android:layout_alignLeft="@+id/exitGameBackground"
		android:layout_marginBottom="30dp" 
		android:layout_marginLeft="35dp"
		android:id="@+id/btn_ok" 
		android:layout_height="wrap_content"
		android:layout_width="wrap_content" 
		android:background="@drawable/btn_ok" />
 
	<!-- 取消按钮 -->
	<ImageButton android:layout_alignBottom="@+id/exitGameBackground"
		android:layout_alignRight="@+id/exitGameBackground"
		android:layout_marginBottom="30dp" 
		android:layout_marginRight="35dp"
		android:id="@+id/btn_cancel" 
		android:layout_height="wrap_content"
		android:layout_width="wrap_content" 
		android:background="@drawable/btn_cancel" />
</RelativeLayout>

就这样经过了以上几步,就可以实现自定义AlertDialog的效果了. 用同样的思路可以实现其它更复杂的效果.

返回 : Android开发博文汇总

如何在SurfaceView中添加广告??

时间:2011年05月10日作者:ronald查看次数:355 views评论次数:0

如何在SurfaceView中添加广告??这个问题我之前在google和百度里找过,但是没有找到答案。我心里想,难道这个问题没有人碰过吗?我开始找之前我以为这个问题很快就能找到答案的。但是结果没有那么顺利。。。。

首先我为什么会出现这样的问题呢?因为之前一直在开发Android应用,现在转成Android的游戏的开发(学习中)。在开发应用一般都是用xml进行布局的,但我第一个游戏开发过程时我没有用xml进行布局。可能是因为开始学游戏开发的时候基本上都是是看《Android2.0 游戏开发实战宝典》来开发,完全参考该书去写代码的。书中是直接用GameView来画页面,代码如下:

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
 
		//全屏
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN ,  
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
 
		setContentView(new GameView(this));
	}

像上面的代码这样来画页面(setContentView方法设置SurfaceView)的话,我真没不到怎么加广告,难道还要画一个广告出来?(呵呵。。) 平时用果合添加广告的时候都是在xml中添加Layout(id=adLayout)。直接设置了SurfaceView到contentView的话那么可以怎么样定义adLayout呢?

最后通过在QQ群中问了下才发现自己方向是完全错误的(可能也是受到《Android2.0 游戏开发实战宝典》书的影响,我觉得该书写到非常好,起码我看完了后了解很多开发游戏的方法与技巧)。其实应该是在xml布局中直接调用SurfaceView。真的不开发游戏也不知道原来可以这样。。因为开发Android应用一般很少使用SurfaceView,起码我自己开发了两三个应用也没有使用过SurfaceView。。真的说来惭愧。。后来就知道了如何用xml布局添加SurfaceView,代码如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent"	
android:layout_height="fill_parent">
 
<rbase.app.hitshrew.view.GameView android:id="@+id/shrew_gameview"
android:layout_width="fill_parent" 
android:layout_height="fill_parent" />
 
<LinearLayout android:id="@+id/adLayout" 
android:layout_alignParentTop="true" 
android:layout_alignParentRight="true" 
android:layout_width="320px" 
android:layout_height="wrap_content" />	 
</LinearLayout>

返回 : Android开发博文汇总