一些杂乱的知识点(二)

一些杂乱的知识点(二)

1.menu菜单

  1. 添加菜单方式 通过一个布局 在resmenu目录下创建一个布局

    <item
        android:id="@+id/add_item"
        android:title="Add" />
    
  2. 重写ActivityonCreateOptionsMenu()方法

    getMenuInflater().inflate(R.menu.main,menu);
    return true;
    
  3. 动态的添加菜单

    menu.add(0,1,0,"后退");
    menu.add(0,2,0,"前进")

  4. 点击menu按键弹出一个对话框 如果你想实现自己定义的view效果,则需要重写onMenuOpened()方法

  5. 定义菜单响应事件:重写ActivityonOptionsItemSelected()方法,根据item.getItemId()即可响应不同的菜单选项的点击事件.

2. AutoCompleteTextView控件的使用

    String[] countries = new String[] { "Belgium",
                "France", "Italy", "Germany", "Spain" };
    AutoCompleteTextView actv_test = (AutoCompleteTextView) findViewById(R.id.actv_test);

            //2. 创建adapter
            ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
                    android.R.layout.simple_dropdown_item_1line, countries);

            //3. 设置adapter
            actv_test.setAdapter(adapter);

3. 补间动画

  1. 透明

    //1.0意味着着完全不透明 0.0意味着完全透明
    AlphaAnimation aa = new AlphaAnimation(1.0f, 0.0f);
    aa.setDuration(2000); //设置动画执行的时间
    aa.setRepeatCount(1); //设置重复的次数
    aa.setRepeatMode(Animation.REVERSE);//设置动画执行的模式
    //iv开始执行动画 
    iv.startAnimation(aa);
    
  2. 旋转

    //fromDegrees 开始角度   toDegrees 结束角度
    //      RotateAnimation  ra = new RotateAnimation(0, 360);  
    RotateAnimation ra = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    ra.setDuration(2000); //设置动画执行的时间
    ra.setRepeatCount(1); //设置重复的次数
    ra.setRepeatMode(Animation.REVERSE);//设置动画执行的模式
    //iv开始执行动画 
    iv.startAnimation(ra);
    
  3. 缩放

    ScaleAnimation sa = new ScaleAnimation(1.0f,2.0f, 1.0f, 2.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    sa.setDuration(2000); //设置动画执行的时间
    sa.setRepeatCount(1); //设置重复的次数
    sa.setRepeatMode(Animation.REVERSE);//设置动画执行的模式
    //iv开始执行动画 
    iv.startAnimation(sa);
    
  4. 位移

    TranslateAnimation ta = new TranslateAnimation
    (Animation.RELATIVE_TO_PARENT, 0, 
    Animation.RELATIVE_TO_PARENT, 0, 
    Animation.RELATIVE_TO_PARENT, 0, 
    Animation.RELATIVE_TO_PARENT, 0.2f);
    ta.setDuration(2000); //设置动画执行的时间
    ta.setFillAfter(true);//当动画结束后 动画停留在结束位置
    
    //开始动画
    iv.startAnimation(ta);
    

总结:补间动画不会改变控件真实的坐标

4. 使用xml文件方式定义补间动画

在res下创建一个目录 anim目录.然后使用时只需要Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.alpha);即可获得该动画的实例.补间动画是开发中用的比较多的.

5. 应用程序(apk)的反编译

apktools 
dex2jar  jd.exe 查看程序源码

Android逆向助手 直接有图形化操作界面 查看源码的话,需要在助手中选择dexjar.
一般我们不会去看别人的源码,没必要,现在大多数应用程序都已经混淆代码了,看起来很费劲.我们主要是反编译获取里面的res目录下的东西,比如动画布局等等.

6. 属性动画

属性动画会改变控件真实的坐标

如果使用xml方式定义属性动画 要在res下创建一个animator目录

7. Manager

  • SmsManager 短信
  • TelephoneManager 电话管理者
  • WindowManager 窗口管理者
  • FragmentManager fragment管理者
  • NotificationManager 通知管理者

8.通知栏 通知 介绍

具体实现如下

    //1. 获取NotificationManager实例
    notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    //创建打电话的Intent
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_CALL);
    intent.setData(Uri.parse("tel:"+112));
    //延时的Intent
    PendingIntent callPhone = PendingIntent.getActivity(getApplicationContext(), 1, intent,Intent.FLAG_ACTIVITY_NEW_TASK);
    //2. 创建Notification对象     这是高版本的写法    链式调用
    Notification notification = new Notification.Builder(this)
            .setContentTitle("我是大标题")
            .setContentText("我是内容")
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentIntent(callPhone)
            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
            .build();

    //实现呼吸灯闪烁   并且振动
    notification.defaults = Notification.DEFAULT_ALL;

    //3. 发送通知   id是通知的标识
    notificationManager.notify(10, notification);

    //取消通知
    notificationManager.cancel(10);

9. 服务与通知连用

在服务的OnCreate()方法里写startForeground(id, notification),瞬间将服务提升为前台进程.这样这个进程就更不容易被系统杀死.

10. 设置为全屏显示

  • activity中加入代码requestWindowFeature(Window.FEATURE_NO_TITLE),但是这个方式有个缺陷,就是只能将当前的activity设置为全屏.如果页面比较多的话,建议用下面的方式.

  • 在清单文件中,修改主程序的android:theme="@android:style/Theme.light.NoTitleBar"即可;(如果是Activity继承自AppCompatActivity,则应该改为android:theme="@style/Theme.AppCompat.NoActionBar").现在当前应用程序的所有Activity都是全屏的.

11. Log的使用

  • 一般tag都是当前类所对应的名称
  • tag一般是这样定义(这样很方便,免得重复写很多次):
    private static final String TAG = "SplashActivity";
  • Log一般会封装到LogUtil中,方便在发布的时候关闭log.

    /**
     * 定制自己的日志工具
     *
     * 开发阶段设置成VERBOSE,当项目正式上线的时候将LEVEL指定成NOTHING就可以了.
     */
    
    public class LogUtil {
    
        public static final int VERBOSE = 1;
        public static final int DEBUG = 2;
        public static final int INFO = 3;
        public static final int WARN = 4;
        public static final int ERROR = 5;
        public static final int NOTHING = 6;
    
        //控制这个即可控制Log输出
        public static final int LEVEL = VERBOSE;
    
        public static void v(String tag, String msg) {
            if(LEVEL <= VERBOSE){
                Log.v(tag,msg);
            }
        }
    
        public static void d(String tag, String msg){
            if(LEVEL <= DEBUG){
                Log.d(tag,msg);
            }
        }
    
        public static void i(String tag, String msg){
            if(LEVEL <= INFO){
                Log.i(tag,msg);
            }
        }
    
        public static void w(String tag, String msg){
            if(LEVEL <= WARN){
                Log.w(tag,msg);
            }
        }
    
        public static void e(String tag, String msg){
            if(LEVEL <= ERROR){
                Log.e(tag,msg);
            }
        }
    
    }
    

12. 对于Android项目来说,有一个标准的需要忽略的文件(当然,也可以根据自己的项目而定),在GitHub上去下载Android.gitignore文件。下面为Android.gitignore文件的内容:

    <span style="font-size:14px;"># Built application files  
    *.apk  
    *.ap_  

    # Files for the Dalvik VM  
    *.dex  

    # Java class files  
    *.class  

    # Generated files  
    bin/  
    gen/  
    out/  

    # Gradle files  
    .gradle/  
    build/  

    # Local configuration file (sdk path, etc)  
    local.properties  

    # Proguard folder generated by Eclipse  
    proguard/  

    # Log Files  
    *.log  

    # Android Studio Navigation editor temp files  
    .navigation/  

    # Android Studio captures folder  
    captures/  

    # Intellij  
    *.iml  

    # Keystore files  
    *.jks</span>  

它主要的写法规则为:”#”表示注释,”*”表示通配符,可多个,”/”表示目录,”!”表示不忽略匹配到的文件或目录,”?”表示通配单个字符,”[]”表示包含单个字符的匹配列表。

13. 打包生成apk过程

  1. 生成签名文件,并且指定所在位置
  2. 使用生成的签名文件,给工程打包生成一个apk

14. 升级apk注意事项

  1. 将原有应用覆盖掉,包名一致
  2. 签名也需要一致 从Android Studio运行到手机上的使用debug.keystore签名的应用.
  3. 签名一致,包名不同:生成2个app,包名是应用的唯一性标志
  4. 签名不同,包名一致,覆盖安装失败
  5. keystore+密码需要妥善保管,svn服务器或者网盘拿来保管

15. 系统会对Button中的所有英文字母自动进行大写转换

如果这不是你想要的效果,可以使用如下配置来禁用这一默认特性:

    android:textAllCaps="false"

16. 图片资源的存放

图片通常都是放在以drawable开头的目录下的,不过默认的drawable没有指定分辨率,所以一般不使用它来放置图片.一般放在drawable-xhdpi目录.

17. Button 里面的英文字母默认大写

解决办法:android:textAllCaps="false"

18. Nine-Patch图片

它是一种被特殊处理过的png图片,能够指定哪些区域可以被拉伸,哪些区域不可以.

使用方法:

  1. 在Android SDK目录下有一个tools文件夹,在这个文件夹中找到draw9patch.bat文件,我们就是使用它来制作Nine-Patch图片的.
  2. 双击打开draw9patch.bat文件,在导航栏点击 File → Open 9-patch 将 .png 的素材文件加载进来。
  3. 在图片的四个边框绘制一个个的小黑点,在上边框和左边框绘制的部分就表示当图片需要拉伸时就拉伸黑点标记的区域,在下边框和右边框绘制的部分则表示内容会被放置的区域。

19. 通知 Notification

support-v4库中提供了一个notificationCompat类,使用这个类的构造器来创建Notification对象,就可以保证我们的程序在所有的Android系统版本上都能正常工作了.

通知入门

private void notificate(Context context){
    //1. 获取NotificationManager对象
    NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    //2.准备PendingIntent对象
    Intent intent =  new Intent(context, NotificationActivity.class);
    intent.putExtra("address",from);
    intent.putExtra("messageBody",messageBody);
    /**
     * 主要提供了几个静态方法获取PendingIntent的实例,可以根据具体的需求来选择是使用getActivity()方法,
     * getBroadcast()方法,还是getService()方法方法.这几个方法所接收的我参数都是相同的.第一个参数依旧是
     * Context,第二个参数一般用不到,通常传入0即可,第3个参数是Intent对象,我们可以通过这个对象构建出PendingIntent的意图
     * ,第4个参数用于确定PendingIntent的行为,具体的去查文档,一般传入0即可.
     */
    PendingIntent pendingIntent = PendingIntent.getActivity(context,0,intent,0);

    //3. 创建Notification对象
    Notification notification = new NotificationCompat.Builder(context)
            .setContentTitle(from)   //设置标题
            .setContentText(messageBody)   //设置内容
            .setWhen(System.currentTimeMillis())  //设置时间
            .setContentIntent(pendingIntent)     //设置点击事件  跳转
            .setAutoCancel(true)    //设置点击hour,自动取消通知
            .setSmallIcon(R.drawable.small_icon)  //设置小图标
            .setLargeIcon(BitmapFactory.decodeResource(context.getResources(),R.drawable.large_icon))
            .builder.setProgress(100, 30, false);   //设置进度条
            .build();
    //4.显示通知   参数:id,notification对象    要保证每个通知的id是不同的
    manager.notify(1,notification);

通知的进阶技巧

  • 播放音频
    setSound(Uri.fromFile(new File("/system/media/audio/ringtones/Luna.ogg")))

  • 设置振动(需要配置权限<uses-permission android:name="android.permission.VIBRATE")
    setVibrate(new long[]{0,1000,1000,1000})

  • 设置LED灯
    setLights(Color.GREEN,1000,1000)

  • 不想设置上面那么多繁琐的设置,下面是直接使用默认的通知效果
    setDefaults(NotificationCompat.DEFAULT_ALL)

通知的高级功能

  • 显示一段长文字(默认是显示不完的,以省略号代替,下面这样写了,就不会这样了)
    setStyle(new NotificationCompat.BigTextStyle().bigText("我是一段长文字,我是一段长文字,我是一段长文字,我是一段长文字,我是一段长文字,我是一段长文字,我是一段长文字,我是一段长文字,我是一段长文字,我是一段长文字,我是一段长文字,我是一段长文字"))

  • 显示大图片
    setStyle(new NotificationCompat.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(getResource(),R.drawable.big_image)))

  • 设置重要程度(越重要的越前面显示)
    一共有5个重要程度等级,这里设置最大值
    setPriority(NotificationCompat.PRIORITY_MAX)
    ,现在的通知不是在系统状态栏显示一个小图标了,而是弹出了一个横幅,并附带了通知的详细内容,表示这是一条非常重要的通知.

20. 获取当前用户的设备的系统版本

Build.VERSION.SDK_INT
返回的是系统版本的api版本,比如Android 7.0就返回24;

21. 沉浸式状态栏

在Android 5.0 之前,我们是无法对状态栏的背景或颜色进行操作的,那个时候也还没有Material Design的概念.
想要让背景图能够和状态栏融合,需要借助android:fitsSystemWindows这个属性来实现.

21.1 使用Material Design方式实现

效果如下:

1.在CoordinatorLayout,AppBarLayout,CollapsingToolbarLayout,ImageView,这种嵌套结构的布局中,将android:fitsSystemWindows属性指定成true;

2.还需要在程序的主题中将状态栏的颜色指定成透明色才行.指定成透明色的方法:在主题中将android:statusBarColor属性的值指定成@android:color/transparent就可以了. 但是有个问题,android:statusBarColor属性是从API 21 开始的,也就是Android 5.0 系统才开始有的.那么,系统差异型的功能实现就要从这里开始了.

在res下创建目录values-21,然后在该目录下创建values resources file ,写入如下代码:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <!--
        values-v21 是android 5.0以上系统才回去读取的
        这个主题是专门为FruitActivity使用的
    -->
    <style name="FruitActivityTheme" parent="AppTheme">
        <item name="android:statusBarColor">@android:color/transparent</item>
    </style>

</resources>

但是,在5.0 之前的系统无法识别FruitActivityTheme这个主题,所以需要在res->values->styles.xml添加2行代码,中间什么都没有.

<style name="FruitActivityTheme" parent="AppTheme">
</style>

3.最后在清单文件中设置FruitActivity的主题时,这样设置android:theme="@style/FruitActivityTheme";

即可实现状态栏沉浸式效果.

21.2 另一种更简单的方式实现

1.在Activity代码的onCreate()方法中,写入

//状态栏沉浸式效果    下面的方法只适合Android 5.0 以上系统
    if (Build.VERSION.SDK_INT >= 21) {
        View decorView = getWindow().getDecorView();  //改变系统UI的显示
        decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE); //表示活动的布局会显示在状态栏上
        getWindow().setStatusBarColor(Color.TRANSPARENT);    //将状态栏设置成透明
    }

2.最后需要设置Activity的布局的顶部布局的android:fitsSystemWindows="true"

22. 通过selector的xml选择View的背景

示例如下:

1.新建一个shape_bg_point_enable.xml 作为View可用时的背景

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="oval">

    <!-- 这是选中状态 -->

    <!-- 半径 -->
    <corners android:radius="10dp"/>

    <!-- 背景颜色 -->
    <solid android:color="#FFFFFF"/>

</shape>

2.新建一个shape_bg_point_disable.xml 作为View不可用时的背景

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval" >

    <corners android:radius="10dp"/>

    <solid android:color="@android:color/darker_gray"/>

</shape>

3.新建一个selector_bg_point.xml 来作为选择器

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <!-- 根据是否enable来选择加载那个背景 -->
    <item android:state_enabled="true" android:drawable="@drawable/shape_bg_point_enable" />
    <item android:state_enabled="false" android:drawable="@drawable/shape_bg_point_disable" />
</selector>

使用方法:
比如:pointView.setBackgroundResource(R.drawable.selector_bg_point); // 设置背景 btn.setEnabled(true); 当按钮可用时,自动选择shape_bg_point_enable.xml作为背景,否则选择shape_bg_point_disable.xml作为背景.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值