Android刮刮乐效果

今天看到一个关于刮刮卡的库,经过测试,感觉还不错,使用方法也比较简单,在这里分享一下。

    Android刮刮乐效果

    github地址:https://github.com/D-clock/ScratchView

    1.xml布局:

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     tools:context="test.riven.com.testscratchview.MainActivity">  
  7.   
  8.     <FrameLayout  
  9.         android:id="@+id/frame"  
  10.         android:layout_width="200dp"  
  11.         android:layout_height="200dp"  
  12.         android:layout_gravity="center_horizontal"  
  13.         android:layout_marginTop="8dp">  
  14.   
  15.         <!-- content view -->  
  16.         <TextView  
  17.             android:textColor="#Ff00"  
  18.             android:textSize="30sp"  
  19.             android:text="一等奖"  
  20.             android:gravity="center"  
  21.             android:layout_width="match_parent"  
  22.             android:layout_height="match_parent" />  
  23.   
  24.         <!-- scratch view -->  
  25.         <com.clock.scratch.ScratchView  
  26.             android:id="@+id/scratch_view"  
  27.             android:layout_width="match_parent"  
  28.             android:layout_height="match_parent" />  
  29.     </FrameLayout>  
  30.   
  31.     <Button  
  32.         android:id="@+id/btnRest"  
  33.         android:layout_below="@+id/frame"  
  34.         android:text="Rest"  
  35.         android:layout_width="wrap_content"  
  36.         android:layout_height="wrap_content" />  
  37.   
  38.   
  39.   
  40. </RelativeLayout>  

    2.MianActivity

[java] view plain copy
  1. public class MainActivity extends AppCompatActivity {  
  2.   
  3.     private ScratchView scratchView;  
  4.   
  5.     @Override  
  6.     protected void onCreate(Bundle savedInstanceState) {  
  7.         super.onCreate(savedInstanceState);  
  8.         setContentView(R.layout.activity_main);  
  9.         scratchView = findViewById(R.id.scratch_view);  
  10.   
  11.         /** 设置刮卡时线条的宽度 */  
  12.         scratchView.setEraserSize(80);  
  13.   
  14.         /** 设置最大的擦除比例 */  
  15.         //scratchView.setMaxPercent(80);  
  16.   
  17.   
  18.         /** 设置蒙板红色 */  
  19.         //scratchView.setMaskColor(0xffff0000);  
  20.   
  21.         /** 设置蒙版水印 */  
  22.         //scratchView.setWatermark(R.mipmap.ic_launcher);  
  23.   
  24.         /** 设置刮卡时的监听 */  
  25.         scratchView.setEraseStatusListener(new ScratchView.EraseStatusListener() {  
  26.             @Override  
  27.             public void onProgress(int percent) {  
  28.                 /** 最大露出面积达80%时,移除蒙版,全部展示 */  
  29.                 if (percent > 80){  
  30.                     scratchView.clear();  
  31.                 }  
  32.             }  
  33.             @Override  
  34.             public void onCompleted(View view) {  
  35.   
  36.             }  
  37.         });  
  38.   
  39.         findViewById(R.id.btnRest).setOnClickListener(new View.OnClickListener() {  
  40.             @Override  
  41.             public void onClick(View view) {  
  42.                 /** 重新设置 */  
  43.                 scratchView.reset();  
  44.             }  
  45.         });  
  46.     }  
  47. }  

    3.api接口

    Android刮刮乐效果