鹅厂实习| 周记(四)

作者: 厘米姑娘 | 来源:发表于2019-03-18 14:22 被阅读605次

    以下是本周的知识清单:

    • TypedArray
    • if-else优化
    • 注解替代枚举
    • 一点小感悟

    1.TypedArray

    a.官方介绍:TypedArray是一个存放array值的容器,通过Resources.Theme#obtainStyledAttributes()Resources#obtainAttributes()方法来检索,完成后要调用recycle()方法。indices用于从obtainStyledAttributes()得到的容器中获取对应的值。

    Container for an array of values that were retrieved with {@link Resources.Theme#obtainStyledAttributes(AttributeSet, int[], int, int)}or {@link Resources#obtainAttributes}. Be sure to call {@link #recycle} when done with them.The indices used to retrieve values from this structure correspond to the positions of the attributes given to obtainStyledAttributes.

    b.常用方法

    • 创建:两种方式
      • TypedArray typedArray = getResources().obtainAttributes()
        • obtainAttributes(AttributeSet set, int[] attrs)
      • TypedArray typedArray = Context.obtainStyledAttributes()
        • obtainStyledAttributes(int[] attrs)
        • obtainStyledAttributes(int resid, int[] attrs)
        • obtainStyledAttributes(AttributeSet set, int[] attrs)
        • obtainStyledAttributes(AttributeSet set, int[] attrs, int defStyleAttr, int defStyleRes)
    • 使用:typedArray.getXXX()
      • getBoolean(int index, boolean defValue)
      • getInteger(int index, boolean defValue)
      • getInt(int index, boolean defValue)
      • getFloat(int index, boolean defValue)
      • getString(int index)
      • getColor(int index, boolean defValue)
      • getFraction(int index, int base, int pbase, boolean defValue)
      • getDimension(int index, float defValue)
    • 回收:typedArray.recycle()

    推荐阅读TypedArray 为什么需要调用recycle()

    c.应用:在自定义view时,用于获取自定义属性

    d.举例:以下是获取自定义属性的步骤

    • 创建自定义属性:创建values/attrs.xml文件,声明自定义属性
      • 属性类型:color颜色值、boolean布尔值、dimesion尺寸值、float浮点值、integer整型值、string字符串、fraction百分数、enum枚举值、reference引用资源文件
    <?xml version="1.0" encoding="utf-8"?>
    <resources>
      <declare-styleable name="MyView">
        <attr name="text" format="string" />
        <attr name="textColor" format="color"/>
        <attr name="textSize" format="dimension"/>
      </declare-styleable>
    </resources>
    
    • 自定义View类:在构造方法中通过TypedArray获取自定义属性
    public class MyCustomView extends View {
    
        public MyCustomView(Context context, AttributeSet attrs) {
            super(context, attrs);
            //创建
            TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyView);
            //使用
            String text = typedArray.getString(R.styleable.MyView_text);
            int textColor = typedArray.getColor(R.styleable.MyView_textColor, 20);
            float textSize = typedArray.getDimension(R.styleable.MyView_textSize, 0xDD333333);
            Log.i("MyCustomView", "text = " + text + " , textColor = " + textColor+ " , textSize = " + textSize);
            //回收
            typedArray.recycle();
        }
        ...
    }
    
    • 在布局文件中使用自定义属性:注意命名空间
    <LinearLayout 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      xmlns:app="http://schemas.android.com/apk/res-auto"   
      ...
      <com.example.attrtextdemo.MyCustomView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:text="我是自定义属性"
        app:textColor="@android:color/black"
        app:textSize="18sp"/>
    </LinearLayout>
    

    2.if-else优化

    • 使用条件三目运算符
    //优化前
    int value = 0;
    if(condition) {
        value=1;
    } else {
        value=2;
    }
    
    //优化后
    int value = condition ? 1 : 2;
    
    • 异常/return/continue/break语句前置
    //优化前
    if(condition) {
        // TODO somethings
    } else {
        return;
    }
    
    //优化后
    if(!condition) {
        return;
    } 
    // TODO somethings
    
    • 使用表驱动法:直接修改表,而不是增加新的分支
    //优化前
    int key = this.getKey();
    int value = 0;
    if(key==1) {
        value = 1;
    } else if(key==2) {
        value = 2;
    } else if(key==3) {
        value = 3;
    } else {
        throw new Exception();
    }
    
     //优化后
    Map map = new HashMap();
    map.put(1,1); 
    map.put(2,2); 
    map.put(3,3); 
    int key = this.getKey(); 
    if(!map.containsKey(key)) { 
        throw new Exception(); 
    } 
    int value = map.get(key);
    
    • 抽象出另一个方法
    //优化前
    public void fun1() {
        if(condition1) {
            // TODO sometings1
            if(condition2) {
                // TODO something2
                if(condition3) {
                    // TODO something3
                }
            }
        } 
        // TODO something4 
    }
    
    //优化后
    public void fun1() {
        fun2();
        // TODO something4
    }
     
    private void fun2() {
        if(!condition1) {
            return;
        }
        // TODO sometings1
        if(!condition2) {
            return;
        }
        // TODO something2 
        if(!condition3) {
            return;
        }
        // TODO something3
    }
    
    • 使用策略模式+工厂模式:把具体的算法封装到了具体策略类内部,增强可扩展性,隐蔽实现细节
      • 抽象策略类:接口或抽象类,包含所有具体策略类所需的接口
      • 具体策略类:继承或实现抽象策略类,实现抽象方法
      • 策略调度与执行者:持有一个对象的引用,并执行对应策略(用工厂实现)

    3.注解替代枚举

    原因:枚举中的每个值在枚举类中都是一个对象,使用枚举值会比直接使用常量消耗更多的内存

    使用枚举:

    public enum WeekDays{
        MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY,SUNDAY;
    }
    

    使用注解:

        //1.定义常量或字符串
        public static final int SUNDAY = 0;
        public static final int MONDAY = 1;
        public static final int TUESDAY = 2;
        public static final int WEDNESDAY = 3;
        public static final int THURSDAY = 4;
        public static final int FRIDAY = 5;
        public static final int SATURDAY = 6;
        
        //2.注解枚举,可选择项@IntDef、@StringDef、@LongDef、StringDef
        @IntDef({SUNDAY, MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY})
        @Retention(RetentionPolicy.SOURCE)
        public @interface WeekDays {}
        
        //3.使用
        @WeekDays
        private static int mCurrentDay = SUNDAY;
        public static void setCurrentDay(@WeekDays int currentDay) {
            mCurrentDay = currentDay;
        }
    

    相关基础


    4.一点小感悟

    回校前的一周多开始写新版本新需求,和另一个iOS开发的毕业生做同个需求,带我俩的是一个iOS大佬,于是不可避免还要接触Object-C语言,加上时间紧任务重,学校那边刚开学也有不少事,因此这段时间真是忙得不可开交,此刻已经回校处理好事情刚休息一会儿,这才有空补上最后一篇实习周记。

    上篇博文的最后贴了几张部门团建的照片,当时大家在进行体育拓展活动,没错被贴纸挡住的就是本人啦!来了鹅厂这么久终于出了次远门,可以说非常激动了!除了在晚宴的抽奖活动中感受不太好之外,已经能预感到未来几年都只能做分母了...

    git命令思维导图
    虽然实习的结束有些仓促和忙乱,但我知道七月还会再回来,就不曾有何遗憾。在真正成为社会人的最后这四个月里,希望能顺利毕业、和身边所有人好好告别、享受最后一段美好的大学时光~

    接下来如果有读书计划的话会继续更新笔记,至于周记就以后再见啦!

    美食空间——第三条“腰带”
    插播一则招聘信息,目前组里急招20届iOS开发暑期实习生和社招iOS开发,详见上一篇 【腾讯】【组内直推】【文末爆照】iOS开发暑期实习生/社招,文章只要不删就还有效,欢迎投简历、推荐和转发,我在腾讯视频等你~

    相关文章

      网友评论

        本文标题:鹅厂实习| 周记(四)

        本文链接:https://www.haomeiwen.com/subject/duivmqtx.html