andorid 开发笔记 -- 问题与解决

1. SQLiteDataBase 中 TimeStamp 转化为 Date 的问题:java.text.ParseException: Unparseable date: "Sun Jan 25 21:15:51 GMT+08:00 2

      解决:读取数据时用 Cursor.GetString() 这样的方式读出来,导致转化时出现问题。

              TimeStamp 其实是一个整型的数据,代码如下:

    private Date getDateFromCursor(Cursor cursor, String columnName) throws Exception {
        long date = cursor.getLong(cursor.getColumnIndex(columnName));
        return new Date(date);
    }

  另外注意 insert or update TimeStamp 字段时,插入值应该是 date().getTime() 转化为长整型

2. ImageView.GetWidth() 为0

  OnCreate() 中显示数据的时ImageView.GetWidth() 为0,有人说这是因为 OnCreate 中垃圾回收,本人觉得是因为这里还没有创建View,所有的Layout均没有正确初始化,可以选择在 onWindowFocusChanged() 计算,这里可以得到正常值。

  但是有一个问题,OnWindowFocusChanged() 有可能会经常调用,例如点击菜单等

   另外有一种更好的解决办法:

        ViewTreeObserver viewTreeObserver = gameFrame.getViewTreeObserver();
        if (viewTreeObserver.isAlive()) {
            viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
                @Override
                public void onGlobalLayout() {
                    gameFrame.getViewTreeObserver().removeOnGlobalLayoutListener(this);//移除响应,表示本监听只需要运行一次,获取需要的数据之后不再需要运行此过程
                    layoutX = gameFrame.getWidth();
                    layoutY = gameFrame.getHeight();
                    refreshView();
                }
            });
        }

3. 暂时正在滑动的图片,停在当前位置

很多人说用 clearAnimation(),事实证明这个不管用

倒是下面一句话给了我启示:

What you need to do is to calculate the amount of time into the current animation cycle, then create a new Animation with a 0 duration, fillEnabled, fillBefore and fillAfter set to true and for the amount of translation to be equal to the amount that the current animation would have been offsetting the View.

long currentTime = System.currentTimeMillis();//记录当前时间
long y = (-layoutY+ imageView.getHeight()) * (currentTime - backupTime) / 5000;  // 5000 为上文定义的动画时间,currentTime-backupTime 为动画运行时间
if(y < -layoutY)return;
imageView.clearAnimation();
imageView.offsetTopAndBottom((int)y);

4. 关于 javaDoc 文档注释

  Android Studio 默认没有快捷键在函数前面生成description, 用户可以在函数前 输入 /** 后按回车即可自动完成。另外用户可以打开  File -> settings(Ctrl + Alt + S) -> Keymap -> Fix doc comment 中设置快捷键。

  @param ParameterName 函数传入参数

  @return ReturnValue 返回值

  @see #MethodName 指向其他函数 (必要时使用)

本文原创, 转载请注明出处: http://www.cnblogs.com/EasyInvoice/p/4255456.html

原文地址:https://www.cnblogs.com/EasyInvoice/p/4255456.html