Skip to content
This repository has been archived by the owner on Dec 1, 2017. It is now read-only.

DesignSpec as Drawable. #5

Closed
pepyakin opened this issue Sep 22, 2014 · 2 comments
Closed

DesignSpec as Drawable. #5

pepyakin opened this issue Sep 22, 2014 · 2 comments

Comments

@pepyakin
Copy link

What about Drawable adapter to design spec?
I see following use case for it.

public class DesignSpecDrawable extends Drawable {
    private final DesignSpec mDesignSpec;

    public DesignSpecDrawable(DesignSpec designSpec) {
        mDesignSpec = designSpec;
    }

    @Override
    public void draw(Canvas canvas) {
        if (mDesignSpec != null) {
            mDesignSpec.draw(canvas);
        }
    }

    /* ... */
}
setContentView(R.layout.main_activity);
/* Resolve views here */

View decorView = getWindow().getDecorView();
mDesignSpec = DesignSpec.fromResource(decorView, R.raw.main_activity_spec);
DesignSpecDrawable drawable = new DesignSpecDrawable(mDesignSpec);
decorView.getOverlay().add(drawable); 

The main advantage that is dspec now controlled from the code. It means we can now easily make it conditional, like:

if (BuildConfig.USE_DSPEC) {
  // ...
}

So it can be stripped from production builds with ease.

However, there is one problem ViewOverlays are only available since API 18 (JB 4.3). Although, we can still use it below this API level by setting it as background, or ImageView or so on, which is not so convient.

@lucasr lucasr closed this as completed in fc3769f Oct 9, 2014
@lucasr
Copy link
Owner

lucasr commented Oct 9, 2014

That's a nice idea. DesignSpec is now a Drawable. You can just do:

DesignSpec designSpec = DesignSpec.fromResource(someView, R.raw.some_spec);
someView.getOverlay().add(designSpec);

@pepyakin
Copy link
Author

pepyakin commented Oct 9, 2014

Great! Thank you.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants