上传文件至 'Android/Demo1'

适用于RecyclerView的右侧半透明遮罩demo
This commit is contained in:
yutou 2022-08-23 16:24:45 +08:00
parent 1204a2cfbb
commit d7d81bc5a7

View File

@ -0,0 +1,55 @@
package com.yunbao.live.custom;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Shader;
import android.graphics.Xfermode;
import androidx.recyclerview.widget.RecyclerView;
/**
* Created by cxf on 2018/1/27.
* RecyclerView右側渐变的itemDecoration
*/
public class RightGradual extends RecyclerView.ItemDecoration {
private Paint mPaint;
private Xfermode mXfermode;
private LinearGradient mLinearGradient;
private int mLayerId;
public RightGradual() {
mPaint = new Paint();
mXfermode = new PorterDuffXfermode(PorterDuff.Mode.DST_IN);
mLinearGradient = null;
}
@Override
public void onDrawOver(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
super.onDrawOver(canvas, parent, state);
float left=parent.getRight()-parent.getRight()/0.1f;
float top=0f;
float right=parent.getRight();
float bottom=parent.getBottom();
if(mLinearGradient==null){
mLinearGradient=new LinearGradient(parent.getMeasuredWidth(), 0.0f, 0.0f, 0.0f, new int[]{0, Color.BLACK}, new float[]{0.0f,0.2f}, Shader.TileMode.CLAMP);
}
mPaint.setXfermode(mXfermode);
mPaint.setShader(mLinearGradient);
//mPaint.setColor(Color.BLUE);
canvas.drawRect(left, top,right, bottom, mPaint);
mPaint.setXfermode(null);
canvas.restoreToCount(mLayerId);
}
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
super.onDraw(c, parent, state);
mLayerId = c.saveLayer(0.0f, 0.0f, (float) parent.getWidth(), (float) parent.getHeight(), mPaint, Canvas.ALL_SAVE_FLAG);
}
}