提问者:小点点

Android画布绘制不可见


如果我使用ImageView,我可以在我的活动中看到图像,但是当我用画布绘制它时我看不到。我错过了什么吗?。可绘制的尺寸为479px*100px。我可以看到onDraw函数正在使用其中的断点调用。

public View getScrollingImage(){
    View temp = new View(this){
        BitmapDrawable cloud=  (BitmapDrawable) MainActivity.this.getResources().getDrawable(R.drawable.cartoon_clouds);
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            canvas.drawBitmap(cloud.getBitmap(), 0,0, null);
            invalidate();
        }
    };
    //ImageView temp = new ImageView(this);
    //temp.setImageDrawable(this.getResources().getDrawable(R.drawable.cartoon_clouds));
    temp.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT,1.0f));

    return temp;
}

添加它以查看,

mainLayout.addView(getScrollingImage());

我在Android4.0.4上。


共1个答案

匿名用户

通过覆盖视图中的onMeasure函数使其工作,从Android获得:在Scrollview中绘制画布感谢stackOverflow。

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    setMeasuredDimension(cloud.getBitmap().getWidth(),
            cloud.getBitmap().getHeight());
}