package com.unilife.fridge.home.ui.main.widget;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import com.unilife.fridge.home.R;
/**
* Created by cj on 2018/1/4.
*/
public class BshRecyclerView extends RecyclerView {
private static final String TAG = "cj";
private Bitmap gradientView;
private Bitmap gradientView2;
private Bitmap gradientTop70;
private Bitmap gradientBottom70;
private int totalHeight;
private int totalWidth;
private boolean showTop = false;
private boolean showBottom = false;
private boolean isIdle = true;
private float density;
private Paint paint;
public BshRecyclerView(Context context) {
this(context, null);
}
public BshRecyclerView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public BshRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
Bitmap gradientView22 = BitmapFactory.decodeResource(getResources(), R.drawable.shape_shadow2);
Bitmap gradientView11 = BitmapFactory.decodeResource(getResources(), R.drawable.shape_shadow);
DisplayMetrics dm = new DisplayMetrics();
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
windowManager.getDefaultDisplay().getMetrics(dm);
density = dm.density;
Log.e(TAG, "BshRecyclerView:density: " + density);
paint = new Paint();
paint.setColor(Color.WHITE);
// 创建操作图片用的matrix对象
Matrix matrix = new Matrix();
// 缩放图片动作
matrix.postScale(1 / density, 1 / density);
//原大小
//上
gradientView2 = Bitmap.createBitmap(gradientView22, 0, 0, gradientView22.getWidth(),
gradientView22.getHeight(), matrix, true);
//下
gradientView = Bitmap.createBitmap(gradientView11, 0, 0, gradientView11.getWidth(),
gradientView11.getHeight(), matrix, true);
Matrix matrix70 = new Matrix();
matrix70.postScale(0.7f, 0.7f);
//缩小70%之后两个
//上
gradientTop70 = Bitmap.createBitmap(gradientView2, 0, 0, gradientView2.getWidth(),
gradientView2.getHeight(), matrix70, true);
//下
gradientBottom70 = Bitmap.createBitmap(gradientView, 0, 0, gradientView.getWidth(),
gradientView.getHeight(), matrix70, true);
Log.e(TAG, "gradientView width:" + gradientView.getWidth() + " height:" + gradientView.getHeight());
this.addOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
if (newState == RecyclerView.SCROLL_STATE_IDLE) {
isIdle = true;
}
if (newState == RecyclerView.SCROLL_STATE_DRAGGING) {
isIdle = false;
}
if (newState == RecyclerView.SCROLL_STATE_SETTLING) {
isIdle = false;
}
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
View child0 = recyclerView.getChildAt(0);
int child0AdapterPosition = recyclerView.getChildAdapterPosition(child0);
if (child0AdapterPosition != 0) {
showTop = true;
} else {
showTop = false;
}
int childCount = recyclerView.getChildCount();
View child1 = recyclerView.getChildAt(childCount - 1);
int child1AdapterPosition = recyclerView.getChildAdapterPosition(child1);
if (child1AdapterPosition != recyclerView.getAdapter().getItemCount() - 1) {
showBottom = true;
} else {
showBottom = false;
}
}
});
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
totalHeight = h;
totalWidth = w;
}
@Override
public void onDraw(Canvas c) {
super.onDraw(c);
c.drawLine(0,0,500,500,paint);
if (showTop) {
if (isIdle) {
Rect rect = new Rect(0, 0, gradientView2.getWidth(), gradientView2.getHeight());
Rect desRect = new Rect(0, 0, totalWidth, gradientView2.getHeight());
c.drawBitmap(gradientView2, rect, desRect, null);
} else {
Rect rect = new Rect(0, 0, gradientTop70.getWidth(), (gradientTop70.getHeight()));
Rect desRect = new Rect(0, 0,totalWidth, (gradientTop70.getHeight()));
c.drawBitmap(gradientTop70, rect, desRect, null);
}
}
if (showBottom) {
if (isIdle) {
Rect rect = new Rect(0, 0, gradientView.getWidth(), gradientView.getHeight());
Rect desRect = new Rect(0, (int) (totalHeight - gradientView.getHeight()), totalWidth, totalHeight);
c.drawBitmap(gradientView, rect, desRect, null);
} else {
Rect rect = new Rect(0, 0, gradientBottom70.getWidth(), gradientBottom70.getHeight());
Rect desRect = new Rect(0, (int) (totalHeight - gradientBottom70.getHeight()), totalWidth, totalHeight);
c.drawBitmap(gradientBottom70, rect, desRect, null);
}
}
}
}