user303730
user303730

Reputation: 394

Android canvas drawing not visible

If i use the ImageView , i can see the image on my activity , however i can't see when i draw it with a canvas. am i missing something ?. The drawable has the dimensions 479px*100px. I can see onDraw function is being called using a breakpoint inside it.

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;
}

adding it to view by ,

mainLayout.addView(getScrollingImage());

I am on Android 4.0.4.

Upvotes: 1

Views: 1734

Answers (1)

user303730
user303730

Reputation: 394

Got this working by overriding the onMeasure function in view , got this from Android: Drawing on Canvas in Scrollview Thanks stackOverflow.

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

Upvotes: 3

Related Questions