Android项目必备技术

1.一个APP只需要一个Activity

    //片段fragment
    implementation 'me.yokeyword:fragmentation:1.3.6'
    implementation 'me.yokeyword:fragmentation-swipeback:1.3.6'

2.懒人必备查找控件

//ButterKnife(10.0必须适配AndroidX,9.0需要java8,快速生成插件android-butterknife-zelezny)
    implementation 'com.jakewharton:butterknife:9.0.0'
    annotationProcessor 'com.jakewharton:butterknife-compiler:9.0.0'

3.方法数超过65k,合并

  • a.引入jar包
    implementation "com.android.support:multidex:1.0.3"
  • b.配置合并dex为开启
    defaultConfig {
        multiDexEnabled true
    }

 

  • c.在自己的MyApplication下的方法中初始化
  @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

4.新版本的Gradle,需要至少在一个Activity中的 里面添加:

 

 

5.拉的自己封装的工具类需要去掉,.git的关联

https://blog.csdn.net/lyj1005353553/article/details/55519487

6.设置去掉所有的页面标题栏

在AppTheme中配置

 

 true

7.快速构建Builder插件

innerbuilder插件

8.使用Dagger2无法找到对于的Component类,

a.必须在基类的onCreate方法中注入

 

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        DaggerCommonComponent.create().inject(this);
    }

而不要注入到另一个onCreate中

 

    @Override
    public void onCreate( @Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
    
    }

9.RecyclerView嵌套ScrollView不流畅

 

recyclerView.setHasFixedSize(true);
recyclerView.setNestedScrollingEnabled(false);

垂直滑动问题:
https://segmentfault.com/a/1190000011553735
recyclerview嵌套在NestedScrollView里,一次性加载出全部数据问题
https://github.com/CymChad/BaseRecyclerViewAdapterHelper/issues/1954
Android SwipeRefreshLayout和RecyclerView嵌套时 下拉刷新冲突的解决办法
https://blog.csdn.net/peirato_/article/details/54913195

针对RecyclerView不显示,只需要设置ScrollView的属性android:layout_height="match_parent" android:fillViewport="true" 就OK了。

  • 监听RecyclerView滚动距离

 

 //RecyclerView滚动监听
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                switch (newState){
                    case RecyclerView.SCROLL_STATE_IDLE://现在不是滚动状态
                        L.e("滚动的距离=="+direction);
                        break;
                    case RecyclerView.SCROLL_STATE_DRAGGING://手指 拖动
                        break;
                    case RecyclerView.SCROLL_STATE_SETTLING://惯性滚动
                        break;
                }
            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                //计算RecyclerView滚动的距离
                direction += dy;
            }
        });

RecyclerView解决数据混乱,禁止复用

 

recyclerView.getRecycledViewPool().setMaxRecycledViews(viewType,0);

https://blog.csdn.net/adojayfan/article/details/87934157

10.Retrofit网络请求,生成

 

 private static void setRetrofit(String defaultHost) {
        retrofit = new Retrofit.Builder()
                        .baseUrl(defaultHost)
                        .client(okHttpClient)
                        .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                        .addConverterFactory(JSONObjectConverterFactory.create())
                        .addConverterFactory(GsonConverterFactory.create()).build();
    }

其中JSONObjectConverterFactory和GsonConverterFactory不能共存,如果想返回JSONObject对象,去掉 .addConverterFactory(GsonConverterFactory.create())如果想直接生成Object对象,去掉.addConverterFactory(JSONObjectConverterFactory.create())

11.TextView加入删除线

 

tv.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);

代码中绘制左侧图片

 

Drawable img = layout.getResources().getDrawable(R.drawable.icon);  
// 调用setCompoundDrawables时,必须调用Drawable.setBounds()方法,否则图片不显示  
img.setBounds(0, 0, img.getMinimumWidth(), img.getMinimumHeight());  
textView.setCompoundDrawables(img, null, null, null); //设置左图标  

文字滚动消息:

 

 < TextView
        android:layout_width ="wrap_content"
        android:layout_height ="wrap_content"
        android:focusable ="true"
        android:focusableInTouchMode ="true"
        android:marqueeRepeatLimit ="marquee_forever"
        android:ellipsize ="marquee"
        android:singleLine ="true"
        android:text ="那么什么是成功的人生,什么样的人又是成功人士呢?像科技界的比尔?盖茨 ,乔布斯 ,商界的巴菲特以及娱乐、体育界的大腕,无疑会被人们视为是成功者。但对于普通民众,如果用比尔?盖茨那样的标准来衡量是否成功似乎有些太苛刻,也不现实。在某种观念中,一个人的成功似乎与财富分不开的。很多国人到美国看到华人最常说的一句话是,你是个成功人士。为什么这么说?因为你能住300平方米、价值百万美元的房子,因为你开的车是奔驰 、宝马等豪华车,因为你有好的工作,年薪至少在10万美元以上。这也许是很多国人看待一个人成功与否的主要标志。但在美国人眼里,成功并不是与拥有众多财富密不可分。在1980年代,多数美国人把拥有更多财富看成人生成功的一个主要标志。而在一项最新的调查中,对于美国人来说,财富不再是成功的最重要组成部分。调查中22个成功人生的潜在组成因素中,“有很多钱”仅排名在第20位。" />

12.对Retrofit回调进行封装,只输出正确和错误信息

自定义Retrofit网络回调结果

13.EditText变搜索按钮,并监听搜索事件

xml中设置android:imeOptions="actionSearch"android:singleLine="true"
代码中

 

        etSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH){
                    String keyWord = v.getText().toString();
                    if (TextUtils.isEmpty(keyWord)){
                        ToastUtil.showLong("请输入搜索内容!");
                        return false;
                    }
                    //开始搜索keyWord相关内容
                }
                return false;
            }
        });

14.CoordinatorLayout布局要设置滚动控件的

 

app:layout_behavior="@string/appbar_scrolling_view_behavior"

里面的字符串爆红,但还是可以运行,但红色总是不好看的,可能是新版本的sdk引起的,所以需要改为

 

app:layout_behavior ="android.support.design.widget.AppBarLayout$ScrollingViewBehavior"

15.WebView不跳转到外部浏览器

 

class MyWebViewClient extends WebViewClient{
        //不跳转到外部浏览器
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }

Android webview loadData 中文乱码
https://www.jianshu.com/p/85957f003dd4

webview加载html图片过大左右滑动的解决/webview加载图片自适应大小

16.WebView出现net::ERR_UNKNOWN_URL_SCHEME错误

https://www.jianshu.com/p/119823e5cfb5

17.Glide显示图片为上面圆角,下面直角

让Glide输出指定位置的圆角图片 2018年,部分方法为Glide4.0以前的,所以无法使用,但方法值得借鉴
Glide 加载部分圆角图片2019年,新的方法,且行为更合理
圆角不圆:有可能是因为图片高度或宽度过大,导致部分圆角不圆
Glide ViewTarget及SimpleTarget加载问题:

18.View转换为Bitmap

 

private Bitmap loadBitmapFromView(View v) {
        int w = v.getWidth();
        int h = v.getHeight();
        Bitmap bmp = Bitmap.createBitmap(w, h, Bitmap.Config.RGB_565);
        Canvas c = new Canvas(bmp);
        c.drawColor(Color.WHITE);
        /** 如果不设置canvas画布为白色,则生成透明 */
        v.layout(0, 0, w, h);
        v.draw(c);
        return bmp;
    }

关于View转化成bitmap保存成图片
两个Bitmap合并为一个
Android:将一个Activity、某块布局转换成图片

19.使用xml绘制虚线好后,4.0以上需要使用 android:layerType="software"属性,不然虚线会变为实线

https://blog.csdn.net/Small_Lee/article/details/52153557

20.微信分享到朋友圈,调用系统分享,需要对Uri单独处理一下

https://blog.csdn.net/qq_34900897/article/details/85320646

21.support版本冲突的解决办法

https://blog.csdn.net/yuzhiqiang_1993/article/details/78214812

22.TabLayout+ViewPager垂直方向联动,用于分类页面

https://www.jianshu.com/p/9266e58cc4f5

23.精确计算RecyclerView滑动高度

SlideRecyclerView

24.adb无线调试

https://jingyan.baidu.com/article/066074d610f4f3c3c21cb0ab.html

25.打包名称自定义

https://www.cnblogs.com/bluestorm/p/6228085.html
https://blog.csdn.net/weixin_33709364/article/details/87160660
打包方法过时警告:https://www.cnblogs.com/blogs-of-lxl/p/10306145.html
我的通用命名方式:包名最后一部分+版本名称+时间+打包方式 taobao_v1.0_2019-05-20_release
在android{}里面写入

 


android.applicationVariants.all { variant ->
        variant.outputs.all{
            outputFileName  = "${applicationId.subSequence(applicationId.lastIndexOf(".")+1,applicationId.length())}_v${versionName}_${releaseTime()}_${baseName}.apk"
        }
    }

其中releaseTime()为写在android{}外部的一个方法

 

def releaseTime() {
    return new Date().format("yyyy-MM-dd", TimeZone.getTimeZone("UTC"))
}

26.apk签名打包,

如何配置签名及生成签名文件:
https://www.cnblogs.com/details-666/p/keystore.html
如何判断你的apk是否已经签名:
https://blog.csdn.net/qq_21376985/article/details/53337977

27.混淆

https://blog.csdn.net/qq634416025/article/details/79686051

28.TextView设置可滚动的方式

xml中设置TextView属性

 

android:scrollbars="vertical"

同时代码中设置

 

 textView.setMovementMethod(ScrollingMovementMethod.getInstance());

29.Android:通过Glide保存图片到本地,并同步到相册

30.Android 调用系统分享(不使用第三方),指定QQ、微信等

https://blog.csdn.net/u010356768/article/details/78246691
qq空间限制:
https://blog.csdn.net/weixin_41239127/article/details/78743421

31.安卓从imageview中获得bitmap的方法

https://blog.csdn.net/yj1499945/article/details/47079621

32.怎样获取到Android控件的高度,通过监听的方式

https://www.jianshu.com/p/2c8e5324ec68

你可能也不知道为什么,第一次进入页面,获取控件的高度有值,再次进入获取的高度居然为0,再再再次进入也为0,杀掉应用,进入页面又有高度了,再次进入又为0。因为我需要通过view来获取Bitmap,那么View的宽高值必不可少,所以我通过上面博客的方法去监听控件的高度才拿到值。但为什么只有第一次进入才能拿到宽高值却拜师不得琪姐,请各位大老解答。

33.AccessibilityService获取控件信息getRootInActiveWindow() 经常为null

https://blog.csdn.net/qq_28210079/article/details/80486592

33.解决android.permission.WRITE_APN_SETTINGS

https://blog.csdn.net/qq_36437339/article/details/81015715

34.新手引导库

GuideView

在Fragment中由于控件位置绘制流程和生命周期的关系,需要监听控件View宽高,有值后才进行引导层的绘制,同时用Handler进行一定的延迟绘制,保证高亮区域的定位精确度

35.Java中对对象进行排序

https://blog.csdn.net/qq_37937537/article/details/80445731

通过Comparable

36.监控当前APP是否回到前台

https://www.jianshu.com/p/101eb42d0fde

37.使用java8新特性快速的打印输出序列对象

 

allList.stream().map(User::toString).forEach(L::e);

38.Fragment的优化

不要再Activity中使用List来保存Fragment
https://blog.csdn.net/qq_30993595/article/details/80736814

39.ViewPager2,支持横竖布局,支持0预加载布局

https://juejin.im/post/5cda3964f265da035d0c9d8f

40.RadioGroup.onCheckedChanged() 会调用多次

https://blog.csdn.net/qq_32452623/article/details/80474487

41.Retrofit2上传图片

https://www.cnblogs.com/zhujiabin/p/7601658.html

42.Java多线程

Java多线程系列目录(共43篇)

43.ArrayList进行排序

24版本以前的老方法:
Collections.sort(arrays);
新方法
arrays.sort();

44.对时间的转换,对比,添加,格式输出

joda-time

45. tools:replace的使用

tools:replace=""有时候需要替换多个项,使用逗号分割
tools:replace="android:allowBackup,android:appComponentFactory"

46. 解决 导入三方时出现: appComponentFactory 错误

https://blog.csdn.net/qq_34224268/article/details/83861897

47. 滚轮时间选择器

可以采用 XPopupWheelPicker
组合的方式来生成一个时间选择器如:https://www.jianshu.com/p/4a2c853d9276

48. fragment里coordinatorlayout+viewpager无法正常滑动问题

fragment里coordinatorlayout+viewpager无法正常滑动问题
有人说fragment是无法运行协调者布局的,这是错误的
解决方法:在你的viewpager子fragment里面布局最外面套上一层NestedScrollView就可以了

49.查看SHA1命令

keytool -list -v -keystore C:\Users\Desktop\browser\debug.keystore -storepass android
后面的android为当前密钥的密码

50.RecyclerView横向分页菜单

https://blog.csdn.net/u014165119/article/details/46834265

51.ArgbEvaluator一个计算颜色渐变值的类

https://blog.csdn.net/u013581141/article/details/68063469
使用事例:自定义CoordinatorLayout.Behavior颜色渐变的TitleBar

52.交互效果咋了系列:

自定义ViewGroup第十三式之移花接木

53.数据绑定DataBind

 

android {
    …
    dataBinding {
        enabled = true
    }
}

告别findView和ButterKnife
Android开发教程 - 使用Data Binding(七)使用BindingAdapter简化图片加载

54.错误检查命令

gradlew processDebugManifest --stacktrace

55.new Handler的警告说明和解决办法@SuppressLint(“HandlerLeak”)

https://blog.csdn.net/androidsj/article/details/79865091

 

    private MyHandler myHandler = new MyHandler(_mActivity);
    private static class MyHandler extends Handler {
        WeakReference weakReference;
        public MyHandler(Activity activity) {
            weakReference = new WeakReference(activity);
        }
        @Override
        public void handleMessage(Message msg) {

        }
    }

另一种写法:

 

private Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            return false;
        }
    }); 

56.多行且需要最后显示省略号的警告处理

logcat 总是报: W/StaticLayout: maxLineHeight should not be -1. maxLines:1 lineCount:1

57.通知无法显示:8.0需要设置渠道和权重

https://www.jianshu.com/p/f85ef58edf63

58.uri指向了外部应用exposed beyond app through RemoteViews.setUri()

在Application中的onCreate方法中添加如下

 

StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
        StrictMode.setVmPolicy(builder.build());
        builder.detectFileUriExposure();

59.解决因为弹窗导致TextView停止滚动的问题:

https://www.jianshu.com/p/22b4aff0dc8e

60.弹窗需要一个自定义的三角形SanJiaoView

https://blog.csdn.net/ZhangLei280/article/details/73207669

61.升级AndroidX后出现缺失AppBarLayout$ScrollingViewBehavior

https://www.jianshu.com/p/6b8104787617

62.APP启动时间,性能优化,启动页白屏

https://www.jianshu.com/p/75b0b128c470

63.AndroidStudio安装失败

image.png

 

方法一:clean项目
方法二:重启大法:重启AndroidStudio,重启手机,重启电脑,重启...
方法三:检查是否只开启了开发者模式和USB调试,却没有开启了USB安装

 

Android项目必备技术_第1张图片

image.png

64.adb命令

https://www.jianshu.com/p/56fd03f1aaae

65.ConstraintLayout基础 及动态控件(动画效果)

https://www.jianshu.com/p/7888cde8292f

66.获取videoView第一针视频报错:

https://blog.csdn.net/guohesheng/article/details/80236799

67.Math函数相关计算

//角度换算为对应数值
double skewRot = Math.toRadians(30);

68. banner库一张图片无效和第一次进入没有轮播的时候点击第一张无效

一张图片的时候需要设置banneronClick事件,多张图第一次还未开始轮播点击无效,需要先设置点击事件,再调用bannerstart方法

69. 标签的使用

标签的使用

70.Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK

https://blog.csdn.net/watermusicyes/article/details/44963773

71.程序崩溃日志收集

 

 Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(Thread thread, final Throwable ex) {
                // Custom code here to handle the error.
                L.e("发生崩溃=="+thread.getName()+"  =="+ex.getMessage());
            }
        });

72.Android Q正式发布。新一代的安卓系统非常注重对用户隐私的保护,它限制APP获取IMEI、DEVICE ID等移动端设备标识码,解决方案:

简书:https://www.jianshu.com/p/df3f549ddd35
官方:https://developer.android.google.cn/training/articles/user-data-ids

73.再按一次退出程序

 

    private long firstTime = 0;
    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                long secondTime = System.currentTimeMillis();
                //如果两次按键时间间隔大于2秒,则不退出
                if (secondTime - firstTime > 2000) {
                    Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
                    firstTime = secondTime;//更新firstTime
                    return true;
                    //两次按键小于2秒时,退出应用
                } else {
                    System.exit(0);
                }
                break;
        }
        return super.onKeyUp(keyCode, event);
    }

如果是Fragment请不要复写onBackPressed()方法,改为复写onBackPressedSupport():

 

    //再按一次退出程序
    private long firstTime = 0;
    @Override
    public void onBackPressedSupport() {
        long secondTime = System.currentTimeMillis();
        if (getSupportFragmentManager().getBackStackEntryCount() == 1 && secondTime - firstTime > 2000) {
            Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
            firstTime = secondTime;
        } else {
            super.onBackPressedSupport();
        }
    }

74.AndroidX升级日记

https://www.jianshu.com/p/499e645ad148

75.AndroidStudio启动模拟器Android10.0报错

Emulator: Process finished with exit code -1073741819 (0xC0000005)
https://stackoverflow.com/questions/47631771/emulator-process-finished-with-exit-code-1073741819-0xc0000005

76.简单实现ImageView宽度填满屏幕,高度自适应的两种方式

https://www.jianshu.com/p/c9424615e99d

77.RecyclerView瀑布流问题

https://www.jianshu.com/p/4e142909b824
https://blog.csdn.net/tobevan/article/details/78924338

78.android studio关闭字符串资源多语言提示

https://blog.csdn.net/sinat_26814541/article/details/97757535

79.ConstraintLayout 采用代码方式布局用法简介

http://xgfe.github.io/2017/09/17/ivanchou/layout-with-constraintlayout-by-programming/

80.Glide获得图片资源,并高斯模糊

高斯模糊是个耗时过程,需要在子线程进行操作

 

        try {
            Drawable drawable = Glide.with(mContext)
                    .load(item.getImage_url())
                    .apply(RequestOptions.bitmapTransform(new BlurTransformation(15, 1)))
                    .submit().get();
            layoutRoot.setBackground(drawable);
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

高斯模糊

 

public class BlurTransformation extends BitmapTransformation {
    private static final int VERSION = 1;
    private static final String ID = "BlurTransformation." + VERSION;

    private static int MAX_RADIUS = 25;
    private static int DEFAULT_DOWN_SAMPLING = 1;

    private int radius;
    private int sampling;

    public BlurTransformation() {
        this(MAX_RADIUS, DEFAULT_DOWN_SAMPLING);
    }

    public BlurTransformation(int radius) {
        this(radius, DEFAULT_DOWN_SAMPLING);
    }

    public BlurTransformation(int radius, int sampling) {
        this.radius = radius;
        this.sampling = sampling;
    }

    @Override
    protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) {
        int width = toTransform.getWidth();
        int height = toTransform.getHeight();
        int scaledWidth = width / sampling;
        int scaledHeight = height / sampling;

        Bitmap bitmap = pool.get(scaledWidth, scaledHeight, Bitmap.Config.ARGB_8888);

        Canvas canvas = new Canvas(bitmap);
        canvas.scale(1 / (float) sampling, 1 / (float) sampling);
        Paint paint = new Paint();
        paint.setFlags(Paint.FILTER_BITMAP_FLAG);
        canvas.drawBitmap(toTransform, 0, 0, paint);
        bitmap = FastBlur.blur(bitmap, radius, true);

        return bitmap;
    }

    @Override public String toString() {
        return "BlurTransformation(radius=" + radius + ", sampling=" + sampling + ")";
    }

    @Override public boolean equals(Object o) {
        return o instanceof BlurTransformation &&
                ((BlurTransformation) o).radius == radius &&
                ((BlurTransformation) o).sampling == sampling;
    }

    @Override public int hashCode() {
        return ID.hashCode() + radius * 1000 + sampling * 10;
    }

    @Override public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
        messageDigest.update((ID + radius + sampling).getBytes(CHARSET));
    }

}

其中的FastBlur来至库glide-transformations

81.Glide获得获取图片主体色系

 

  Glide.with(mContext).load(item.getImage_url())
                .listener(GlidePalette.with(item.getImage_url())
                        .use(GlidePalette.Profile.VIBRANT)
                        .intoBackground(layoutRoot, GlidePalette.Swatch.RGB)
                        .crossfade(true)
                ).into(imageView);

82.Android获取内网IP

方法一:

 

  package com.rongyan.clienttest;
 
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
public class NetWorkUtil {
    //匹配C类地址的IP
    public static final String regexCIp = "^192\\.168\\.(\\d{1}|[1-9]\\d|1\\d{2}|2[0-4]\\d|25\\d)\\.(\\d{1}|[1-9]\\d|1\\d{2}|2[0-4]\\d|25\\d)$";
    //匹配A类地址
    public static final String regexAIp = "^10\\.(\\d{1}|[1-9]\\d|1\\d{2}|2[0-4]\\d|25\\d)\\.(\\d{1}|[1-9]\\d|1\\d{2}|2[0-4]\\d|25\\d)\\.(\\d{1}|[1-9]\\d|1\\d{2}|2[0-4]\\d|25\\d)$";
    //匹配B类地址
    public static final String regexBIp = "^172\\.(1[6-9]|2\\d|3[0-1])\\.(\\d{1}|[1-9]\\d|1\\d{2}|2[0-4]\\d|25\\d)\\.(\\d{1}|[1-9]\\d|1\\d{2}|2[0-4]\\d|25\\d)$";
 
 
    public static String getHostIp() {
        String hostIp;
        Pattern ip = Pattern.compile("(" + regexAIp + ")|" + "(" + regexBIp + ")|" + "(" + regexCIp + ")");
        Enumeration networkInterfaces = null;
        try {
            networkInterfaces = NetworkInterface.getNetworkInterfaces();
        } catch (SocketException e) {
            e.printStackTrace();
        }
        InetAddress address;
        while (networkInterfaces.hasMoreElements()) {
            NetworkInterface networkInterface = networkInterfaces.nextElement();
            Enumeration inetAddresses = networkInterface.getInetAddresses();
            while (inetAddresses.hasMoreElements()) {
                address = inetAddresses.nextElement();
                String hostAddress = address.getHostAddress();
                Matcher matcher = ip.matcher(hostAddress);
                if (matcher.matches()) {
                    hostIp = hostAddress;
                    return hostIp;
                }
 
            }
        }
        return null;
    }
}

方法二:

 

    public String getWifiIp() {
        //获取wifi服务
        WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        //判断wifi是否开启
        if (!wifiManager.isWifiEnabled()) {
            wifiManager.setWifiEnabled(true);
        }
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        int ipAddress = wifiInfo.getIpAddress();
        return intToIp(ipAddress);
    }
    //获取Wifi ip 地址
    private String intToIp(int i) {
        return (i & 0xFF) + "." +
                ((i >> 8) & 0xFF) + "." +
                ((i >> 16) & 0xFF) + "." +
                (i >> 24 & 0xFF);
    }

参考:https://www.cnblogs.com/jxust-jiege666/p/8168149.html

83.Android获取外网IP

参考:https://www.jianshu.com/p/1e3eaf887191

方法一,通过访问第三方接口地址来获取

 

import org.json.JSONException;
import org.json.JSONObject;
 /**
     * 获取外网IP地址
     * @return
     */
    public void GetNetIp() {
        new Thread(){
            @Override
            public void run() {
                String line = "";
                URL infoUrl = null;
                InputStream inStream = null;
                try {
                    infoUrl = new URL("http://pv.sohu.com/cityjson?ie=utf-8");
                    URLConnection connection = infoUrl.openConnection();
                    HttpURLConnection httpConnection = (HttpURLConnection) connection;
                    int responseCode = httpConnection.getResponseCode();
                    if (responseCode == HttpURLConnection.HTTP_OK) {
                        inStream = httpConnection.getInputStream();
                        BufferedReader reader = new BufferedReader(new InputStreamReader(inStream, "utf-8"));
                        StringBuilder strber = new StringBuilder();
                        while ((line = reader.readLine()) != null)
                            strber.append(line + "\n");
                        inStream.close();
                        // 从反馈的结果中提取出IP地址
                        int start = strber.indexOf("{");
                        int end = strber.indexOf("}");
                        String json = strber.substring(start, end + 1);
                        if (json != null) {

                            JSONObject jsonObject = null;
                            try {
                                jsonObject = new JSONObject(json);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            line = jsonObject.optString("cip");

                        }
                        L.e("line=="+line);
                        Message msg = new Message();
                        msg.what = 1;
                        msg.obj = line;

                    }
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

84.颜色均匀渐变的动画

 

ValueAnimator animator = ValueAnimator.ofInt(0xffffff00,0xff0000ff);
animator.setEvaluator(new ArgbEvaluator());
animator.setDuration(3000);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
    @Override
    public void onAnimationUpdate(ValueAnimator animation) {
        int curValue = (int)animation.getAnimatedValue();
        tv.setBackgroundColor(curValue);
 
    }
});
animator.start();

85.Android的cpu架构7种

参考:https://blog.csdn.net/u012400885/article/details/52923765
https://www.jianshu.com/p/f29ad4beef59

86.'gradle' 不是内部或外部命令,也不是可运行的程序 或批处理文件

https://blog.csdn.net/u014743890/article/details/84316176

87.使用ImmersionBar时,加载出现Dialog的显示会导致导航栏出现不消失:

88.Dagger2-Android不支持泛型Activity的注入

https://blog.csdn.net/ybf326/article/details/82931587

89.删除git配置

Android项目必备技术_第2张图片

删除

90.把本地项目复制到github

Android项目必备技术_第3张图片

分享

91.AndroidStudio设置github账号

Android项目必备技术_第4张图片

image.png

92.邀请码识别来源

https://www.openinstall.io/

93.Android 自动抓取网站图标实现分享样式的定制

https://blog.csdn.net/dnsliu/article/details/57122535

94.Fragment实现懒加载

https://blog.csdn.net/vic6329063/article/details/82838430

95.版权登记

http://www.ccopyright.com/index.php?optionid=1216

96.获取随机颜色值

分别取rgb的随机值(0~256),然后加起来就是一个随机颜色值,通过Color.parseColor()转为color值即可使用:

 

public static String getRandColor() {
        String R, G, B;
        Random random = new Random();
        R = Integer.toHexString(random.nextInt(256)).toUpperCase();
        G = Integer.toHexString(random.nextInt(256)).toUpperCase();
        B = Integer.toHexString(random.nextInt(256)).toUpperCase();

        R = R.length() == 1 ? "0" + R : R;
        G = G.length() == 1 ? "0" + G : G;
        B = B.length() == 1 ? "0" + B : B;

        return "#" + R + G + B;
    }

97.Glide设置圆角图片后设置ImageVIew的scanType="centerCrop"无效解决办法

https://www.jianshu.com/p/95d3f64a48dc

 

RequestOptions options = new RequestOptions()
                .transform(new CenterCrop(),new RoundedCorners(18))
                .placeholder(R.drawable.no_banner)
                .error(R.drawable.no_banner);
        Glide.with(context).load(path)
                .apply(options)
                .thumbnail(0.1f)
                .into(imageView);

98.线性渐变字体

https://blog.csdn.net/negineko/article/details/100033250

 

public class GradientColorTextView extends AppCompatTextView {

    private Rect mTextBound = new Rect();

    public GradientColorTextView (Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int mViewWidth = getMeasuredWidth();
        Paint mPaint = getPaint();
        String mTipText = getText().toString();
        mPaint.getTextBounds(mTipText, 0, mTipText.length(), mTextBound);
        @SuppressLint("DrawAllocation") LinearGradient mLinearGradient = new LinearGradient(0, 0, mViewWidth, 0, new int[]{0xFFFF0000, 0xFF5400FF}, null, Shader.TileMode.REPEAT);
        mPaint.setShader(mLinearGradient);
        canvas.drawText(mTipText, getMeasuredWidth() / 2 - mTextBound.width() / 2, getMeasuredHeight() / 2 + mTextBound.height() / 2, mPaint);
    }
}

99.TextView 设置其他字体后,显示不全的问题

https://blog.csdn.net/u010979599/article/details/86650297
https://www.jianshu.com/p/4314cc68c1f3

100.Android视频播放,选择,压缩,上传

https://www.jianshu.com/p/78b7176c041e

101.循环枚举获得枚举值

 

public static  & BaseEnum> T getEnumType(String enumCode, Class enumClass) {
  T enumType = null;
  EnumSet enumSet = EnumSet.allOf(enumClass);   //获取枚举类型
  for (T enumItem : enumSet) {       //循环枚举
    if(enumItem.getEnumCode().equals(enumCode)) {
      enumType = enumItem;
      break;
    }
  }
  return enumType;
}

102.权限申请

AndPermission
文档:https://yanzhenjie.com/AndPermission/cn/

 

AndPermission.with(FortuneFragment.this)
                                .runtime()
                                .permission(Permission.Group.STORAGE)
                                .onGranted(permissions -> {
                                    ToastUtil.showShort("get permiss");
                                })
                                .onDenied(new Action>() {
                                    @Override
                                    public void onAction(List permissions) {
                                        ToastUtil.showShort("你拒绝了获取此权限!");
                                        // 这些权限被用户总是拒绝。
                                        if (AndPermission.hasAlwaysDeniedPermission(FortuneFragment.this, permissions)) {
                                            new AlertDialog.Builder(FortuneFragment.this.getActivity())
                                                    .setTitle("权限申请")
                                                    .setMessage("需要此权限才能使用此功能,去设置?")
                                                    .setPositiveButton("去设置", new DialogInterface.OnClickListener() {
                                                        @Override
                                                        public void onClick(DialogInterface dialog, int which) {
                                                            AppUtils.goIntentSetting(FortuneFragment.this.getActivity());
                                                        }
                                                    })
                                                    .setNegativeButton("取消",null)
                                                    .show();
                                        }
                                    }
                                })
                                .start();

103.Android TextView 判断文字内容是否超出显示省略号

https://blog.csdn.net/gentlemanyc/article/details/49967719?locationNum=2

104.关于Android弹出软键盘“顶起”View的问题

https://blog.csdn.net/q4878802/article/details/94382815

 

public void setHintKeyboardView(View view) {
            view.setOnTouchListener(new View.OnTouchListener() {
                public boolean onTouch(View v, MotionEvent event) {
                    hintKeyboard(NewsInfoActivity.this);
                    return false;
                }
            });
            if (view instanceof ViewGroup){
                ViewGroup viewGroup = (ViewGroup) view;
                for (int i = 0; i < viewGroup.getChildCount(); i++) {
                    View innerView =  viewGroup.getChildAt(i);
                    setHintKeyboardView(innerView);
                }
            }
    }

要顶起的控件

 

 setHintKeyboardView(db.layoutComment);

主要布局xml中要设置:android:fitsSystemWindows="true"才生效

105.DataBind中神奇的“最”字

出现异常:
Caused by: org.apache.xerces.impl.io.MalformedByteSequenceException: Invalid byte 3 of 3-byte UTF-8 sequence.
原因是在xml中使用了“最”字和对象的结合

 


估计是DataBind的一个bug,只有在代码中写这个“最”字了。

 

db.textView18.setText("最大特征:"+data.getAttr().getZdtd());

106.可以调整间隙的星星控件

SimpleRatingBar
如果使用DataBind出错,需要下载下来,写入设置星星个数的setter方法。

107.背景高斯模糊

 

 private Bitmap applyBlur() {
        Bitmap bitmap = ScreenUtils.snapShotWithStatusBar(this.getActivity());
        return blur(bitmap);
    }
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    private Bitmap blur(Bitmap bkg) {
        long startMs = System.currentTimeMillis();
        float radius = 1;
        bkg = small(bkg);
        Bitmap bitmap = bkg.copy(bkg.getConfig(), true);

        final RenderScript rs = RenderScript.create(this.getContext());
        final Allocation input = Allocation.createFromBitmap(rs, bkg, Allocation.MipmapControl.MIPMAP_NONE,
                Allocation.USAGE_SCRIPT);
        final Allocation output = Allocation.createTyped(rs, input.getType());
        final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
        script.setRadius(radius);
        script.setInput(input);
        script.forEach(output);
        output.copyTo(bitmap);

        bitmap = big(bitmap);
        MainActivity.ivCover.setBackground(new BitmapDrawable(getResources(), bitmap));
        rs.destroy();
        Log.d("zhangle","blur take away:" + (System.currentTimeMillis() - startMs )+ "ms");
        return bitmap;
    }

    private static Bitmap big(Bitmap bitmap) {
        Matrix matrix = new Matrix();
        matrix.postScale(4f,4f); //长和宽放大缩小的比例
        Bitmap resizeBmp = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
        return resizeBmp;
    }

    private static Bitmap small(Bitmap bitmap) {
        Matrix matrix = new Matrix();
        matrix.postScale(0.25f,0.25f); //长和宽放大缩小的比例
        Bitmap resizeBmp = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
        return resizeBmp;
    }

108.java Enum 枚举帮助类,根据值或者名称获取枚举类型对象

https://blog.csdn.net/busbanana/article/details/72954676

109'Android打开系统拍照&相册获取头像

https://www.jianshu.com/p/e29e24b88440

110. FileProvider相关

Android之FileProvider :通过FileProvider来获取content uri
https://blog.csdn.net/yegshun/article/details/81478619
android 7.0+ FileProvider 访问隐私文件 相册、相机、安装应用的适配

111. view 可见性 监听探究

https://www.jianshu.com/p/a5cc954b997c

112. android checkbox 未选中状态 已选中状态 替换成自己的图片

113. 获取今日,明日,本周,本月,今年时间

https://www.cnblogs.com/shuilangyizu/p/6902643.html

 

        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00"));
        int firstDayOfWeek = calendar.getFirstDayOfWeek();
        List times  =new ArrayList<>();
        for (int i = 0; i < 7; i++) {
            calendar.set(Calendar.DAY_OF_WEEK, firstDayOfWeek + i);
            // 获取星期的显示名称,例如:周一、星期一、Monday等等
            String format = new SimpleDateFormat("MM月dd日").format(calendar.getTime());
            times.add(format);
        }
        String time6 = times.get(6);
        time6 = time6.substring(time6.indexOf("月")+1);

        calendar.add(Calendar.DAY_OF_MONTH, -1);
        String jr = new SimpleDateFormat("MM月dd日").format(calendar.getTime());
        calendar.add(Calendar.DAY_OF_MONTH, +1);
        String mr = new SimpleDateFormat("MM月dd日").format(calendar.getTime());
        dayName[0] = "今日运势("+jr+")";
        dayName[1] = "明日运势("+mr+")";
        dayName[2] = "本周运势("+times.get(0)+"-"+time6+")";
        dayName[3] = "本月运势("+(calendar.get(Calendar.MONTH) + 1)+"月)";
        dayName[4] = "今年运势("+calendar.get(Calendar.YEAR)+"年)";
        dayNameShow = dayName[0];

114. Android定位相关

Android系统GPS定位实现
Andriod 手机定位 解决location为null的问题
Android地图开发中的地理编码与地理反编码
经纬度查询:https://www.juhe.cn/cellmap/lat
Android之GPS定位类 LocationManager、LocationListener、GpsStatus.Listener、Location详解

总结,使用:
(方式一)locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 3000, 0, locationListener);
(方式二)locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 0, locationListener);好用得多!
使用网络方式定位比gps定位更高概率拿到Location对象,从而获取经纬度。但要注意使用网络LocationManager.NETWORK_PROVIDER的方式,依然要开启gps和网络,否则依然无法获取Location对象。而(方式二)我只有一次获取成功了,之后的监听再也没拿到过Location对象。

115. Android TextView加上阴影效果

https://blog.csdn.net/hewence1/article/details/39993415

116.Android中给View设置阴影的三种方式

https://blog.csdn.net/wang29169/article/details/84206379
关于CardView参考:使用CardView实现卡片式设计
或者第三方的控件 shadow-layout

117.Android去掉阻尼效果

只需要在控件中添加如下属性即可:android:overScrollMode="never"

118.Android版本升级坑

https://www.jianshu.com/p/123c12218b7a

119.startActivityForResult的用法,以及intent传递图片

https://blog.csdn.net/bangxianzhou5100/article/details/101077196

120.第三方地图接入相关

高德地图接入笔记


注:https://www.jianshu.com/p/6be91ee932a7
 

 

 

 

你可能感兴趣的:(Android项目必备技术)