Android刮刮乐效果
今天看到一个关于刮刮卡的库,经过测试,感觉还不错,使用方法也比较简单,在这里分享一下。
github地址:https://github.com/D-clock/ScratchView
1.xml布局:
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context="test.riven.com.testscratchview.MainActivity">
- <FrameLayout
- android:id="@+id/frame"
- android:layout_width="200dp"
- android:layout_height="200dp"
- android:layout_gravity="center_horizontal"
- android:layout_marginTop="8dp">
- <!-- content view -->
- <TextView
- android:textColor="#Ff00"
- android:textSize="30sp"
- android:text="一等奖"
- android:gravity="center"
- android:layout_width="match_parent"
- android:layout_height="match_parent" />
- <!-- scratch view -->
- <com.clock.scratch.ScratchView
- android:id="@+id/scratch_view"
- android:layout_width="match_parent"
- android:layout_height="match_parent" />
- </FrameLayout>
- <Button
- android:id="@+id/btnRest"
- android:layout_below="@+id/frame"
- android:text="Rest"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
- </RelativeLayout>
2.MianActivity
- public class MainActivity extends AppCompatActivity {
- private ScratchView scratchView;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- scratchView = findViewById(R.id.scratch_view);
- /** 设置刮卡时线条的宽度 */
- scratchView.setEraserSize(80);
- /** 设置最大的擦除比例 */
- //scratchView.setMaxPercent(80);
- /** 设置蒙板红色 */
- //scratchView.setMaskColor(0xffff0000);
- /** 设置蒙版水印 */
- //scratchView.setWatermark(R.mipmap.ic_launcher);
- /** 设置刮卡时的监听 */
- scratchView.setEraseStatusListener(new ScratchView.EraseStatusListener() {
- @Override
- public void onProgress(int percent) {
- /** 最大露出面积达80%时,移除蒙版,全部展示 */
- if (percent > 80){
- scratchView.clear();
- }
- }
- @Override
- public void onCompleted(View view) {
- }
- });
- findViewById(R.id.btnRest).setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- /** 重新设置 */
- scratchView.reset();
- }
- });
- }
- }
3.api接口