Skip to content

Commit 7954b48

Browse files
Mask
1 parent 406a78e commit 7954b48

File tree

3 files changed

+43
-16
lines changed

3 files changed

+43
-16
lines changed

app/src/main/java/com/anupkumarpanwar/scratchview/MainActivity.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,24 @@
55
import android.support.v7.app.AppCompatActivity;
66
import android.os.Bundle;
77
import android.util.Log;
8+
import android.view.View;
9+
import android.widget.Button;
810
import android.widget.Toast;
911

1012
public class MainActivity extends AppCompatActivity {
1113

1214
ScratchView scratchView;
15+
Button mask;
1316

1417
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
1518
@Override
1619
protected void onCreate(Bundle savedInstanceState) {
1720
super.onCreate(savedInstanceState);
1821
setContentView(R.layout.activity_main);
1922

23+
mask = findViewById(R.id.mask);
2024
scratchView = findViewById(R.id.scratch_view);
25+
2126
scratchView.setRevealListener(new ScratchView.IRevealListener() {
2227
@Override
2328
public void onRevealed(ScratchView scratchView) {
@@ -31,5 +36,12 @@ public void onRevealPercentChangedListener(ScratchView scratchView, float percen
3136
}
3237
}
3338
});
39+
40+
mask.setOnClickListener(new View.OnClickListener() {
41+
@Override
42+
public void onClick(View v) {
43+
scratchView.mask();
44+
}
45+
});
3446
}
3547
}
Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,36 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3-
xmlns:app="http://schemas.android.com/apk/res-auto"
2+
<RelativeLayout
43
xmlns:tools="http://schemas.android.com/tools"
5-
android:layout_width="match_parent"
6-
android:layout_height="match_parent"
4+
xmlns:app="http://schemas.android.com/apk/res-auto"
5+
tools:context=".MainActivity"
76
android:gravity="center"
8-
tools:context=".MainActivity">
7+
xmlns:android="http://schemas.android.com/apk/res/android"
8+
android:layout_height="match_parent"
9+
android:layout_width="match_parent">
910

1011
<ImageView
11-
android:gravity="center"
12+
android:layout_centerInParent="true"
1213
android:layout_width="300dp"
1314
android:layout_height="300dp"
14-
android:src="@drawable/anup"/>
15+
android:gravity="center"
16+
android:src="@drawable/anup" />
1517

1618
<com.anupkumarpanwar.scratchview.ScratchView
19+
android:layout_centerInParent="true"
1720
android:id="@+id/scratch_view"
1821
android:layout_width="300dp"
1922
android:layout_height="300dp"
20-
app:tile_mode="CLAMP"
21-
app:overlay_width="300dp"
2223
app:overlay_height="300dp"
23-
/>
24+
app:overlay_width="300dp"
25+
app:overlay_image="@drawable/scratch_card"
26+
app:tile_mode="CLAMP" />
27+
28+
<Button
29+
android:id="@+id/mask"
30+
android:layout_alignParentBottom="true"
31+
android:layout_width="match_parent"
32+
android:layout_height="60dp"
33+
android:gravity="center"
34+
android:text="MASK" />
2435

2536
</RelativeLayout>

scratchview/src/main/java/com/anupkumarpanwar/scratchview/ScratchView.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ public interface IRevealListener {
102102
*/
103103
private int mThreadCount = 0;
104104

105+
Bitmap scratchBitmap;
106+
105107

106108
public ScratchView(Context context) {
107109
super(context);
@@ -163,7 +165,7 @@ private void init() {
163165
tileMode = "CLAMP";
164166
}
165167

166-
Bitmap scratchBitmap = BitmapFactory.decodeResource(getResources(), overlayImage);
168+
scratchBitmap = BitmapFactory.decodeResource(getResources(), overlayImage);
167169
scratchBitmap = Bitmap.createScaledBitmap(scratchBitmap, (int) overlayWidth, (int) overlayHeight, false);
168170
mDrawable = new BitmapDrawable(getResources(), scratchBitmap);
169171

@@ -179,7 +181,6 @@ private void init() {
179181
break;
180182
default:
181183
mDrawable.setTileModeXY(Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
182-
183184
}
184185

185186
}
@@ -220,7 +221,6 @@ protected void onDraw(Canvas canvas) {
220221
super.onDraw(canvas);
221222
canvas.drawBitmap(mScratchBitmap, 0, 0, mBitmapPaint);
222223
canvas.drawPath(mErasePath, mErasePaint);
223-
224224
}
225225

226226
private void touch_start(float x, float y) {
@@ -296,8 +296,14 @@ public void reveal() {
296296
clear();
297297
}
298298

299-
private void touch_up() {
299+
public void mask() {
300+
clear();
301+
mRevealPercent = 0;
302+
mCanvas.drawBitmap(scratchBitmap, 0, 0, mBitmapPaint);
303+
invalidate();
304+
}
300305

306+
private void touch_up() {
301307
drawPath();
302308
}
303309

@@ -350,9 +356,7 @@ public boolean isRevealed() {
350356
}
351357

352358
private void checkRevealed() {
353-
354359
if (!isRevealed() && mRevealListener != null) {
355-
356360
int[] bounds = getViewBounds();
357361
int left = bounds[0];
358362
int top = bounds[1];

0 commit comments

Comments
 (0)