上传文件至 'Android/Demo1'
适用于RecyclerView的右侧半透明遮罩demo
This commit is contained in:
parent
1204a2cfbb
commit
d7d81bc5a7
55
Android/Demo1/RightGradual.java
Normal file
55
Android/Demo1/RightGradual.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user