Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use ScratchableLinearLayout Programmatically #15

Closed
Kida007 opened this issue Sep 18, 2019 · 3 comments
Closed

use ScratchableLinearLayout Programmatically #15

Kida007 opened this issue Sep 18, 2019 · 3 comments

Comments

@Kida007
Copy link

Kida007 commented Sep 18, 2019

Hey I was trying to use this sweet library via React-Native . so i tried something like that :


ScratchableLinearLayout view = new ScratchableLinearLayout(reactContext);

Canvas imageCanvas = new Canvas(resource);

 ScratchoffController controller = new ScratchoffController(reactContext)
.setThresholdPercent(0.40d)
.setTouchRadiusDip(reactContext, 30)
.setFadeOnClear(true)
 .setClearOnThresholdReached(true);
                        
controller.draw(imageCanvas);
                        
view.initialize(controller);

Looks like I made some strong assumptions to make it work and this is not working(lol) . What would be the correct way of Programmatically (purely) using this library .

@mattsilber
Copy link
Collaborator

Can you give some more context of what's not working to help troubleshoot your case? Are those views actually being added to the hierarchy? Are they correctly overlapping one another?

Have you also tried calling the attach function on the controller with the scratchable/behind views?
Relevant sample from the readme:

ScratchoffController controller = new ScratchoffController(context)
        ........
        .setClearOnThresholdReached(true)
        .attach(findViewById(R.id.scratch_view), findViewById(R.id.scratch_view_behind));

Where findViewById(R.id.scratch_view) would be your ScratchableLinearLayout and findViewById(R.id.scratch_view_behind) would be the hidden layout you're trying to reveal.

I don't believe n unattached canvas would suffice in any case, as there'd never be anything attached to the UI to draw to.

@Kida007
Copy link
Author

Kida007 commented Sep 18, 2019

Thanks for so quick reply. I was actually just quickly hacking to make things work ( my bad )

#Context

so I was trying to render it from React-Native as a Native Module. This can be done defining a class extending SimpleViewManger and then registering and stuff( irrelevant).

Relevant :

so this class has function called createViewInstance which will return any type of View and that will be rendered ultimately . so there are no R.idsss and stuff

For my case I want to render ScratchableLinearLayout

@Override
    protected ScratchableLinearLayout createViewInstance(@Nonnull ThemedReactContext reactContext) {
        this.reactContext = reactContext;
        ScratchableLinearLayout scratchImageView = new ScratchableLinearLayout(reactContext);

        return  scratchImageView;
    }

Now I just want to just render the scratch part programmatically , the bottom part I will render through JS (react-native) [ so we can ignore that ].

So How can I possibly set a canvas and controller to this scratchImageView. Thanks

@mattsilber
Copy link
Collaborator

so there are no R.idsss and stuff

That's ok, Android's findViewById(Int) method returns a View instance, which is what the ScratchoffProcessor takes as arguments. In this case, using the View reference from createViewInstance() should be what you need to use.

the bottom part I will render through JS (react-native) [ so we can ignore that ]

I don't think you can ignore it here, as the ScratchoffProcessor needs both a scratchable/foreground (the scratchable surface) View and the behind/background (the content being revealed) View in order to function properly.

When you attach the processor to both the scratchable and behind Views, it renders the drawing cache of the behind View to a Bitmap, hides the real content, attaches the necessary touch interceptors, and starts the background processors that make it all work together.

Meaning, it should work as long as you have a real View to use for the behind view that you can pass to the processor's attach(View,View) method.

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

No branches or pull requests

2 participants