A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© y1787257661 中级黑马   /  2015-1-5 10:43  /  2329 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

自定义了一个surfaceview,我在上面绘制多张图片,让它能够上下方滚显示图片,但是onMeasure()方法在重写的时候遇到了问题,不知道如何设置它的高度
  1. public class MySurfaceView extends SurfaceView implements Callback{
  2.      
  3.     private SurfaceHolder sfh;
  4.     private Paint paint;
  5.     public MySurfaceView(Context context) {
  6.         super(context);
  7.         init(context);
  8.     }

  9.      
  10.      
  11.     private void init(Context context) {
  12.         sfh=this.getHolder();
  13.         sfh.addCallback(this);
  14.         paint=new Paint();
  15.         paint.setColor(Color.WHITE);
  16.          
  17.     }



  18.     public MySurfaceView(Context context, AttributeSet attrs, int defStyle) {
  19.         super(context,attrs,defStyle);
  20.         init(context);
  21.     }



  22.     public MySurfaceView(Context context, AttributeSet attrs) {
  23.         super(context,attrs);
  24.         init(context);

  25.     }



  26.     @Override
  27.     public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {
  28.          
  29.     }

  30.     @Override
  31.     public void surfaceCreated(SurfaceHolder holder) {
  32.         myDraw();
  33.     }

  34.     private void myDraw() {
  35.         Canvas canvas=sfh.lockCanvas();
  36.         canvas.drawRect(0, 0, this.getWidth(), this.getHeight(), new Paint());
  37.         Bitmap bmp=readBitmap(getResources(), R.drawable.pic0);
  38.         Matrix matrix=new Matrix();
  39.          
  40.         matrix.setScale(0.15f, 0.15f);
  41.         matrix.postTranslate(100, 0);
  42.         canvas.drawBitmap(bmp, matrix, paint);
  43.          
  44.         matrix.postTranslate(0, 450);
  45.         bmp=readBitmap(getResources(), R.drawable.pic2);
  46.         canvas.drawBitmap(bmp, matrix, paint);
  47.          
  48.         matrix.postTranslate(0, 500);
  49.         bmp=readBitmap(getResources(), R.drawable.pic5);
  50.         canvas.drawBitmap(bmp, matrix, paint);
  51.          
  52.         matrix.postTranslate(0, 550);
  53.         bmp=readBitmap(getResources(), R.drawable.pic7);
  54.         canvas.drawBitmap(bmp, matrix, paint);
  55.          
  56.         sfh.unlockCanvasAndPost(canvas);
  57.         if(bmp!=null)
  58.         bmp.recycle();
  59.     }

  60.     @Override
  61.     public void surfaceDestroyed(SurfaceHolder holder) {
  62.          
  63.     }

  64.     @Override
  65.     public boolean onTouchEvent(MotionEvent event) {
  66.         return super.onTouchEvent(event);
  67.     }
  68.      
  69.      
  70.     @Override
  71.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  72.         // TODO Auto-generated method stub
  73.         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  74.          
  75.         setMeasuredDimension(800, 2000);//2:5   2000 5000   1600 4000  2:5
  76.     }
  77.          

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

  86. }
复制代码

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马