• 2016年3月3日android实习笔记


    1:

    android自定义控件时,通常需要重写父类构造函数。这三个够找函数具体啥时调用?

    public View (Context context) 是在java代码创建视图的时候被调用,如果是从xml填充的视图,就不会调用这个
    public View (Context context, AttributeSet attrs) 这个是在xml创建但是没有指定style的时候被调用
    public View (Context context, AttributeSet attrs, int defStyle) 在xml创建并指定style的时候被调用

    如果在Code中实例化一个View会调用第一个构造函数,如果在xml中定义会调用第二个构造函数,而第三个函数系统是不调用的,要由View(我们自定义的或系统预定义的View,如此处的CustomTextView和Button)显式调用,比如在这里我们在第二个构造函数中调用了第三个构造函数,并将R.attr.CustomizeStyle传给了第三个参数。

    关于第三个函数,这里附带一篇长博:http://www.cnblogs.com/angeldevil/p/3479431.html

    2:API 11 之后不能使用硬件渲染,所以需要关闭硬件加速,view级别的关闭硬件加速比较好,this.setLayerType(View.LAYER_TYPE_SOFTWARE,null);加到自定义的View 类中的构造函数就可以

    3:

    //为SeekBar添加滑动事件
    mSkBarGifPlay.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
    // TODO Auto-generated method stub

    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
    // TODO Auto-generated method stub

    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress,
    boolean fromUser) {
    // TODO Auto-generated method stub

    }
    });

    出错的话,可能是没有引入相应的包。

    4.Activity的的方法中使用Toast。

    Toast.makeText(getBaseContext(),  Integer.toString(progress), Toast.LENGTH_SHORT).show();

    或者Toast.makeText(XXXActivity.this,  Integer.toString(progress), Toast.LENGTH_SHORT).show();

    所显示的内容必须是字符串,否则会crash。

    5.gif图片可以通过拉伸Canvas来做到

    核心代码  自己加个layout,Activity测试
    public class GifView extends View {

        private Movie mMovie;
        private long mMovieStart;
        
      

        private int mWidth, mHeight;
        private int mViewWidht, mViewHeight;

        private OnPlayListener onPlayListener;

        public GifView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            // TODO Auto-generated constructor stub
        }

        public GifView(Context context) {
            super(context);

            mMovie = Movie.decodeStream(getResources().openRawResource(
                    R.raw.gif_anim));
        }

        public GifView(Context context, AttributeSet attrs) {
            super(context, attrs);
            //使用Movie解析gif图片
            mMovie = Movie.decodeStream(getResources().openRawResource( R.raw.gif_anim));
            //获得屏幕宽度,高度
            mWidth = BaseApplication.getInstance().screenWidth;
            mHeight = BaseApplication.getInstance().screenHeight;
            //gif图片宽度,高度
            mViewHeight = mMovie.height();
            mViewWidht = mMovie.width();
        }
            
        public OnPlayListener getOnPlayListener() {
            return onPlayListener;
        }
            
        public void setOnPlayListener(OnPlayListener onPlayListener) {
            this.onPlayListener = onPlayListener;
        }

        boolean isDraw = true;

        public void onDraw(Canvas canvas) {
            long now = android.os.SystemClock.uptimeMillis();

            if (isDraw) {

                if (mMovieStart == 0) { // first time
                    mMovieStart = now;
                }
                if (mMovie != null) {

                    int dur = mMovie.duration();
                    if (dur == 0) {
                        dur = 5000;
                    }
                    //计算gif播放时间,gif播放完成,关闭界面
                    if (now - mMovieStart >= dur) {
                        isDraw = false;
                        if (onPlayListener != null) {
                            onPlayListener.onFinished();
                        }
                    }

                    int relTime = (int) ((now - mMovieStart) % dur);

                    mMovie.setTime(relTime);
                    //根据屏幕大小计算缩放比例
                    float saclex = (float) mWidth / (float) mViewWidht;
                    float sacley = (float) mHeight / (float) mViewHeight;
                    float sameRate = saclex > sacley ? saclex : sacley;
                    canvas.scale(sameRate, sameRate);
                    mMovie.draw(canvas, 0, 0);

                    invalidate();
                }
            }
        }
            //gif关闭接口
        public static interface OnPlayListener {
            public void onFinished();
        }
    }

    参考:http://www.apkbus.com/android-142921-1-1.html

    6.自定义View中No resource identifier found for attribute X in package X。

    解决方法路径中只需要写自己的AndroidManifest文件中的包名即可。

    如:xmlns:attr="http://schemas.android.com/apk/res/com.vane.demo"

    参考:http://blog.csdn.net/xiaoguohaha/article/details/12676691

    7.Thread.sleep需要捕捉异常操作

    try {
    Thread.sleep(800);
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

  • 相关阅读:
    2. Add Two Numbers
    1. Two Sum
    22. Generate Parentheses (backTracking)
    21. Merge Two Sorted Lists
    20. Valid Parentheses (Stack)
    19. Remove Nth Node From End of List
    18. 4Sum (通用算法 nSum)
    17. Letter Combinations of a Phone Number (backtracking)
    LeetCode SQL: Combine Two Tables
    LeetCode SQL:Employees Earning More Than Their Managers
  • 原文地址:https://www.cnblogs.com/hsqdboke/p/5237513.html
Copyright © 2020-2023  润新知