android 自定义gridview(导航)
最近又重新做回安卓,做了个小项目。下绝心使用android studio,通过这一回实战,终于用上了。综合了前人的经验,搞了个自己满意的导航界面,用的是gridview。
代码:
package com.cquni.control; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Rect; import android.graphics.RectF; import android.util.AttributeSet; import android.widget.AbsListView; import android.widget.GridView; import com.cquni.smartwcs.R; /* 用来做导航的自定义GridView */ public class NavigateGridView extends GridView { private Bitmap background; private int img_Id = R.drawable.gridbg; public NavigateGridView(Context context) { super(context); background = BitmapFactory.decodeResource(getResources(), img_Id); } public NavigateGridView(Context context, AttributeSet attrs) { super(context, attrs); background = BitmapFactory.decodeResource(getResources(), img_Id); } public NavigateGridView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); background = BitmapFactory.decodeResource(getResources(), img_Id); } /* 高度实现自适应 */ @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int heightSpec; if (getLayoutParams().height == AbsListView.LayoutParams.WRAP_CONTENT) { heightSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); } else { heightSpec = heightMeasureSpec; } super.onMeasure(widthMeasureSpec, heightSpec); } /* 拉伸背景图 */ @Override protected void dispatchDraw(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 = new Rect(); src.left = 0; src.top = 0; src.right = backgroundWidth; src.bottom = backgroundHeight; for (int y = top; y < height; y += backgroundHeight) { RectF dst = new RectF(); dst.left = 0; dst.top = y; dst.right = width; dst.bottom = (y + backgroundHeight) / 2; canvas.drawBitmap(background, src, dst, null); } super.dispatchDraw(canvas); } }
gridview改进:
gv.setSelector(new ColorDrawable(Color.TRANSPARENT));
选中item时的动画
ImageView itemImg = arg1.findViewById(R.id.ItemImageView);
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.item_img);
itemImg.startAnimation(animation);
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <alpha android:fromAlpha="0.5" android:toAlpha="1.0" android:duration="50"/> </set>
作者:数据酷软件
出处:https://www.cnblogs.com/datacool/p/datac.html
关于作者:20年编程从业经验,持续关注MES/ERP/POS/WMS/工业自动化
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明。
联系方式: qq:71008973;wx:6857740733
基于人脸识别的考勤系统 地址: https://gitee.com/afeng124/viewface_attendance_ext
自己开发安卓应用框架 地址: https://gitee.com/afeng124/android-app-frame
WPOS(warehouse+pos) 后台演示地址: http://47.239.106.75:8080/
分类:
程序设计
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构