Android开源刮刮卡库ScratchCardLayout使用教程

Home / Android MrLee 2019-4-18 3675

自己实现这个功能非常简单,绘制2层,然后顶层的path设置为可视区即可。即便很简单,但我们也不用浪费精力,该秉承不重复造车的原则,有人写了就直接拿来用呗。有兴趣可以看源码。https://github.com/skymansandy/scratchCardLayout

特征:

  • 刮刮卡效果到任何视图。
  • 用ScratchCardLayout包装现有的复杂布局。
  • 基于CardView的库。所以CardView的所有样式都可用。
  • 设置划痕画笔宽度。
  • 启用/禁用划痕效果
  • 将Drawable设置为划痕(颜色/图像)。
  • 应该显示完整布局时设置划痕的百分比。
  • 开始刮,进展(百分比)和停止时获取回调。

用法

依赖库:

dependencies {
     implementation 'in.codeshuffle.scratchcardlayout:ScratchCardLayout:1.0.5'
}

XML 

 <in.codeshuffle.scratchcardview.ui.ScratchCardLayout
        android:id="@+id/scratchCard"
        android:layout_width="250dp"
        android:layout_height="250dp"
        android:layout_centerInParent="true"
        app:scratchWidth="40dp"
        app:scratchEnabled="true"
        app:scratchDrawable="@drawable/your_drawable"
        app:scratchRevealFullAtPercent="100">
        <!--Your complex view here-->
 </in.codeshuffle.scratchcardview.ui.ScratchCardLayout>

Java 

 //Get view reference
       ScratchCardLayout scratchCardLayout = findViewById(R.id.scratchCard);
       
       //Set the drawable (programmatically)
       scratchCardLayout.setScratchDrawable(getResources().getDrawable(R.drawable.car));
       
       //Set scratch brush width
       scratchCardLayout.setScratchWidth(30f);
       
       //Reveal full layout when some percent of the view is scratched
       scratchCardLayout.setRevealFullAtPercent(40);
       
       //Scratching enable/disable
       scratchCardLayout.setScratchEnabled(true);
       
       //Remove all scratch made till now
       scratchCardLayout.resetScratch();

事件监听

          //Implement this to your class
          yourClass extends someBaseClass implements ScratchListener
          
          //Set the listener
          scratchCardLayout.setScratchListener(this);
            
          //You'll have three main callback methods as scratch listeners
          //Scratch started
          void onScratchStarted();
            
          //Scracth progress (NOTE: not guaranteed to be exact percent. consider it like atleast this much percent has been scratched)
          void onScratchProgress(ScratchCardLayout scratchCardLayout, int atLeastScratchedPercent);
            
          //Scratch completed
          void onScratchComplete();


本文链接:https://www.it72.com/12535.htm

推荐阅读
最新回复 (0)
返回