android surfaceview 切换全屏怎么全屏播放

Android SurfaceView使用详解 - Sun‘刺眼的博客 - 博客园
随笔 - 608, 文章 - 0, 评论 - 7, 引用 - 0
1. SurfaceView的定义前面已经介绍过View了,下面来简单介绍一下SurfaceView,参考SDK文档和网络资料:SurfaceView是View的子类,它内嵌了一个专门用于绘制的Surface,你可以控制这个Surface的格式和尺寸,Surfaceview控制这个Surface的绘制位置。surface是纵深排序(Z-ordered)的,说明它总在自己所在窗口的后面。SurfaceView提供了一个可见区域,只有在这个可见区域内的surface内容才可见。surface的排版显示受到视图层级关系的影响,它的兄弟视图结点会在顶端显示。这意味者 surface的内容会被它的兄弟视图遮挡,这一特性可以用来放置遮盖物(overlays)(例如,文本和按钮等控件)。注意,如果surface上面有透明控件,那么每次surface变化都会引起框架重新计算它和顶层控件的透明效果,这会影响性能。SurfaceView默认使用双缓冲技术的,它支持在子线程中绘制图像,这样就不会阻塞主线程了,所以它更适合于游戏的开发。
2. SurfaceView的使用首先继承SurfaceView,并实现SurfaceHolder.Callback接口,实现它的三个方法:surfaceCreated,surfaceChanged,surfaceDestroyed。surfaceCreated(SurfaceHolder holder):surface创建的时候调用,一般在该方法中启动绘图的线程。surfaceChanged(SurfaceHolder holder, int format, int width,int height):surface尺寸发生改变的时候调用,如横竖屏切换。surfaceDestroyed(SurfaceHolder holder) :surface被销毁的时候调用,如退出游戏画面,一般在该方法中停止绘图线程。还需要获得SurfaceHolder,并添加回调函数,这样这三个方法才会执行。
3. SurfaceView实战下面通过一个小demo来学习SurfaceView在实际项目中的使用,绘制一个精灵,该精灵有四个方向的行走动画,让精灵沿着屏幕四周不停的行走。游戏中精灵素材和最终实现的效果图:
首先创建核心类GameView.java,源码如下:
public class GameView&extends SurfaceView&implements
&&&&&&&&SurfaceHolder.Callback {
&&&&public static int SCREEN_WIDTH;
&&&&public static int SCREEN_HEIGHT;
&&&&private Context mC
&&&&private SurfaceHolder mH
&&&&private static final int DRAW_INTERVAL =&30;
&&&&private DrawThread mDrawT
&&&&private FrameAnimation []spriteA
&&&&private Sprite mS
&&&&private int spriteWidth =&0;
&&&&private int spriteHeight =&0;
&&&&private float spriteSpeed = (float)((500& * SCREEN_WIDTH /&480) *&0.001);
&&&&private int row =&4;
&&&&private int col =&4;
&&&&public GameSurfaceView(Context context) {
&&&&&&&&super(context);
&&&&&&&&this.mContext =
&&&&&&&&mHolder =&this.getHolder();
&&&&&&&&mHolder.addCallback(this);
&&&&&&&&initResources();
&&&&&&&&mSprite =&new Sprite(spriteAnimations,0,0,spriteWidth,spriteHeight,spriteSpeed);
&&&&private void initResources() {
&&&&&&&&Bitmap[][] spriteImgs = generateBitmapArray(mContext, R.drawable.sprite, row, col);
&&&&&&&&spriteAnimations =&new FrameAnimation[row];
&&&&&&&&for(int i =&0; i & i ++) {
&&&&&&&&&&&&Bitmap []spriteImg = spriteImgs[i];
&&&&&&&&&&&&FrameAnimation spriteAnimation =&new FrameAnimation(spriteImg,new int[]{150,150,150,150},true);
&&&&&&&&&&&&spriteAnimations[i] = spriteA
&&&&public Bitmap decodeBitmapFromRes(Context context,&int resourseId) {
&&&&&&&&BitmapFactory.Options opt =&new BitmapFactory.Options();
&&&&&&&&opt.inPreferredConfig = Bitmap.Config.RGB_565;
&&&&&&&&opt.inPurgeable =&true;
&&&&&&&&opt.inInputShareable =&true;
&&&&&&&&InputStream is = context.getResources().openRawResource(resourseId);
&&&&&&&&return BitmapFactory.decodeStream(is,&null, opt);
&&&&public Bitmap createBitmap(Context context, Bitmap source,&int row,
&&&&&&&&&&&&int col,&int rowTotal,&int colTotal) {
&&&&&&&&Bitmap bitmap = Bitmap.createBitmap(source,
&&&&&&&&&&&&&&&&(col -&1) * source.getWidth() / colTotal,
&&&&&&&&&&&&&&&&(row -&1) * source.getHeight() / rowTotal, source.getWidth()
&&&&&&&&&&&&&&&&&&&&&&&&/ colTotal, source.getHeight() / rowTotal);
&&&&&&&&return
&&&&public Bitmap[][] generateBitmapArray(Context context,&int resourseId,
&&&&&&&&&&&&int row,&int col) {
&&&&&&&&Bitmap bitmaps[][] =&new Bitmap[row][col];
&&&&&&&&Bitmap source = decodeBitmapFromRes(context, resourseId);
&&&&&&&&this.spriteWidth = source.getWidth() /
&&&&&&&&this.spriteHeight = source.getHeight() /
&&&&&&&&for (int i =&1; i &= i++) {
&&&&&&&&&&&&for (int j =&1; j &= j++) {
&&&&&&&&&&&&&&&&bitmaps[i -&1][j -&1] = createBitmap(context, source, i, j,
&&&&&&&&&&&&&&&&&&&&&&&&row, col);
&&&&&&&&&&&&}
&&&&&&&&if (source !=&null && !source.isRecycled()) {
&&&&&&&&&&&&source.recycle();
&&&&&&&&&&&&source =&null;
&&&&&&&&return
&&&&public void surfaceChanged(SurfaceHolder holder,&int format,&int width,
&&&&&&&&&&&&int height) {
&&&&public void surfaceCreated(SurfaceHolder holder) {
&&&&&&&&if(null == mDrawThread) {
&&&&&&&&&&&&mDrawThread =&new DrawThread();
&&&&&&&&&&&&mDrawThread.start();
&&&&public void surfaceDestroyed(SurfaceHolder holder) {
&&&&&&&&if(null != mDrawThread) {
&&&&&&&&&&&&mDrawThread.stopThread();
&&&&private class DrawThread&extends Thread {
&&&&&&&&public boolean isRunning =&false;
&&&&&&&&public DrawThread() {
&&&&&&&&&&&&isRunning =&true;
&&&&&&&&public void stopThread() {
&&&&&&&&&&&&isRunning =&false;
&&&&&&&&&&&&boolean workIsNotFinish =&true;
&&&&&&&&&&&&while (workIsNotFinish) {
&&&&&&&&&&&&&&&&try {
&&&&&&&&&&&&&&&&&&&&this.join();
&&&&&&&&&&&&&&&&}&catch (InterruptedException e) {
&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&e.printStackTrace();
&&&&&&&&&&&&&&&&}
&&&&&&&&&&&&&&&&workIsNotFinish =&false;
&&&&&&&&&&&&}
&&&&&&&&public void run() {
&&&&&&&&&&&&long deltaTime =&0;
&&&&&&&&&&&&long tickTime =&0;
&&&&&&&&&&&&tickTime = System.currentTimeMillis();
&&&&&&&&&&&&while (isRunning) {
&&&&&&&&&&&&&&&&Canvas canvas =&null;
&&&&&&&&&&&&&&&&try {
&&&&&&&&&&&&&&&&&&&&synchronized (mHolder) {
&&&&&&&&&&&&&&&&&&&&&&&&canvas = mHolder.lockCanvas();
&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&&&&&mSprite.setDirection();
&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&&&&&mSprite.updatePosition(deltaTime);
&&&&&&&&&&&&&&&&&&&&&&&&drawSprite(canvas);
&&&&&&&&&&&&&&&&&&&&}
&&&&&&&&&&&&&&&&}&catch (Exception e) {
&&&&&&&&&&&&&&&&&&&&e.printStackTrace();
&&&&&&&&&&&&&&&&}&finally {
&&&&&&&&&&&&&&&&&&&&if (null != mHolder) {
&&&&&&&&&&&&&&&&&&&&&&&&mHolder.unlockCanvasAndPost(canvas);
&&&&&&&&&&&&&&&&&&&&}
&&&&&&&&&&&&&&&&}
&&&&&&&&&&&&&&&&deltaTime = System.currentTimeMillis() - tickT
&&&&&&&&&&&&&&&&if(deltaTime & DRAW_INTERVAL) {
&&&&&&&&&&&&&&&&&&&&try {
&&&&&&&&&&&&&&&&&&&&&&&&Thread.sleep(DRAW_INTERVAL - deltaTime);
&&&&&&&&&&&&&&&&&&&&}&catch (InterruptedException e) {
&&&&&&&&&&&&&&&&&&&&&&&&e.printStackTrace();
&&&&&&&&&&&&&&&&&&&&}
&&&&&&&&&&&&&&&&}
&&&&&&&&&&&&&&&&tickTime = System.currentTimeMillis();
&&&&&&&&&&&&}
&&&&private void drawSprite(Canvas canvas) {
&&&&&&&&canvas.drawColor(Color.BLACK);
&&&&&&&&mSprite.draw(canvas);
GameView.java中包含了一个绘图线程DrawThread,在线程的run方法中锁定Canvas、绘制精灵、更新精灵位置、释放Canvas等操作。因为精灵素材是一张大图,所以这里进行了裁剪生成一个二维数组。使用这个二维数组初始化了精灵四个方向的动画,下面看Sprite.java的源码。
public class Sprite {
&&&&public static final int DOWN =&0;
&&&&public static final int LEFT =&1;
&&&&public static final int RIGHT =&2;
&&&&public static final int UP =&3;
&&&&public float x;
&&&&public float y;
&&&&public int
&&&&public int
&&&&public double
&&&&public int
&&&&public FrameAnimation[] frameA
&&&&public Sprite(FrameAnimation[] frameAnimations,&int positionX,
&&&&&&&&&&&&int positionY,&int width,&int height,&float speed) {
&&&&&&&&this.frameAnimations = frameA
&&&&&&&&this.x = positionX;
&&&&&&&&this.y = positionY;
&&&&&&&&this.width =
&&&&&&&&this.height =
&&&&&&&&this.speed =
&&&&public void updatePosition(long deltaTime) {
&&&&&&&&switch (direction) {
&&&&&&&&case LEFT:
&&&&&&&&&&&&
&&&&&&&&&&&&this.x =&this.x - (float) (this.speed * deltaTime);
&&&&&&&&&&&&break;
&&&&&&&&case DOWN:
&&&&&&&&&&&&this.y =&this.y + (float) (this.speed * deltaTime);
&&&&&&&&&&&&break;
&&&&&&&&case RIGHT:
&&&&&&&&&&&&this.x =&this.x + (float) (this.speed * deltaTime);
&&&&&&&&&&&&break;
&&&&&&&&case UP:
&&&&&&&&&&&&this.y =&this.y - (float) (this.speed * deltaTime);
&&&&&&&&&&&&break;
&&&&&* 根据精灵的当前位置判断是否改变行走方向
&&&&public void setDirection() {
&&&&&&&&if (this.x &=&0
&&&&&&&&&&&&&&&&&& (this.y +&this.height) & GameSurfaceView.SCREEN_HEIGHT) {
&&&&&&&&&&&&if (this.x &&0)
&&&&&&&&&&&&&&&&this.x =&0;
&&&&&&&&&&&&this.direction = Sprite.DOWN;
&&&&&&&&}&else if ((this.y +&this.height) &= GameSurfaceView.SCREEN_HEIGHT
&&&&&&&&&&&&&&&&&& (this.x +&this.width) & GameSurfaceView.SCREEN_WIDTH) {
&&&&&&&&&&&&if ((this.y +&this.height) & GameSurfaceView.SCREEN_HEIGHT)
&&&&&&&&&&&&&&&&this.y = GameSurfaceView.SCREEN_HEIGHT -&this.
&&&&&&&&&&&&this.direction = Sprite.RIGHT;
&&&&&&&&}&else if ((this.x +&this.width) &= GameSurfaceView.SCREEN_WIDTH
&&&&&&&&&&&&&&&&&&&this.y &&0) {
&&&&&&&&&&&&if ((this.x +&this.width) & GameSurfaceView.SCREEN_WIDTH)
&&&&&&&&&&&&&&&&this.x = GameSurfaceView.SCREEN_WIDTH -&this.
&&&&&&&&&&&&this.direction = Sprite.UP;
&&&&&&&&}&else {
&&&&&&&&&&&&if (this.y &&0)
&&&&&&&&&&&&&&&&this.y =&0;
&&&&&&&&&&&&this.direction = Sprite.LEFT;
&&&&public void draw(Canvas canvas) {
&&&&&&&&FrameAnimation frameAnimation = frameAnimations[this.direction];
&&&&&&&&Bitmap bitmap = frameAnimation.nextFrame();
&&&&&&&&if (null != bitmap) {
&&&&&&&&&&&&canvas.drawBitmap(bitmap, x, y,&null);
精灵类主要是根据当前位置判断行走的方向,然后根据行走的方向更新精灵的位置,再绘制自身的动画。由于精灵的动画是一帧一帧的播放图片,所以这里封装了FrameAnimation.java,源码如下:
public class FrameAnimation{
&&&&/**动画显示的需要的资源 */
&&&&private Bitmap[]
&&&&/**动画每帧显示的时间 */
&&&&private int[]
&&&&/**动画上一帧显示的时间 */
&&&&protected Long lastBitmapT
&&&&/**动画显示的索引值,防止数组越界 */
&&&&protected int
&&&&/**动画是否重复播放 */
&&&&protected boolean
&&&&/**动画重复播放的次数*/
&&&&protected int repeatC
&&&&&* @param bitmap:显示的图片&br/&
&&&&&* @param duration:图片显示的时间&br/&
&&&&&* @param repeat:是否重复动画过程&br/&
&&&&public FrameAnimation(Bitmap[] bitmaps,&int duration[],&boolean repeat) {
&&&&&&&&this.bitmaps =
&&&&&&&&this.duration =
&&&&&&&&this.repeat =
&&&&&&&&lastBitmapTime =&null;
&&&&&&&&step =&0;
&&&&public Bitmap nextFrame() {
&&&&&&&&if (step &= bitmaps.length) {
&&&&&&&&&&&&
&&&&&&&&&&&&if( !repeat ) {
&&&&&&&&&&&&&&&&return null;
&&&&&&&&&&&&}&else {
&&&&&&&&&&&&&&&&lastBitmapTime =&null;
&&&&&&&&&&&&}
&&&&&&&&if (null == lastBitmapTime) {
&&&&&&&&&&&&
&&&&&&&&&&&&lastBitmapTime = System.currentTimeMillis();
&&&&&&&&&&&&return bitmaps[step =&0];
&&&&&&&&long nowTime = System.currentTimeMillis();
&&&&&&&&if (nowTime - lastBitmapTime &= duration[step]) {
&&&&&&&&&&&&
&&&&&&&&&&&&
&&&&&&&&&&&&return bitmaps[step];
&&&&&&&&lastBitmapTime = nowT
&&&&&&&&return bitmaps[step++];
FrameAnimation根据每一帧的显示时间返回当前的图片帧,若没有超过指定的时间则继续返回当前帧,否则返回下一帧。接下来需要做的是让Activty显示的View为我们之前创建的GameView,然后设置全屏显示。
public void onCreate(Bundle savedInstanceState) {
&&&&&super.onCreate(savedInstanceState);
&&&&&getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
&&&&&&&&&&&&&WindowManager.LayoutParams.FLAG_FULLSCREEN);
&&&&&requestWindowFeature(Window.FEATURE_NO_TITLE);
&&&&&getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
&&&&&&&&&&&&&WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
&&&&&DisplayMetrics outMetrics =&new DisplayMetrics();
&&&&&this.getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
&&&&&GameSurfaceView.SCREEN_WIDTH = outMetrics.widthP
&&&&&GameSurfaceView.SCREEN_HEIGHT = outMetrics.heightP
&&&&&GameSurfaceView gameView =&new GameSurfaceView(this);
&&&&&setContentView(gameView);
现在运行Android工程,应该就可以看到一个手持宝剑的武士在沿着屏幕不停的走了。&>&&>&&>&&>&SurfaceView+MediaPlayer视频播放以及小窗口和全屏播放
SurfaceView+MediaPlayer视频播放以及小窗口和全屏播放
上传大小:363KB
SurfaceView+MediaPlayer视频播放,包含小窗口和全屏播放
综合评分:0(0位用户评分)
所需积分:3
下载次数:6
审核通过送C币
创建者:jin80506
创建者:iwanghang
创建者:chenchenfly
课程推荐相关知识库
上传者其他资源上传者专辑
移动开发热门标签
VIP会员动态
android服务器底层网络模块的设计方法
所需积分:0
剩余积分:720
您当前C币:0
可兑换下载积分:0
兑换下载分:
兑换失败,您当前C币不够,请先充值C币
消耗C币:0
你当前的下载分为234。
SurfaceView+MediaPlayer视频播放以及小窗口和全屏播放
会员到期时间:
剩余下载次数:
你还不是VIP会员
开通VIP会员权限,免积分下载
你下载资源过于频繁,请输入验证码
你下载资源过于频繁,请输入验证码
您因违反CSDN下载频道规则而被锁定帐户,如有疑问,请联络:!
若举报审核通过,可奖励20下载分
被举报人:
hongzhan012
举报的资源分:
请选择类型
资源无法下载
资源无法使用
标题与实际内容不符
含有危害国家安全内容
含有反动色情等内容
含广告内容
版权问题,侵犯个人或公司的版权
*详细原因:Android&SurfaceView的详细用法
作者:Legend
上一节讨论了SurfaceView的初步知识,这一节将通过一个简单的例子来进一步学习SurfaceView。
本节将学习一个例子来对上一节内容做个总结,该例子讲演示一个篮球上下运动的动画。java
eye的blog贴图还需要将图片上传到其他网站在转帖,我实在觉得有些麻烦,所以就不贴图了。大家下载之后再机子上运行一下就ok了。
Activity中很简单,代码中需要注意的地方与知识点都已经给了注释,代码如下:
Java代码&&
package&cn.edu.heut.&&
import&android.app.A&&
import&android.content.pm.ActivityI&&
import&android.os.B&&
import&android.util.DisplayM&&
import&android.view.W&&
import&android.view.WindowM&&
public&class&SportActivity&extends&Activity&{&&
&&&&&&&&&&
&&&&&&&&&public&int&screenWidth&;&&
&&&&&&&&&public&int&screenHeight&;&&
&&&&&&&&&BallSurfaceView&bsv&;&&
&&&&@Override&&
&&&&public&void&onCreate(Bundle&savedInstanceState)&{&&
&&&&&&&&super.onCreate(savedInstanceState);&&
&&&&&&&&bsv&=&new&BallSurfaceView(this);&&
&&&&&&&&//获得屏幕尺寸&&
&&&&&&&&DisplayMetrics&dm&=&new&DisplayMetrics();&&
&&&&dm&=&this.getApplicationContext().getResources().getDisplayMetrics();&&
&&&&&&&&&&&&&&screenWidth&=&dm.widthP&&
&&&&&&&&&&&&&&screenHeight&=&dm.heightP&&
&&&&&&&&&&&&//下两句为设置全屏&&
&&&&&&&&requestWindowFeature(Window.FEATURE_NO_TITLE);&&
&&&&&&&&&&&&&&&&&&&getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN&,&&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&WindowManager.LayoutParams.FLAG_FULLSCREEN);&&
&&&&&&&&setContentView(bsv);&&
接下来介绍球类:
Java代码&&
&strong&package&cn.edu.heut.&&
import&android.graphics.B&&
import&android.graphics.BitmapF&&
import&android.graphics.C&&
import&android.graphics.C&&
import&android.graphics.P&&
import&android.util.DisplayM&&
public&class&Ball&{&&
&&&&&&&&&&&&
&&&&&&&&&&&
&&&&&&&&&public&static&final&int&HEIGHT&=&93;&&
&&&&&&&&&&&
&&&&&&&&&public&static&final&int&WIDTH&=&93;&&
&&&&&&&&&private&static&final&int&STEPLENGTH&=&10;//每次运动的间距&&
&&&&&&&&&private&static&final&float&REDUCEPERCENTAGE&=&&0.35F;//递减系数&&
&&&&&&&&&private&int&stepReduce&;//每次反向运动的缩短的距离&&
&&&&&&&&&&
&&&&&&&&&private&float&runX&;//球的位置&&
&&&&&&&&&private&float&runY&;//球的位置&&
&&&&&&&&&private&BallSurfaceView&bsv&;&&
&&&&&&&&&private&boolean&upDirection&=&false;//if&true,up&direction,or&is&down&direction&&
&&&&&&&&&private&float&maxHeight&;//当前运动最高的高度&&
&&&&&&&&&private&Paint&paint&;&&
&&&&&&&&&&
&&&&&&&&&Bitmap&ballBitmap&;//球的图片&&
&&&&&&&&&SportActivity&sa&;&&
&&&&&&&&&public&Ball(float&initX&,&float&initY&,&BallSurfaceView&bsv){&&
&&&&&&&&&&&&&&&&&&&this.runX&&=&initX;&&
&&&&&&&&&&&&&&&&&&&this.runY&=&initY&;&&
&&&&&&&&&&&&&&&&&&&maxHeight&=&initY;&&
&&&&&&&&&&&&&&&&&&&this.bsv&=&&&
&&&&&&&&&&&&&&&&&&&ballBitmap&=&BitmapFactory.decodeResource(bsv.getResources(),&R.drawable.ball);//加载图片&&
&&&&&&&&&&&&&&&&&&&paint&=&new&Paint();&&
&&&&&&&&&&&&&&&&&&&sa&=&bsv.sportA&&
&&&&&&&&&}&&
&&&&&&&&&&
&&&&&&&&&public&void&onDraw(Canvas&canvas)&{&&
&&&&&&&&&&&&&&&&&&&int&c&=&paint.getColor();//保存颜色,之后还原为之前颜色&&
&&&&&&&&&&&&&&&&&&&boundaryTest();&&
&&&&&&&&&&&&&&&&&&&if(canvas&!=&null)&canvas.drawBitmap(ballBitmap,runX,runY,paint);&&
&&&&&&&&&&&&&&&&&&&paint.setColor(c);&&
&&&&&&&&&&&&&&&&&&&move();&&
&&&&&&&&&}&&
&&&&&&&&&&&
&&&&&&&&&private&void&move()&{&&
&&&&&&&&&&&&&&&&&&&if(maxHeight&&=&(sa.screenHeight&-&HEIGHT))&{&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&return;&&
&&&&&&&&&&&&&&&&&&&}&&
&&&&&&&&&&&&&&&&&&&if(upDirection){//向上&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&runY&=&runY&+&STEPLENGTH&;&&
&&&&&&&&&&&&&&&&&&&}else{&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&runY&=&runY&-&STEPLENGTH&;&&
&&&&&&&&&&&&&&&&&&&}&&
&&&&&&&&&}&&
&&&&&&&&&&&
&&&&&&&&&private&void&boundaryTest(){&&
&&&&&&&&&&&&&&&&&&&if(runY&&&sa.screenHeight&-&HEIGHT){//向下运动到头&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&upDirection&=&!upD//方向置反&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&runY&=&sa.screenHeight&-&HEIGHT;&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&stepReduce&=&(int)&(maxHeight&*&REDUCEPERCENTAGE);&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&maxHeight&=&maxHeight&+&stepReduce&;//最大高度递减&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&}&&
&&&&&&&&&&&&&&&&&&&if(runY&&&maxHeight&){//向上运动到头&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&upDirection&=&!upD//方向置反&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&if(maxHeight&&=&(sa.screenHeight&-&HEIGHT))&return;&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&runY&=&maxHeight&;&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&}&&
&&&&&&&&&}&&
}&/strong&&&
SurfaceView类:
Java代码&&
&strong&package&cn.edu.heut.&&
import&android.R.&&
import&android.content.C&&
import&android.graphics.C&&
import&android.graphics.C&&
import&android.graphics.P&&
import&android.view.SurfaceH&&
import&android.view.SurfaceV&&
public&class&BallSurfaceView&extends&SurfaceView&&
implements&SurfaceHolder.Callback{&&
&&&&&&&&&SportActivity&sportActivity&;//调用该SurfaceView的上下文引用&&
&&&&&&&&&private&Ball&ball&;//小球&&
&&&&&&&&&SurfaceHolder&holder&;&&
&&&&&&&&&&
&&&&&&&&&public&BallSurfaceView(Context&context)&{&&
&&&&&&&&&&&&&&&&&&&super(context);&&
&&&&&&&&&&&&&&&&&&&this.sportActivity&=&(SportActivity)context&;&&
&&&&&&&&&&&&&&&&&&&ball&=&new&Ball(100,&100,&this);&&
&&&&&&&&&&&&&&&&&&&holder&=&this.getHolder();&&
&&&&&&&&&&&&&&&&&&&holder.addCallback(this);&&
&&&&&&&&&}&&
&&&&&&&&&@Override&&
&&&&&&&&&protected&void&onDraw(Canvas&canvas)&{&&
&&&&&&&&&&&&&&&&&&&super.onDraw(canvas);&&
&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&if(canvas&==&null)&canvas&=&holder.lockCanvas();//锁定画布&&
&&&&&&&&&&&&&&&&&&&Paint&p&=&new&Paint();&&
&&&&&&&&&&&&&&&&&&&int&c&=&p.getColor();&&
&&&&&&&&&&&&&&&&&&&p.setColor(Color.WHITE);//设置背景白色&&
&&&&&&&&&&&&&&&&&&&if(canvas&!=&null)&&
&&&&&&&&&&&&&&&&&&&canvas.drawRect(0,&0,&sportActivity.screenWidth,&sportActivity.screenHeight,&p);&&
&&&&&&&&&&&&&&&&&&&p.setColor(c);&&
&&&&&&&&&&&&&&&&&&&ball.onDraw(canvas);&&
&&&&&&&&&&&&&&&&&&&holder.unlockCanvasAndPost(canvas);//释放锁&&
&&&&&&&&&}&&
&&&&&&&&&@Override&&
&&&&&&&&&public&void&surfaceChanged(SurfaceHolder&holder,&int&format,&int&width,&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&int&height)&{&&
&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&}&&
&&&&&&&&&@Override&&
&&&&&&&&&public&void&surfaceCreated(SurfaceHolder&holder)&{&&
&&&&&&&&&&&&&&&&&&&new&RefreshThread().start();&&
&&&&&&&&&}&&
&&&&&&&&&@Override&&
&&&&&&&&&public&void&surfaceDestroyed(SurfaceHolder&holder)&{&&
&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&}&&
&&&&&&&&&&
&&&&&&&&&private&class&RefreshThread&extends&Thread{&&
&&&&&&&&&&&&&&&&&&&@Override&&
&&&&&&&&&&&&&&&&&&&public&void&run()&{&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&while(true){&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&Canvas&canvas&=&null;&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&try{&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&onDraw(canvas);&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&}catch(Exception&e){&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&e.printStackTrace();&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&}&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&try&{&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&Thread.sleep(40);&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&}&catch&(InterruptedException&e)&{&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&e.printStackTrace();&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&}&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&}&&
&&&&&&&&&&&&&&&&&&&}&&
&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&}&&
}&/strong&&&
代码中的注释已经很清楚了,具体原理参考上一节的内容。如需代码,留下邮箱。
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。}

我要回帖

更多关于 surfaceview双击全屏 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信