android 中自定义gridview,android 自定义gridview(导航)

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);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值