刮刮乐 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接口

    

    Demo下载

    

    

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/dong_junshuai/article/details/80434016
海参的功效,北京人不知道海参有这些功效!沈阳盛鼎 · 顶新

Android 刮刮卡效果 自定义控件实现

2014年10月18日 4.66MB 下载

打印机出租,新机销售,选弘顺瑞通,绿色节能办公的首选!弘顺瑞通 · 顶新

android刮刮卡效果实现

2017年06月10日 27.95MB 下载

Android实现自定义刮刮卡效果View(源码)

2014年12月31日 1.66MB 下载

个人资料

原创
55
粉丝
11
喜欢
2
评论
13
等级:
访问:
3万+
积分:
884
排名:
6万+
勋章:

最新评论

猜你喜欢

转载自blog.csdn.net/s2421458535/article/details/80538956