packagecom.cquni.control;importandroid.content.Context;importandroid.graphics.Bitmap;importandroid.graphics.BitmapFactory;importandroid.graphics.Canvas;importandroid.graphics.Rect;importandroid.graphics.RectF;importandroid.util.AttributeSet;importandroid.widget.AbsListView;importandroid.widget.GridView;importcom.cquni.smartwcs.R;/*用来做导航的自定义GridView*/
public class NavigateGridView extendsGridView {privateBitmap background;private int img_Id =R.drawable.gridbg;publicNavigateGridView(Context context) {super(context);
background=BitmapFactory.decodeResource(getResources(), img_Id);
}publicNavigateGridView(Context context, AttributeSet attrs) {super(context, attrs);
background=BitmapFactory.decodeResource(getResources(), img_Id);
}public NavigateGridView(Context context, AttributeSet attrs, intdefStyleAttr) {super(context, attrs, defStyleAttr);
background=BitmapFactory.decodeResource(getResources(), img_Id);
}/*高度实现自适应*/@Overrideprotected void onMeasure(int widthMeasureSpec, intheightMeasureSpec) {intheightSpec;if (getLayoutParams().height ==AbsListView.LayoutParams.WRAP_CONTENT) {
heightSpec= MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
}else{
heightSpec=heightMeasureSpec;
}super.onMeasure(widthMeasureSpec, heightSpec);
}/*拉伸背景图*/@Overrideprotected voiddispatchDraw(Canvas canvas) {int count =getChildCount();int top = count > 0 ? getChildAt(0).getTop() : 0;int backgroundWidth =background.getWidth();int backgroundHeight =background.getHeight();int width =getWidth();int height =getHeight();final Rect src = newRect();
src.left= 0;
src.top= 0;
src.right=backgroundWidth;
src.bottom=backgroundHeight;for (int y = top; y < height; y +=backgroundHeight) {
RectF dst= newRectF();
dst.left= 0;
dst.top=y;
dst.right=width;
dst.bottom= (y + backgroundHeight) / 2;
canvas.drawBitmap(background, src, dst,null);
}super.dispatchDraw(canvas);
}
}