Reputation: 959
This might not be the best practice for this but its seems like it would be the easiest. I need to pass a bitmap to a SurfaceView so its ready with onDraw is called. But I cant seem to figure out the syntax for when its assigned. Note the extra bitmap in the constructor.
public class SampleView extends SurfaceView implements SurfaceHolder.Callback {
SurfaceHolder holder;
Bitmap mBitmap = null;
public SampleView (Context context, AttributeSet attrs, Bitmap bitmap) {
super(context, attrs);
if(bitmap != null)
mBitmap = bitmap
SurfaceHolder holder = getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
protected void onDraw(Canvas canvas) {
Paint paint = new Paint();
paint.setFilterBitmap(true);
double aspectRatio = ((double) mBitmap.getWidth()) / mBitmap.getHeight();
Rect dest = new Rect(0, 0, this.getWidth(),(int) (this.getHeight() / aspectRatio));
canvas.drawBitmap(mBitmap, null, dest, paint);
}
Then i'm trying to assign it, but I don't have a clue how to set the view and pass the bitmap
setContentView(R.layout.sample); //has my custom view
mSampleView = (SampleView) findViewById(R.id.sampleView);
This maybe me not understanding the ways you can init a view. I have seen examples using "addview()". The base goal of this is to display a bitmap in a surfaceview. Based on my googling you cant access the canvas other than the onDraw so I would need to pass it in when its init (i think). I'm also not sure that the constructor is called when I set the var "mSampleView" or when I use setContentView() to call the layout. If its the latter all this mute and makes me wonder if there is a way to access the canvas or target the main activity. If I could target the main activity i could just access it there when needed but I dont have a very good grasp on scope in android/java.
Is it a bad practice to pass data this way? Would be there a more standard way to passdata other than a global datastore.
Upvotes: 2
Views: 3048
Reputation: 20167
To clarify my comment a bit more...You can't pass another object directly to the constructor of a class when its inherited class doesn't support that object in the constructor call.
You would have to pass the bitmap afterwards in another method call, either via the value, or by passing a reference to the object.
Your getting extremely confused with the way the onDraw method works with the objects that it uses. I know because I had trouble understanding it for the first time also.
Here is an example of something you could do.
First construct the view and give the view a bitmap
private Bitmap myReferencedBitmap;
public SampleView (Context context, AttributeSet attrs) {
super(context, attrs);
}
Now pass in the bitmap object you want to use.
public fooBitmap(Bitmap b) {
myReferencedBitmap = b;
}
Now use onDraw with the referenced bitmap
protected void onDraw(Canvas canvas) {
canvas.drawBitmap(myReferencedBitmap, null, null, paint);
}
Now whenever you modify the object b
that you passed into the SampleView
class, and then invalidate the view, the onDraw
method will use the new object, because myReferencedBitmap
holds a reference to the (object) value.
Another thing to look at is byref vs byval in Java.
Good luck.
Upvotes: 2