展会信息港展会大全

Android自定义surfaceView显示多张图片
来源:互联网   发布日期:2015-10-13 07:38:24   浏览:3395次  

导读:我自定义了一个surfaceview,我在上面绘制多张图片,让它能够上下方滚显示图片,但是onMeasure()方法在重写的时候遇到了问题,不知道如何设置它的高度,public class MySurfaceView extends SurfaceView ......

我自定义了一个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);

}

}

赞助本站

人工智能实验室

相关热词: surfaceView

AiLab云推荐
推荐内容
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港