`

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();
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics