Android-WScratchView icon indicating copy to clipboard operation
Android-WScratchView copied to clipboard

Resize scratchview programatically

Open maneca opened this issue 9 years ago • 12 comments

Hello,

First of all, thank you for the work you have made by creating this library. It is great!

I have an issue. I have been trying to resize the view that holds the image to scratch but I cannot reset the values for the height or the width. I can reset the margins but I can't resize it. Can you help me?

Here is my code:

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
float y_factor = (float) ((double) height)/720;
float x_factor = (float) ((double) width)/1196;
ImageMargins next_image_margins = margins[next_image];

params.setMargins(Math.round((next_image_margins.getLeft()) * x_factor),Math.round((next_image_margins.getTop()) * y_factor),Math.round((next_image_margins.getRight()) * x_factor),Math.round((next_image_margins.getBottom()) * y_factor));

to_erase.setLayoutParams(params);
to_erase.getLayoutParams().height = 2000;
to_erase.getLayoutParams().width = 500;

to_erase.requestLayout();
to_erase.setScratchAll(false);
updatePercentage(0f);

maneca avatar Sep 02 '15 14:09 maneca

I think you should setLayoutParam after setting height and width

android.view.ViewGroup.LayoutParams layoutParam = scratchView.getLayoutParams();
layoutParam.height=2000;
layoutParam.width=500;
scratchView.setLayoutParams(layoutParam);

winsontan520 avatar Sep 03 '15 02:09 winsontan520

Hello winsontan520.

Thanks for your reply :)

I have tried that but it did not work. I must be doing something very stupid... :/

maneca avatar Sep 03 '15 11:09 maneca

Hi @winsontan520,

I managed to solve this issue by re-defining the drawable:

Drawable dr = getResources().getDrawable(R.drawable.imagem_jj);
Bitmap bitmap = ((BitmapDrawable) dr).getBitmap();

Drawable d = new BitmapDrawable(getResources(),
Bitmap.createScaledBitmap(bitmap,Math.round(sizes[next_image].getWidth()*x_factor), Math.round(sizes[next_image].getHeight()*y_factor), true));

to_erase.setScratchDrawable(d);
to_erase.setLayoutParams(params);

On another topic, I noticed that sometimes, when I pass the finger over the image I want to scratch, nothing happens. From my tests, it appears that the issue is with the Android API, mainly the onTouch method.

Did you notice this also?

maneca avatar Sep 04 '15 14:09 maneca

it was designed as scratch to reveal but not touch to reveal, mean you need to touch and drag it to reveal. On 4 Sep, 2015 10:28 pm, "Joao Ferreira" [email protected] wrote:

Hi winsontan520,

I managed to solve this issue by re-defining the drawable:

Drawable dr = getResources().getDrawable(R.drawable.imagem_jj); Bitmap bitmap = ((BitmapDrawable) dr).getBitmap();

Drawable d = new BitmapDrawable(getResources(), Bitmap.createScaledBitmap(bitmap,Math.round(sizes[next_image].getWidth()_x_factor), Math.round(sizes[next_image].getHeight()_y_factor), true));

to_erase.setScratchDrawable(d); to_erase.setLayoutParams(params);

On another topic, I noticed that sometimes, when I pass the finger over the image I want to scratch, nothing happens. From my tests, it appears that the issue is with the Android API, mainly the onTouch method.

Did you notice this also?

— Reply to this email directly or view it on GitHub https://github.com/winsontan520/Android-WScratchView/issues/18#issuecomment-137750600 .

winsontan520 avatar Sep 05 '15 14:09 winsontan520

@winsontan520

I know that but sometimes when I scratch the image nothing happens. I tested in different smartphones and it happens in all of them but as I said is not always, only sometimes.

Does this also happen to you?

maneca avatar Sep 05 '15 14:09 maneca

Hi Joao,

Test it in release apk.

2015-09-05 10:26 GMT-04:00 Joao Ferreira [email protected]:

@winsontan520 https://github.com/winsontan520

I know that but sometimes when I scratch the image nothing happens. I tested in different smartphones and it happens in all of them but as I said is not always, only sometimes.

Does this also happen to you?

— Reply to this email directly or view it on GitHub https://github.com/winsontan520/Android-WScratchView/issues/18#issuecomment-137960189 .

gilbert1991 avatar Sep 05 '15 14:09 gilbert1991

Hi @gilbert1991

I have tested it and it also happened. I think that it may not have to be with the library itself but with the Android API. I am just asking to see if this is something that you are aware of.

maneca avatar Sep 05 '15 14:09 maneca

I met this in debug mode but not release versions. Sometimes just delayed reveal so try smaller revealSize.

gilbert1991 avatar Sep 05 '15 14:09 gilbert1991

I also noticed the delayed reveal but that for me is not a "problem".

What is happening is that nothing happens when I scratch. I tried to debug it and the onTouch method that you use on your library, in the situations where nothing happens, it does not recognize the touch on the screen.

maneca avatar Sep 05 '15 14:09 maneca

See if onTouch was called or override. Otherwise should be devices problem

gilbert1991 avatar Sep 05 '15 19:09 gilbert1991

OnTouch is not called when this issue happens.

maneca avatar Sep 08 '15 09:09 maneca

Hello again @gilbert1991,

we have discovered why this is happening. If the user's first touch is not on the image that he wants to erase then his movements are not recognized and t ans even though he can pass with his finger over the image, nothing is scratched.

I am trying to figure out a way to overcome this.

maneca avatar Sep 13 '15 11:09 maneca