Android自定义surfaceView显示多张图片
jopen
13年前
我自定义了一个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); } }