surfaceview的使用

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();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值