您的位置:首页 > 移动开发 > Android开发

Android自定义surfaceView显示多张图片

2013-09-26 09:31 417 查看
我自定义了一个surfaceview,我在上面绘制多张图片,让它能够上下方滚显示图片,但是onMeasure()方法在重写的时候遇到了问题,不知道如何设置它的高度,

public class MySurfaceView extends SurfaceView implements Callback{

private SurfaceHolder sfh;
private Paint paint;
public MySurfaceView(Context context) {
super(context);
init(context);
}

private void init(Context context) {
sfh=this.getHolder();
sfh.addCallback(this);
paint=new Paint();
paint.setColor(Color.WHITE);

}

public MySurfaceView(Context context, AttributeSet attrs, int defStyle) {
super(context,attrs,defStyle);
init(context);
}

public MySurfaceView(Context context, AttributeSet attrs) {
super(context,attrs);
init(context);

}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {

}

@Override
public void surfaceCreated(SurfaceHolder holder) {
myDraw();
}

private void myDraw() {
Canvas canvas=sfh.lockCanvas();
canvas.drawRect(0, 0, this.getWidth(), this.getHeight(), new Paint());
Bitmap bmp=readBitmap(getResources(), R.drawable.pic0);
Matrix matrix=new Matrix();

matrix.setScale(0.15f, 0.15f);
matrix.postTranslate(100, 0);
canvas.drawBitmap(bmp, matrix, paint);

matrix.postTranslate(0, 450);
bmp=readBitmap(getResources(), R.drawable.pic2);
canvas.drawBitmap(bmp, matrix, paint);

matrix.postTranslate(0, 500);
bmp=readBitmap(getResources(), R.drawable.pic5);
canvas.drawBitmap(bmp, matrix, paint);

matrix.postTranslate(0, 550);
bmp=readBitmap(getResources(), R.drawable.pic7);
canvas.drawBitmap(bmp, matrix, paint);

sfh.unlockCanvasAndPost(canvas);
if(bmp!=null)
bmp.recycle();
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {

}

@Override
public boolean onTouchEvent(MotionEvent event) {
return super.onTouchEvent(event);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);

setMeasuredDimension(800, 2000);//2:5   2000 5000   1600 4000  2:5
}

public static Bitmap readBitmap(Resources r, int resId) {
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inPreferredConfig = Bitmap.Config.RGB_565;
opt.inPurgeable = true;
opt.inInputShareable = true;
InputStream is = r.openRawResource(resId);
return BitmapFactory.decodeStream(is, null, opt);
}

}


来自:安卓教程网
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: