亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

android使用ItemDecoration給RecyclerView 添加水印

 更新時(shí)間:2017年02月06日 14:02:57   作者:baiiu  
本篇文章主要介紹了android使用ItemDecoration給RecyclerView 添加水印,介紹了自定義Drawable來完成水印圖片和使用ItemDecoration來布局水印,有興趣的可以了解一下。

前言

項(xiàng)目中有使用到水印效果,如下圖所示。在實(shí)現(xiàn)過程中,最終選用ItemDecoration來實(shí)現(xiàn),其中有兩大步驟:自定義Drawable來完成水印圖片、使用ItemDecoration來布局水印。

Demo在 WatermarkFragment 中,效果圖如下:

1. 自定義Drawable完成水印圖片

public class MyDrawable extends Drawable {
 Paint mPaint;

 public MyDrawable() {
  mPaint = new Paint();
  mPaint.setColor(Color.parseColor("#1A000000"));
  mPaint.setAntiAlias(true);
  mPaint.setTextAlign(Paint.Align.LEFT);//從字的最左邊開始畫
  mPaint.setTextSize(54);
 }

 @Override public void draw(@NonNull Canvas canvas) {
  Rect r = getBounds();

  //畫斜著的字
  canvas.save();
  canvas.rotate(-30, r.left, r.bottom);
  canvas.drawText("哈哈哈哈哈哈哈", r.left, r.bottom, mPaint);

  canvas.restore();
 }

 /*
  復(fù)寫這兩個(gè)方法是為了當(dāng)在控件wrap_content時(shí)能自己測(cè)量出高,同時(shí)也方便布局。
 */ 

 //傾斜30度,可以算出高來
 @Override public int getIntrinsicHeight() {
  return (int) (Math.sqrt(3) / 3 * getIntrinsicWidth() + 0.5F);

 }

 @Override public int getIntrinsicWidth() {
  return (int) (mPaint.measureText("DecorationDraw") + 0.5F);
 }

 //...模板方法省略

}

這里說一下,自定義該Drawable是比較簡(jiǎn)單的,但是想到這一步的話就簡(jiǎn)答多了,剛開始是想直接在ItemDecoration里邊繪制邊布局,但后來嘗試了一下太復(fù)雜,所以就使用Drawable獨(dú)立出來,然后就順利了好多。

2. 使用ItemDecoration布局水印

public class MyDecoration extends RecyclerView.ItemDecoration {
 private Drawable mDivider;
 private int mScrollY;

 public MyDecoration() {
  mDivider = new MyDrawable();
 }

 @Override public void onDraw(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
  //清除之前畫的
  // canvas.drawColor(Color.WHITE);

  /*
   * 跟著滑動(dòng)是因?yàn)閎ounds在不停的變化,就是top
   */
  int top = UIUtil.dp(20) - mScrollY;

  // 對(duì)于畫多少個(gè)水印,根據(jù)業(yè)務(wù)需求來,這里直接畫count個(gè)
  int itemCount = parent.getAdapter().getItemCount();

  // 進(jìn)行布局
  for (int i = 0; i < itemCount; ++i) {
   int left = i % 2 == 0 ? UIUtil.dp(20) : parent.getMeasuredWidth() -mDivider.getIntrinsicWidth() - UIUtil.dp(20);

   //通過setBounds來控制水印的左右
   mDivider.setBounds(left, top, parent.getMeasuredWidth(), top + mDivider.getIntrinsicHeight());
   mDivider.draw(canvas);

   if (i % 2 == 0) {
    top += UIUtil.dp(20) + mDivider.getIntrinsicHeight();
   } else {
    top += UIUtil.dp(140) + mDivider.getIntrinsicHeight();
   }
  }

 }

 /*
  mScrollY用于監(jiān)測(cè)recyclerView的滑動(dòng)距離,此處使用的是onScrollListener中dy的累加值,當(dāng)item不發(fā)生刪除添加操作時(shí)是準(zhǔn)確的
 */ 
 public void setScrollY(int scrollY) {
  this.mScrollY = scrollY;
 }
}

在RecyclerView中:

private int totallyY = 0;

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
 @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
  totallyY += dy;
  myDecoration.setScrollY(totallyY);
  }
});

結(jié)語

這么寫下來感覺還是很簡(jiǎn)單的,剛開始實(shí)現(xiàn)時(shí)感覺確實(shí)有點(diǎn)難度,RecyclerView寫的真的好,藝術(shù)般的控件。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論