View 操作

日常积累View操作,持续更新

尺寸

在View没有绘制之前获取大小

private int[] unDisplayViewSize(View view){
        int[] size = new int[2];
        int width = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
        int height = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
        view.measure(width,height);
        size[0] = view.getMeasuredWidth();
        size[1] = view.getMeasuredHeight();
        return size;
    }
复制代码

View 事件分发

只有可被点击的控件才能够获取到手指的抬起时间,进而获取点击效果。如果想要拦截抬起操作,需要添加

this.setClickable(true)
复制代码

测量文字高度

String test = "QinShiMingYue";
Rect rect = new Rect();
mPaint.getTextBounds(text, 0, test.length(), rect);
int width = rect.width();//文字宽
int height = rect.height();//文字高
复制代码

DataBinding 使用 include标签内部的对象

使用 include 标签内部控件时,需要给 include 一个id,通过id获取控件

 <include
    android:id="@+id/include"
    layout="@layout/item_class_modul" />
复制代码
 mBinding.include.lessonView1.XXX
复制代码

页面切换动画透明度变化

Activity 页面切换时透明度变化,可以把上一个页面设置成透明效果。

<style name="transp_activity" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@color/main_transp</item>
    <item name="android:windowContentOverlay">@null</item>
</style>
复制代码

这样背景的透明度变化就成了页面中背景图的透明度变化

修改应用内部字体

  • 单个字体修改

      Typeface typeface = Typeface.createFromAsset(getAssets(), "syr.ttf");
          mBinding.tvTypefaceTest.setTypeface(typeface);
    复制代码
  • 全局字体的修改

      public class FontsOverride {
          public static void setDefaultFont(Context context,
                                          String staticTypefaceFieldName, String fontAssetName) {
              final Typeface regular = Typeface.createFromAsset(context.getAssets(),
                      fontAssetName);
              replaceFont(staticTypefaceFieldName, regular);
          }
    
          public static boolean isVersionGreaterOrEqualToLollipop() {
              if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                  return true;
              }
              return false;
          }
    
          protected static void replaceFont(String staticTypefaceFieldName, final Typeface newTypeface) {
              if (isVersionGreaterOrEqualToLollipop()) {
                  Map<String, Typeface> newMap = new HashMap<String, Typeface>();
                  newMap.put("sans-serif", newTypeface);
                  try {
                      final Field staticField = Typeface.class.getDeclaredField("sSystemFontMap");
                      staticField.setAccessible(true);
                      staticField.set(null, newMap);
                  } catch (NoSuchFieldException e) {
                      e.printStackTrace();
                  } catch (IllegalAccessException e) {
                      e.printStackTrace();
                  }
              } else {
                  try {
                      final Field staticField = Typeface.class.getDeclaredField(staticTypefaceFieldName);
                      staticField.setAccessible(true);
                      staticField.set(null, newTypeface);
                  } catch (NoSuchFieldException e) {
                      e.printStackTrace();
                  } catch (IllegalAccessException e) {
                      e.printStackTrace();
                  }
              }
          }
      }
    
    
      //application :
       FontsOverride.setDefaultFont(this,"MONOSPACE", "syr.ttf");
    复制代码

    共享元素动画

      // startActivity
      if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
          ActivityOptions options = ActivityOptions.
                  makeSceneTransitionAnimation(getActivity(),
                          new Pair<View, String>(bgview, "lesson_Name"),
                          new Pair<View, String>(parentView.getChildAt(0).findViewById(R.id.iv_class_icon), "class_1"),
                          new Pair<View, String>(parentView.getChildAt(1).findViewById(R.id.iv_class_icon), "class_2"),
                          new Pair<View, String>(parentView.getChildAt(2).findViewById(R.id.iv_class_icon), "class_3"));
    
          startActivity(new Intent(getContext(), LessonModuleActivity.class), options.toBundle());
      }
    复制代码
      // endActivity
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
          // 接收上一个页面的共享元素
          mBinding.ivBackground.setTransitionName("lesson_Name");
    
          mBinding.include.lessonView1.setTransitionName("class_1");
          mBinding.include.lessonView2.setTransitionName("class_2");
          mBinding.include.lessonView3.setTransitionName("class_3");
      }
    
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
          this.setEnterSharedElementCallback(new SharedElementCallback() {
              // 共享元素动画停止
              @Override
              public void onSharedElementEnd(List<String> sharedElementNames, List<View> sharedElements, List<View> sharedElementSnapshots) {
                  super.onSharedElementEnd(sharedElementNames, sharedElements, sharedElementSnapshots);
                  mBinding.sbvBackground.setVisibility(View.VISIBLE);
              }
          });
      }
    复制代码

RecyclerView itemType

使用 RecyclerView 时可能需要设置 itemViewType。当使用时对于页面视图没有相互转化关系,且视图差别较大时可以使用。但是在两个不同 itemView 之间有相互的转化关系是,不推荐使用。 因为不同的 ItemView 之间在进行切换的时候一般不是生硬的进行切换,中间会存在一定的切换动画,如果使用不同的 itemViewType,当数据更新时真实的 itemView 已经改变,前后两个 itemView 的瞬时状态不容易获取,变化过程会变得很复杂。

转载于:https://juejin.im/post/5afe5055f265da0b8d4229d0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值