踩坑记录史,一把辛酸泪

记录一下

备忘录:
1.目前我最喜欢的RxJava2的教程,简单易懂 感谢 Season_zlc 简书主页就是教程
2.对于崩溃以前都是关闭应用,现在突然觉得展示界面还不错,有轮子就直接用了CustomActivityOnCrash
3.对于RecyclerView的优化 这个文章很不错 喜欢的 RecyclerView 体验优化及入坑总结
4.对于不同分辨率的屏幕适配 Android 屏幕适配方案
我在这找了所有的安卓分辨率全部写上了,如果还有其他自行增加
工具和一些其他需要增加的分辨率这里下载 https://github.com/zhxcryptic/auto

(1)Fragment进行数据的懒加载

 @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        isViewCreated = true;
        lazyLoad();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            isUIVisible = true;
            lazyLoad();
        }
    }

    private void lazyLoad() {
        //这里进行双重标记判断,是因为setUserVisibleHint会多次回调,
        //并且会在onCreateView执行前回调,必须确保onCreateView加载完毕且页面可见,才加载数据
        if (isViewCreated && isUIVisible) {
            initialize();
            //数据加载完毕,恢复标记,防止重复加载
            isViewCreated = false;
            isUIVisible = false;
        }
    }

    /**
     * 数据懒加载的初始化
     */
    protected abstract void initialize();

(2)使用字符串之前,有些可能传过来的数据是字符串的null,判断字符串为空

    /**
     * 判断字符串是否为空
     *
     * @param str
     * @return
     */
    public static boolean isEmpty(@Nullable CharSequence str) {
        return  str == null || str.length() == 0||"null".equals(str.toString().trim()) ;
    }

(3)JSON数据解析要进行try catch ,指不定哪天就给你来一下

(4)将带有html格式的文本转化,主要我也不知道后台回传过来什么东西

    /**
     * 将带有格式的html文本转化为字符串
     *
     * @param htmlStr
     * @return
     */
    public static String htmlToText(@Nullable String htmlStr) {
        if (isEmpty(htmlStr)) {
            return "";
        }
        return htmlStr.replaceAll("]+>", "").replaceAll("\\s*|\t|\r|\n", "");
    }

(5)项目开始前就把bug日志收集,和内存检测框架集成,别忘了。。。推荐bugly和leakcanary

(6)安卓7.0的本机图片采集和服务器安装包下载安装需要重新创建Provider继承FileProvider

(7)删除大法,删除.gradle文件夹,然后rebuild一下,再试试试试行不行,不行?清空studio缓存重启studio,不行?google,百度。。。

(8)ScrollView嵌套RecyclerView,怎么界面展示不全?
–> 重写LinearLyoautManager,GridLayoutManager
–> 将RecyclerView用RelativeLayout或者LinearLayout包围起来
–> 检查检查RecyclerView的高度wrap_content,RecyclerView的ItemView的布局高度wrap_content;

(9)Android studio中关于 No cached version of ** available for of处理
File -> settings -> Build,Execution,Deployment -> Gradle -> 右侧offine work 这里的勾勾去掉 然后重新同步一下

(10)FileProvider —-> Failed to find configured root that contains 问题

<paths>
      
      <cache-path name="name" path="" />
paths>

对应: 
getFilesDir()  "/data/data/com.note.demo/files"  <files-path name="name" path="" />
getCacheDir()  "/data/data/com.note.demo/cache"  <cache-path name="name" path="" />
getExternalFilesDir()  "/storage/emulated/0/Android/data/com.note.demo/files"  <external-files-path name="name" path="" />
getExternalCacheDir()  "/storage/emulated/0/Android/data/com.note.demo/cache"  <external-cache-path name="name" path="" />
Environment.getExternalStorageDirectory  "/storage/emulated/0"  <external-path name="name" path="" />

其他问题 传送门:FileProvider无法获取外置SD卡问题解决方案 | Failed to find configured root that contains

(10)三星手机拍照有些会翻转90度

   /**
     * 读取图片属性:旋转的角度
     *
     * @param path 图片路径
     * @return degree 旋转的角度
     */
    public static int readPictureDegree(String path) {
        int degree = 0;
        try {
            ExifInterface exifInterface = new ExifInterface(path);
            int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
            switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    degree = 90;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    degree = 180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    degree = 270;
                    break;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return degree;
    }

      /**
        * 旋转图片
        * @param degree 旋转角度
        * @param bitmap 初始被旋转的bitmap
        * @return Bitmap 返回正常的bitmap
        */
    public static Bitmap rotaingImageView(int degree, Bitmap bitmap) {
        //旋转图片 动作
        Matrix matrix = new Matrix();
        matrix.postRotate(degree);
        // 创建新的图片
        Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,
                bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        return resizedBitmap;
    }

(10)app跳转到第三方地图进行导航
1.判断是否存在
2.1 不同的坐标系要进行相互转换 !!!
2.2 不同的坐标系要进行相互转换 !!!
2.3 不同的坐标系要进行相互转换 !!!
3.调用

private static final String BaiduMap = "com.baidu.BaiduMap";
    private static final String GaodeMap = "com.autonavi.minimap";
    private static final String TencentMap = "com.tencent.map";

    /**
     * 判断有没有安装应用
     *
     * @param context
     * @param packageName 传入包名
     * @return true 有  false 没有
     */
    private static boolean isAppExist(Context context, String packageName) {
        final PackageManager packageManager = context.getPackageManager();//获取packagemanager
        List installedPackages = packageManager.getInstalledPackages(0);//获取所有已安装程序的包信息
        List pName = new ArrayList();//用于存储所有已安装程序的包名
        //从pinfo中将包名字逐一取出,压入pName list中
        if (installedPackages != null) {
            for (int i = 0; i < installedPackages.size(); i++) {
                String pn = installedPackages.get(i).packageName;
                pName.add(pn);
            }
        }
        return pName.contains(packageName);
    }

    //百度地图是否存在
    public static boolean isBaiduMapExist(Context mContext) {
        return isAppExist(mContext, BaiduMap);
    }

    //高德地图是否存在
    public static boolean isGaodeMapExist(Context mContext) {
        return isAppExist(mContext, GaodeMap);
    }

    //腾讯地图是否存在
    public static boolean isTencentMapExist(Context mContext) {
        return isAppExist(mContext, TencentMap);
    }


    /**
     * 开启百度导航
     */
    public static void startNaviWithBaidu(Context mContext, double endLat, double endLon) {
        Intent naviIntent = new Intent("android.intent.action.VIEW",
                android.net.Uri.parse("baidumap://map/geocoder?location=" + endLat + "," + endLon));
        mContext.startActivity(naviIntent);
    }

    /**
     * 开启网页百度导航
     */
    public static String getWebUrlWithBaidu(double startLat, double startLon, double endLat, double endLon) {
        return "http://api.map.baidu.com/direction?" +
                "origin=latlng:" + startLat + "," + startLon + "|name:起点" +
                "&destination=latlng:" + endLat + "," + endLon + "|name:终点" +
                "&mode=driving®ion=中国&output=html&src=yourAppName";
    }

    /**
     * 开启高德导航
     */
    public static void startNaviWithGaode(Context mContext, double endLat, double endLon) {
        Intent intent = new Intent("android.intent.action.VIEW",
                android.net.Uri.parse("androidamap://route?sourceApplication=appName&slat=&slon=&sname=我的位置&dlat=" + endLat + "&dlon=" + endLon + "&dname=目的地&dev=0&t=2"));
        mContext.startActivity(intent);
    }

    /**
     * 开启网页高德导航
     */
    public static String getWebUrlWithGaode(double startLat, double startLon, double endLat, double endLon) {
        return "http://uri.amap.com/navigation?" +
                "from" + startLon + "," + startLat + ",起点" +
                "&to=" + endLon + "," + startLat + ",终点" +
                "&mode=car&policy=1&src=mypage&coordinate=gaode&callnative=0";
    }

    /**
     * 开启腾讯导航
     */
    public static void startNaviWithTecent(Context mContext, double endLat, double endLon) {
        Intent intent = new Intent("android.intent.action.VIEW",
                android.net.Uri.parse("qqmap://map/routeplan?type=drive&from=&fromcoord=&to=目的地&tocoord=" + endLat + "," + endLon + "&policy=0&referer=appName"));
        mContext.startActivity(intent);
    }

    /**
     * 开启网页腾讯导航
     */
    public static String getWebUrlWithTencent(double startLat, double startLon, double endLat, double endLon) {
        return "http://apis.map.qq.com/uri/v1/routeplan?" +
                "from=起点&fromcoord=" + startLat + "," + startLon +
                "&to=终点&tocoord=" + endLat + "," + endLon +
                "&type=drive&policy=1&referer=myapp";
    }

(11) webview展示地图配置

        WebSettings webSettings = mNaviWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setGeolocationEnabled(true);
        webSettings.setAllowFileAccess(true);
        webSettings.setDomStorageEnabled(true);
        mNaviWebView.setWebViewClient(new WebViewClient());
        mNaviWebView.loadUrl(url);

(12)界面展示一个SurfaceView(地图mapview也是SurfaceView)在页面转换的时候 状态栏会闪烁

getWindow().setFormat(PixelFormat.TRANSLUCENT);

(13)全局设置activity进入退出动画不起作用

<item name="android:windowIsTranslucent">trueitem>//这一行需要删掉
<item name="android:windowAnimationStyle">@style/AnimationActivityitem>

你可能感兴趣的:(android)