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

}

来自:安卓教程网

Android自定义surfaceView显示多张图片的更多相关文章

  1. android 自定义Toast显示风格

    1.创建一个自己想要显示Toast风格的XML如下代码(toast_xml.xml): <?xml version="1.0" encoding="utf-8&qu ...

  2. 天气渐热,来片雪花降降温——Android自定义SurfaceView实现雪花效果

    实现雪花的效果其实也可以通过自定义View的方式来实现的(SurfaceView也是继承自View的),而且操作上也相对简单一些,当然也有一些不足啦... 相对于View,SurfaceView有如下 ...

  3. Android 自定义 ListView 显示网络上 JSON 格式歌曲列表

    本文内容 环境 项目结构 演示自定义 ListView 显示网络上 JSON 歌曲列表 参考资料 本文最开始看的是一个国人翻译的文章,没有源代码可下载,根据文中提供的代码片段,自己新建的项目(比较可恶 ...

  4. Android自定义PopupWindow显示在控件上方或者下方

    记录学习之用 View view = mInflater.inflate(R.layout.layout_popupwindow, null); PopUpwindowLayout popUpwind ...

  5. Android自定义滑动显示隐藏布局

    方式一:上下左右滑动显示隐藏布局 总结代码地址: http://git.oschina.net/anan9303/customView参考例子: http://www.jianshu.com/p/fc ...

  6. Android自定义View (二) 进阶

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/24300125 继续自定义View之旅,前面已经介绍过一个自定义View的基础的例 ...

  7. Android自定义View

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/24252901 很多的Android入门程序猿来说对于Android自定义View ...

  8. Android 自定义View (二) 进阶

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/24300125 继续自定义View之旅,前面已经介绍过一个自定义View的基础的例 ...

  9. Android 自定义 ListView 上下拉动“刷新最新”和“加载更多”歌曲列表

    本文内容 环境 测试数据 项目结构 演示 参考资料 本文演示,上拉刷新最新的歌曲列表,和下拉加载更多的歌曲列表.所谓"刷新最新"和"加载更多"是指日期.演示代码 ...

随机推荐

  1. 说说设计模式~组合模式(Composite)

    返回目录 何时能用到它? 组合模式又叫部分-整体模式,在树型结构中,模糊了简单元素和复杂元素的概念,客户程序可以向处理简单元素一样来处理复杂元素,从而使得客户程序与复杂元素的内部结构解耦.对于今天这个 ...

  2. PHP 函数基础

    1.简单函数     函数四要素:返回类型,函数名,参数列表,函数体 function Show(){      echo "hello";}Show();         输出了 ...

  3. Ubuntu14中supervisor的安装及配置

    supervisor是一款很好用的进程管理工具,其命令也很简单,其安装过程如下: Ubuntu14: 首先保证本地的Python环境是OK的,并且已经安装supervisor包,如果没有安装可以用ea ...

  4. ListView优化为何ViewHolder用static类(转载)

    如果有人还不了解ViewHolder为什么可以起到优化作用,我这边再做下简单说明:Android的findViewById动作是比较耗时的,需要遍历布局的树形结构,才能找到相应的视图.所以如果想在这一 ...

  5. Object.create

    var emptyObject = Object.create(null); var emptyObject = Object.create(null); var emptyObject = {}; ...

  6. Android入门(六)碎片

    原文链接:http://www.orlion.ga/493/ 一.碎片 碎片(Fragment)是一种可以嵌入在活动当中的 UI片段,它能让程序更加合理和充分地利用大屏幕的空间,因而在平板上应用的非常 ...

  7. CSS实现水平垂直同时居中的5种思路

    × 目录 [1]水平对齐+行高 [2]水平+垂直对齐 [3]margin+垂直对齐[4]absolute[5]flex 前面的话 水平居中和垂直居中已经单独介绍过,本文将介绍水平垂直同时居中的5种思路 ...

  8. Html标签之frameset&图片切换

    今天为大家分享一下刚刚总结好的html经验,以备不时之需. 首先介绍一下frameset标签,此标签用于同一页面内切换网页,在大多数网页中都可以看到,因为项目的需要,故而研究一二. frameset标 ...

  9. 【吐槽】VS2012的安装项目只能用InstallShield Limited Edition

    以前版本的Visual Stuido中安装项目都可以使用微软自家的Visual Studio Installer,但是到了VS2012这一切都变了,只能用InstallShield Limited E ...

  10. 《BI那点儿事》Microsoft 决策树算法

    Microsoft 决策树算法是由 Microsoft SQL Server Analysis Services 提供的分类和回归算法,用于对离散和连续属性进行预测性建模.对于离散属性,该算法根据数据 ...