由于最近项目需要,需要实现列表上下渐变过渡的效果.网上查阅了相关资料,有一种方法是实现了上方的渐变,但几乎查到的都是类似的上方渐变效果,因此,我写了下面的过渡效果,方便大家开发使用.
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Shader;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
public class FadingRecyclerView extends RecyclerView {
private static final String TAG = "FadingRecyclerView";
private Paint paint;
private int height;
private int width;
private int spanPixel = 100;
public FadingRecyclerView(Context context) {
super(context);
init(context, null);
}
public FadingRecyclerView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public FadingRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
paint = new Paint();
paint.setAntiAlias(true);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
// paint.setShader(new LinearGradient(0, 0, 0, 890/2, 0x00000000, 0xff000000, Shader.TileMode.CLAMP)); //仅上部渐变
}
public void setSpanPixel(int spanPixel) {
this.spanPixel = spanPixel;
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
height = h;
width = w;
float spanFactor = spanPixel / (height / 2f);
LinearGradient linearGradient = new LinearGradient(0, 0, 0, height / 2,
new int[]{0x00000000, 0xff000000, 0xff000000}, new float[]{0, spanFactor, 1f}, Shader.TileMode.MIRROR);
paint.setShader(linearGradient);
}
@Override
public void draw(Canvas c) {
c.saveLayer(0, 0, width, height, null, Canvas.ALL_SAVE_FLAG);
super.draw(c);
c.drawRect(0, 0, width, height, paint);
c.restore();
}
}
效果示意图: