Android 学习 - Vo7ice/Vo7ice.github.io GitHub Wiki

知识点

  • 记录遇到的问题所需要的知识

动画

MotionEvent

坐标系

  1. View提供的获取坐标方法 - getTop(): 获取到的是View自身的顶边到其父布局顶边的距离 - getLeft(): 获取到的是View自身的左边到其父布局左边的距离 - getRight(): 获取到的是View自身的右边到其父布局左边的距离 - getBottom(): 获取到的是View自身的底边到其父布局顶边的距离

  2. MotionEvent提供的方法 - getX(): 获取点击事件距离控件左边的距离,即视图坐标 - getY(): 获取点击事件距离控件顶边的距离,即视图坐标 - getRawX(): 获取点击事件距离整个屏幕顶边的距离,即绝对坐标 - getRawY(): 获取点击事件距离整个屏幕左边的距离,即绝对坐标

Tip

  • 修改默认加密手机:修改fstab.in中的flag FLAG_FDE_TYPE /或修改device\mediatek\ [project]\ fstab.{ro.hardware} forceencrypt->encrypt

  • 自定义控件属性时: TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.TopBar); obtainStyledAttributes需要调用两个参数的函数,不然自定义属性不起作用

  • onDraw方法中调用画布canvastranslate方法将绘制的位置进行平移(+width为左,+height为下)

  • LinearGradient渐变器中

    public LinearGradient(
            float x0,//渐变器x起始线
            float y0,//渐变器y起始线
            float x1,//渐变器x终止线 x1-x0为渐变器宽度
            float y1,//渐变器y终止线 y1-y0为渐变器高度
            int color0,//靠近起始线颜色
            int color1,//靠近终止线颜色
            TileMode tile)//平铺形式 有CLAMP,REPEAT,MIRROR

    图像渲染(Shader)

  • 为控件添加触摸水波纹效果,在style中配置了material主题后在xml布局文件中增加属性

    • android:background="?android:attr/selectableItemBackground"

    • android:background="?android:attr/selectableItemBackgroundBorderless"

    • <item name="android:colorControlHighlight">@color/colorAccent</item> 可以更改波纹的颜色

    • 为自定义背景添加水波纹效果

      <ripple xmlns:android="http://schemas.android.com/apk/res/android"
        android:color="@android:color/white"><!-- 水波纹颜色 必须-->
        <item>
          <!-- 原block-->
        </item>
      </ripple>
  • 给英文设置粗体的方法:

    • xmlandroid:textStyle="bold"
    • 在代码中,tv.getPaint().setFakeBoldText(true);
  • hasOverLapping 自定义 View 时重写 hasOverlappingRendering 方法指定 View 是否有 Overlapping 的情况,提高渲染性能.

      @Override
      public boolean hasOverlappingRendering() {
          return false;
      }
  • View.getLocationInWindow()View.getLocationOnScreen()区别

// location [0]--->x坐标,location [1]--->y坐标
int[] location = new  int[2];
// 获取在当前窗口内的绝对坐标,getLeft,getTop,getBottom,getRight 这一组是获取相对在它父窗口里的坐标.
view.getLocationInWindow(location); 
// 获取在整个屏幕内的绝对坐标,注意这个值是要从屏幕顶端算起,也就是包括了通知栏的高度.
view.getLocationOnScreen(location);
  1. 如果在ActivityOnCreate()事件输出那些参数,是全为0,要等UI控件都加载完了才能获取到这些.在onWindowFocusChanged(boolean hasFocus)中获取为好.
  2. View.getLocationInWindow()View.getLocationOnScreen()window占据全部screen时,返回值相同. 不同的典型情况是在Dialog中时,当Dialog出现在屏幕中间时,View.getLocationOnScreen()取得的值要比View.getLocationInWindow()取得的值要大
  • cardview加上点击效果(水波纹) layout中增加android:foreground="?attr/selectableItemBackground"
  • Gson解析json数据的时候,如果jsonkey是动态的,可以用Map<String T>来保存

Android Studio Tips

  1. 自动导包失效 清除缓存並重启(File-->Invalidate Cache\Restart...)
  2. Android studio最实用快捷键
  3. Android studio使用android.os.SystemProperties方法
在Moudul的build.gradle中定义这个jar包路径,编译进应用中
def getLayoutLibPath() {
   return "${android.getSdkDirectory().getAbsolutePath()}" + "/platforms/" +
          android.compileSdkVersion + "/data/layoutlib.jar"
}
provided files(getLayoutLibPath())

Material Icon

技术blog主

⚠️ **GitHub.com Fallback** ⚠️