安卓实习期间整理知识点(十二)

来源:互联网 发布:莎莎源码网 编辑:程序博客网 时间:2024/04/30 11:38

安卓实习期间整理知识点(十二)

封装类,提供接口,尽量减少每个类的代码量

目前遇见到了体现单一职能最为明显的例子有:

  • HeaderView, BottomView 封装了APP的上栏与下栏。
  • TargetView, AutoViewInitializer 封装了根据id动态初始化组件的类
  • NetworkTool, Trace 分别用来检查网络和输出Log

它们都有几个共同的点:只做一件事,耦合性弱;可重用;

总而言之,尽量减少每个类的代码量,能够这样做说明各种类的耦合度已经很低了,进行查看时思路会很清晰。更重要的是方便进行修改,当项目需要添加功能或者删除不需要的,都可以很快的进行变更

分析LoadingManager

  • 这个类使用时,只需要提供view, id得到实例,提供调用的接口,即可在应用变化时实现各种状态
public class LoadingManager {    private Activity mActivity;    private LayoutInflater mInflater;    private View mView;    private ImageView mLoadingView;    private Animation mAnimation;    private RelativeLayout mListParent;    private FrameLayout mLoadingFrame;    private RelativeLayout mRlLoading;    private LinearLayout mLlNoData;    private LinearLayout mLlNoNetwork;    public LoadingManager(Activity activity, View view, int resId){        mActivity = activity;        mView = view;        mInflater = mActivity.getLayoutInflater();        mListParent = (RelativeLayout) mView.findViewById(resId);        initViews();        initAnimation();    }    public LoadingManager(Activity activity, int resId){        mActivity = activity;        mInflater = mActivity.getLayoutInflater();        mListParent = (RelativeLayout) mActivity.findViewById(resId);        initViews();        initAnimation();    }    private void initViews() {        mLoadingFrame = new FrameLayout(mActivity);        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);        mLoadingFrame.setLayoutParams(params);        // 初始化三种状态的layout        mRlLoading = (RelativeLayout) mInflater.inflate(R.layout.page_loading, null);        mLlNoData = (LinearLayout) mInflater.inflate(R.layout.page_no_data, null);        mLlNoNetwork = (LinearLayout) mInflater.inflate(R.layout.page_no_network, null);        // 在mRllLoading中过去ImageView        mLoadingView = (ImageView) mRlLoading.findViewById(R.id.iv_rotate);        // 初始化后隐藏        hideAll();        // 添加三种状态为frame的子view        mLoadingFrame.addView(mRlLoading);        mLoadingFrame.addView(mLlNoData);        mLoadingFrame.addView(mLlNoNetwork);        mListParent.addView(mLoadingFrame);    }    public void hideAll() {        mRlLoading.setVisibility(View.GONE);        mLlNoData.setVisibility(View.GONE);        mLlNoNetwork.setVisibility(View.GONE);        mLoadingFrame.setVisibility(View.GONE);    }    private void initAnimation() {        mAnimation = new RotateAnimation(0.0f, 360f,                Animation.RELATIVE_TO_SELF, 0.5f,                Animation.RELATIVE_TO_SELF, 0.5f);        mAnimation.setInterpolator(new LinearInterpolator());        mAnimation.setDuration(2000);        mAnimation.setRepeatMode(Animation.RESTART);// 重复        mAnimation.setRepeatCount(Animation.INFINITE);// 无限次    }    public void showLoading(){        hideAll();        mLoadingFrame.setVisibility(View.VISIBLE);        mRlLoading.setVisibility(View.VISIBLE);        mLoadingView.setAnimation(mAnimation);        mAnimation.startNow();    }    public void showNoData(){        hideAll();        mLoadingFrame.setVisibility(View.VISIBLE);        mLlNoData.setVisibility(View.VISIBLE);    }    public void showNoNetwork(){        hideAll();        mLoadingFrame.setVisibility(View.VISIBLE);        mLlNoNetwork.setVisibility(View.VISIBLE);    }    public boolean isHiding(){        return mLoadingFrame.getVisibility() == View.GONE;    }}

开始保存可重用的代码文件

  • 有的类如果写得很好,耦合性低,功能强大,可以将它独自拷贝出来,这样下次做相同功能时可以直接使用原先的代码。如果功能有出入也可以在原先的代码上进行修改。
  • 尽量去写高效的代码,写高效代码的过程中,更加容易思考,体会编程思想;也可方便业务应对业务需求的变化。
  • 写过的工具代码,尽量降低它的耦合性,将它保存起来,方便下次重用,相应地功能知识的可以不深刻的记忆,只需要能够看见代码回想起其基本功能就行
  • 关于如何阅读优秀的代码也可以从这里得到启发,优秀的代码耦合性低,如果不了解具体代码的功能和其知识点,可以将整个项目分为多个模块。理清各个功能,理解整个框架,然后更具需要学习的地方,查看具体的代码内容。

动态初始化控件

  • inflate方法中不需要parent
  • 使用LayoutParams传入参数
mLoadingFrame = new FrameLayout(mActivity);        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);        mLoadingFrame.setLayoutParams(params);        // 初始化三种状态的layout        mRlLoading = (RelativeLayout) mInflater.inflate(R.layout.page_loading, null);        mLlNoData = (LinearLayout) mInflater.inflate(R.layout.page_no_data, null);        mLlNoNetwork = (LinearLayout) mInflater.inflate(R.layout.page_no_network, null);        // 在mRllLoading中过去ImageView        mLoadingView = (ImageView) mRlLoading.findViewById(R.id.iv_rotate);        // 初始化后隐藏        hideAll();        // 添加三种状态为frame的子view        mLoadingFrame.addView(mRlLoading);        mLoadingFrame.addView(mLlNoData);        mLoadingFrame.addView(mLlNoNetwork);        mListParent.addView(mLoadingFrame);

Animation, Interpolator的使用代码

mAnimation = new RotateAnimation(0.0f, 360f,                Animation.RELATIVE_TO_SELF, 0.5f,                Animation.RELATIVE_TO_SELF, 0.5f);        mAnimation.setInterpolator(new LinearInterpolator());        mAnimation.setDuration(2000);        mAnimation.setRepeatMode(Animation.RESTART);// 重复        mAnimation.setRepeatCount(Animation.INFINITE);// 无限次
0 0