zoukankan      html  css  js  c++  java
  • 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>
    
    
    
     



  • 相关阅读:
    Docker初识
    57、android 应用内全局通知的实现方法
    56、使用android studio(v1.3.*)修改包名 (rename package name)
    55、android app借助友盟实现微信授权登录
    54、edittext输入类型限制为ip,inputType应该如何设置
    53、listview、expandableListview如何选中时保持高亮?
    52、sb犯的错误
    51、如何提取android代码中的字符串为系统资源文件 (I18N)
    50、转自知乎上android开发相见恨晚的接口
    49、android studio 使用技巧记录
  • 原文地址:https://www.cnblogs.com/datacool/p/datac.html
Copyright © 2011-2022 走看看