最近做项目中发现,继承了view类的子类,可以用invalidate方法刷新,或者用hanlder消息机制和线程来处理,或者直接在线程中用postInvalidate来处理,但是继承了surface的子类,就不可以用invalidate方法来刷新界面。我写了个简单刷新界面加载图片的demo,先看效果图:
程序图:没有点击事件 点击男生前面圆圈的效果,再次点击消失
点击矩形区域:下箭头 效果图:数字改变
点击矩形区域: 上箭头数字改变,增加:
一、MainActivity类的代码:
package com.cn.android; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.Window; import android.view.WindowManager; public class MainActivity extends Activity { public static final int REFRESH = 0; public static final int RETURN = 1; public static final int REGAIN = 2; GameView mGameView = null;// = new GameView(this,this); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN , WindowManager.LayoutParams.FLAG_FULLSCREEN); this.initGameView(); new Thread(new GameThread()).start(); } Handler mHandler=new Handler(){ public void handleMessage(Message msg) { switch(msg.what){ case MainActivity.REFRESH: mGameView.postInvalidate(); //刷新界面 break; case MainActivity.REGAIN: initGameView(); break; case MainActivity.RETURN: initReturn(); break; default: break; } } }; public void initGameView(){ mGameView = new GameView(this,this); setContentView(mGameView); } public void initReturn(){ //结束程序 System.exit(0); } class GameThread implements Runnable { public void run() { while (!Thread.currentThread().isInterrupted()) { Message message = new Message(); message.what = MainActivity.REFRESH; // 发送消息 MainActivity.this.mHandler.sendMessage(message); try { Thread.sleep(100); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } } } }
二、GameView类的代码:
package com.cn.android; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Rect; import android.view.MotionEvent; import android.view.View; public class GameView extends View{ private MainActivity activity; private Bitmap bggame_image; private Bitmap game_Spoint; private Bitmap game_Dpoint; private boolean showSpoint = false; private boolean showDpoint = false; private boolean showNose = false; private static int scount = 1; private static int dcount = 1; private static int ncount = 1; Paint paint; private String redText = "6"; private static int rcount = 6; private String blueText = "1"; private static int bcount = 1; public GameView(Context context,MainActivity activity) { super(context); this.activity = activity; InitBitmap(); } @Override public void onDraw(Canvas canvas) { paint = new Paint(); paint.setTextSize(35.0f); canvas.drawBitmap(bggame_image,0,0,null); if(showSpoint){ canvas.drawBitmap(game_Spoint,32,124,null); } if(showDpoint){ canvas.drawBitmap(game_Dpoint,32,207,null); } if(showNose){ canvas.drawBitmap(game_Dpoint,88,329,null); } canvas.drawText(redText, 148, 152, paint); canvas.drawText(blueText, 148, 230, paint); } //加载图片 private void InitBitmap() { bggame_image = BitmapFactory.decodeResource(this.getResources(), R.drawable.ernie); game_Spoint = BitmapFactory.decodeResource(this.getResources(), R.drawable.game_point); game_Dpoint = BitmapFactory.decodeResource(this.getResources(), R.drawable.game_point); } @Override public boolean onTouchEvent(MotionEvent event) { int x=(int)event.getX(); int y=(int)event.getY(); if(event.getAction()!=MotionEvent.ACTION_DOWN) return super.onTouchEvent(event); //设置点击区域,可以用刷新界面来显示要加载的图片 Rect rcSimpleChoice,rcDoulbeChoice,rcRegainGame,rcReturnSE, rcRedBig,rcRedSmall,rcBlueBig,rcBlueSmall,rcNose; rcSimpleChoice=new Rect(21,112,63,157); rcDoulbeChoice=new Rect(21,196,63,235); rcRegainGame=new Rect(148,424,189,445); rcReturnSE=new Rect(216,424,258,445); rcRedBig=new Rect(192,114,240,137); rcRedSmall=new Rect(189,142,236,168); rcBlueBig=new Rect(196,188,244,215); rcBlueSmall=new Rect(193,219,240,245); rcNose=new Rect(82,326,111,343); //点击男生的圆圈区域 if(rcSimpleChoice.contains(x,y)) { if(scount%2 == 1){ showSpoint = true; } else if(scount%2 == 0){ showSpoint = false; } scount ++; //通过发送消息来刷新界面 activity.mHandler.sendEmptyMessage(MainActivity.REFRESH); //invalidate(); } //点击女生的圆圈区域 if(rcDoulbeChoice.contains(x,y)) { if(dcount%2 == 1){ showDpoint = true; } else if(dcount%2 == 0){ showDpoint = false; } dcount ++; // activity.mHandler.sendEmptyMessage(MainActivity.REFRESH); //或者直接发送消息 invalidate(); } if(rcRegainGame.contains(x,y)) { //点击返回键 activity.mHandler.sendEmptyMessage(MainActivity.RETURN); } if(rcReturnSE.contains(x,y)) { //点击重玩 rcount = 6; bcount = 1; activity.mHandler.sendEmptyMessage(MainActivity.REGAIN); } //点击红球上箭头按钮 if(rcRedBig.contains(x,y)) { if(rcount<20){ rcount ++ ; redText = String.valueOf(rcount); invalidate(); } } //点击红球下箭头按钮 if(rcRedSmall.contains(x,y)) { if(rcount>6){ rcount -- ; redText = String.valueOf(rcount); invalidate(); } } //点击蓝球上箭头按钮 if(rcBlueBig.contains(x,y)) { if(bcount<16){ bcount ++ ; blueText = String.valueOf(bcount); invalidate(); } } //点击蓝球下箭头按钮 if(rcBlueSmall.contains(x,y)) { if(bcount>1){ bcount -- ; blueText = String.valueOf(bcount); invalidate(); } } //点击小人图片鼻子区域 if(rcNose.contains(x,y)) { if(ncount%2 == 1){ showNose = true; } else if(ncount%2 == 0){ showNose = false; } ncount ++; //通过发送消息来刷新界面 activity.mHandler.sendEmptyMessage(MainActivity.REFRESH); //invalidate(); } return super.onTouchEvent(event); } }
三、特别说明,有想要源码的可以留言,有问题的可以留言,有好的见解的也可以留言,互相讨论,共同进步;
转载请标明出处:http://blog.csdn.net/wdaming1986/article/details/6677729
谢谢