六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 157|回复: 0

surfaceview的使用

[复制链接]

升级  28%

22

主题

22

主题

22

主题

秀才

Rank: 2

积分
92
 楼主| 发表于 2013-1-19 04:14:26 | 显示全部楼层 |阅读模式
1.添加surfaceview上点击事件,左右滚动等事件。
private SurfaceView mPreview;
public SurfaceHolder holder;
mPreview = (SurfaceView) findViewById(R.id.fullplayer);
mPreview.setOnTouchListener(this);
holder = mPreview.getHolder();
holder.addCallback(this);
public boolean onTouch(View v, MotionEvent event) {
return mGestureDetector.onTouchEvent(event);
}

public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
Log.d(TAG, "onDown");
return true;
}

public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
return false;
}

public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub

}

public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
// TODO Auto-generated method stub
return false;
}

public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub

}

public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
Log.d(TAG, "onSingleTapUp");
}
return false;
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
System.out.println("surfaceChanged()");
}

public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
Log.d(TAG, "surfaceCreated");
}

public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub

Log.d(TAG, "surfaceDestroyed");
}
2.在单独的线程中画图
class DrawBitamp extends Thread{
public void run(){
while (mRun) {
                Canvas c = null;
                try {
                    c = mSurfaceHolder.lockCanvas(null);
                    synchronized (mSurfaceHolder) {
                        //画图语句                    }
                } finally {
                    // do this in a finally so that if an exception is thrown
                    // during the above, we don't leave the Surface in an
                    // inconsistent state
                    if (c != null) {
                        mSurfaceHolder.unlockCanvasAndPost(c);
                    }
                }
            }
}
}
3.surfaceview中做游戏一些常用语句
(1)获取drawable中的图片
Resources res = context.getResources();
            // cache handles to our key sprites & other drawables
           Drawable mLanderImage = context.getResources().getDrawable(
                    R.drawable.lander_plain);
(2)drawable->bitmap
Bitamp mBackgroundImage = BitmapFactory.decodeResource(res,
                    R.drawable.earthrise);

            // Use the regular lander image as the model size for all sprites
            mLanderWidth = mLanderImage.getIntrinsicWidth();
            mLanderHeight = mLanderImage.getIntrinsicHeight();
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表