1

I'm developing a small android project of a 'virtual blackboard'. I'm able to draw on my mobile/tablet and the lines are stored in a server to be reproduced in future (lines are sent through socket)

All this stuff is done by using a custom View. Now, I need to add the option of changing the view's background dynamically. By now it does not matter how I'll get this image (from gallery, sent by another app, etc). It's enough for the moment to have something (a file, an inputstream, a Bitmap) and set it as my view's background and then be able to draw on it (I'm still not worrying about sizing, scaling, etc)

I've read some stuff about loading images from resources and assets but none of them apply to my case (IMO) since images are not part of my app.

Any suggestion on how I should proceed?

LucasM
  • 421
  • 2
  • 7
  • 23

4 Answers4

1

When you create a customView just simply right the below code in any of your constructor which are you using

public ShapeView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setBackgroundColor(Color.parseColor("#FFFF00"));
        setBackgroundResource(R.drawable.process_dafault);
        setBackground(ContextCompat.getDrawable(mContext,R.drawable.process_dafault));

    }

Use any of the above method to change Background.

Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300
0

Maybe this could help.
setBackground(Drawable background)

SirAnderson
  • 101
  • 2
  • 14
  • Thanks for your answer! This method is available from API 16 on. I need my app to be compliant with API 13 on. – LucasM Mar 08 '14 at 09:53
  • @LucasM [Here](http://stackoverflow.com/a/12551838/3302510) someone suggested how to use different methods based on different API levels. – SirAnderson Mar 08 '14 at 16:31
0

My first suggestion would be to go check out Picasso, an open source library that do all the work about loading and caching images for you.

If you insted want to go for somthing custom here is an old question about downloading images

Loading remote images

and to set it as background use:

Community
  • 1
  • 1
Mario Lenci
  • 10,422
  • 5
  • 39
  • 50
0

You could override your OnDraw(Canvas canvas) method and use the canvas to draw your image as a bitmap. Like this:

Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pic180);  
Matrix matrix=new Matrix();
matrix.postScale(0.8f, 0.8f);
matrix.postRotate(45);
Bitmap dstbmp=Bitmap.createBitmap(bmp,0,0,bmp.getWidth(),
bmp.getHeight(),matrix,true);
canvas.drawColor(Color.BLACK); 
canvas.drawBitmap(dstbmp, 10, 10, null); 

The code above is tested and works great. Please bear in mind if you are overriding onDraw() then you would want to load the bitmap only once (not every time the view is redrawn

Has AlTaiar
  • 4,052
  • 2
  • 36
  • 37