学习android两个多月了,开始时连java都没搞懂是啥玩意,幸好有些c,c++基础。然后每天看博客,逛论坛。渐渐的对java的语法,应用等方面有了些了解。一直以来,看到像qq,新浪微博等软件,特别是游戏的界面,都非常神往。想和某一天也可以开发个应用程序能够拥有如此绚丽的界面。
心理有些冲动了,果断在当当上买了一本《android2.0游戏看法实战宝典》,开始了学习之路。
一般来说,实现用户的交互,按钮是很常用的控件,android自带的按钮太经典了,有点确认个人色彩。因此我自己动手些了个ImageButton的类,方便以后的使用。ImageButton这个类要实现的方法主要有一下几点:1.检测触摸屏按下的位置是否为按钮所在的区域;2.点击按钮的时候反馈信息给用户,我这里为了简单起见,主要给按钮加了个边框而已;3.最重要的,当然是实现相应事件了。
一下是基于canvas应用的ImageButton类的代码:
package lxx.mygame;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Paint.Style;
import android.util.Log;
public class ImageButton {
Bitmap bitmap;
Rect rect;
int x;
int y;
boolean isTouch = false;
ImageButton(Bitmap bitmap, int x, int y){
this.bitmap = bitmap;//得到图片资源
this.x = x;
this.y = y;
rect = new Rect(x,y,x+bitmap.getWidth(),y+bitmap.getHeight());//记录位图的矩形区域
}
public void drawImage(Canvas canvas){//界面上显示ImageButton
canvas.drawBitmap(bitmap, x, y, null);
if(isTouch ){//检测触摸点是否在按钮区域,然后决定是否加边框
Paint borderPaint = new Paint();
borderPaint.setStyle(Style.STROKE);
borderPaint.setARGB(255, 255, 0, 0);
borderPaint.setStrokeWidth(4f);
canvas.drawRect(rect, borderPaint);
}
}
public boolean getClicked(int x, int y){){//检测触摸点是否在按钮区域
boolean ret = false;
if(rect.contains(x,y))
{
ret = true;
isTouch = true;
}
return ret;
}
public void getButtonUp(){//检测触摸屏的释放
isTouch = false;
}
public void dragButton(int x, int y){//拖动按钮
if(isTouch){
this.x = x;//保存新的坐标
this.y = y;
int width = bitmap.getWidth();
int height = bitmap.getHeight();
rect = new Rect(x,y,x+width,y+height);
Log.d("Season","x:" + x + " y:"+y) ;
}
}
}
Activity中的代码如下:
package lxx.mygame;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
public class MygameActivity extends Activity {
/** Called when the activity is first created. */
WelcomView welcomView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);//全屏显示
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//横屏显示
welcomView = new WelcomView(this);
setContentView(welcomView);
}
}
WelcomView 类主要实现触摸屏位置的检测,然后对按钮进行相应的处理
package lxx.mygame;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Toast;
public class WelcomView extends SurfaceView implements SurfaceHolder.Callback{
MygameActivity father;
WelcomThread welcomThread;
Bitmap background;
Bitmap startBitmap;
Bitmap clubBitmap;
Bitmap speedBitmap;
ImageButton start;
ImageButton club;
boolean speedEnable = false;
int x;
int y;
public WelcomView(MygameActivity father) {
super(father);
// TODO Auto-generated constructor stub
this.father = father;
getHolder().addCallback(this);
initBitmap(father);
start = new ImageButton(startBitmap,40,40);
club = new ImageButton(clubBitmap,100,40);
}
public void doDraw(Canvas canvas){
Paint paint = new Paint();
paint.setColor(Color.BLACK);
canvas.drawColor(Color.BLACK);
canvas.drawBitmap(background, 0,0, null);
start.drawImage(canvas);
club.drawImage(canvas);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
int x = (int) event.getX();
int y = (int) event.getY();
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN://按下按钮
if(start.getClicked(x, y)){
Toast.makeText(getContext(), "你点击了开始按钮", Toast.LENGTH_SHORT).show();
}
if(club.getClicked(x, y)){
Toast.makeText(getContext(), "你点击了club按钮", Toast.LENGTH_SHORT).show();
}
this.x = x;
this.y= y;
break;
case MotionEvent.ACTION_UP://释放按钮
start.getButtonUp();
club.getButtonUp();
speedEnable =false;
this.x = x;
this.y= y;
break;
case MotionEvent.ACTION_MOVE://拖动按钮
start.dragButton(x, y);
club.dragButton(x, y);
this.x = x;
this.y= y;
break;
}
return true;
}
public void initBitmap(Context context){//初始化图片资源
Resources r = context.getResources();
background = BitmapFactory.decodeResource(r, R.drawable.welcome);
startBitmap = BitmapFactory.decodeResource(r, R.drawable.start);
clubBitmap = BitmapFactory.decodeResource(r, R.drawable.club);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
welcomThread = new WelcomThread(this,getHolder());
this.welcomThread.flag = true;
welcomThread.start();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
boolean retry =true;
welcomThread.flag = false;
while(retry){
try{
welcomThread.join();//等待线程的结束
retry = false;
}
catch(InterruptedException e){
e.printStackTrace();
}
}
}
}
在WelcomThread中主要定时的刷新界面
package lxx.mygame;
import android.graphics.Canvas;
import android.util.Log;
import android.view.SurfaceHolder;
public class WelcomThread extends Thread{
int sleepSpan = 50;
WelcomView father;
SurfaceHolder surfaceHolder;
boolean flag ;
WelcomThread(WelcomView father, SurfaceHolder surfaceHolder){
this.father = father;
this.surfaceHolder = surfaceHolder;
this.flag = true;
}
@Override
public void run() {
// TODO Auto-generated method stub
Canvas canvas = null;
while(flag){
try{
canvas = surfaceHolder.lockCanvas(null);//锁住画布
synchronized(surfaceHolder){
father.doDraw(canvas);
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally{
if(canvas != null)
{
surfaceHolder.unlockCanvasAndPost(canvas); //显示新的画布
}
}
try{
Thread.sleep(sleepSpan); //休眠一小会
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
}
为了方便大家学习交流,特意提供源码下载地址:
http://download.csdn.net/source/3556628
最后的显示效果如下图,可以任意拖动按钮并触发事件等,因为比较细化极品飞车,就把他弄了个背景,哈哈