android - Passing arguments to SurfaceView via Constructor -


this might not best practice seems easiest. need pass bitmap surfaceview ready ondraw called. cant seem figure out syntax when assigned. note bitmap in 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 assign it, don't have clue how set view , pass bitmap

setcontentview(r.layout.sample); //has custom view msampleview = (sampleview) findviewbyid(r.id.sampleview); 

this maybe me not understanding ways can init view. have seen examples using "addview()". base goal of display bitmap in surfaceview. based on googling cant access canvas other ondraw need pass in when init (i think). i'm not sure constructor called when set var "msampleview" or when use setcontentview() call layout. if latter mute , makes me wonder if there way access canvas or target main activity. if target main activity access there when needed dont have grasp on scope in android/java.

is bad practice pass data way? there more standard way passdata other global datastore.

to clarify comment bit more...you can't pass object directly constructor of class when inherited class doesn't support object in constructor call.

you have pass bitmap afterwards in method call, either via value, or passing reference object.

your getting extremely confused way ondraw method works objects uses. know because had trouble understanding first time also.

here example of do.

first construct view , give view bitmap

private bitmap myreferencedbitmap;  public sampleview (context context, attributeset attrs) {      super(context, attrs); }  

now pass in bitmap object want use.

public foobitmap(bitmap b) {      myreferencedbitmap = b; } 

now use ondraw referenced bitmap

protected void ondraw(canvas canvas) {      canvas.drawbitmap(myreferencedbitmap, null, null, paint); } 

now whenever modify object b passed sampleview class, , invalidate view, ondraw method use new object, because myreferencedbitmap holds reference (object) value.

another thing @ byref vs byval in java.

good luck.


Comments