第1节 原生控件的用法
1.1 View的基本属性
- android:choiceMode
CheckableLinearLayout.java
android:choiceModenone/singleChoice/multipleChoice
multipleChoiceModalsetMultiChoiceModeListener(AbsListView.MultiChoiceModeListener)
1.2 基本控件
TextView
字体颜色设置select
drawable/selector.xml<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <!--点击变红色--> <item android:state_pressed="true" android:color="#ffff0000"/> <!--获得焦点蓝色--> <item android:state_focused="true" android:color="#ff0000ff"/> <!--默认黑色--> <item android:color="#ff000000"/> </selector>
设置属性
android:textColor="@drawable/selector"
文字控制
首行缩进2个字符(中午有效)
setText("\u3000\u3000"+xxxxx);
间距控制
1、android:lineSpacingExtra 设置行间距,如”8dp”。 2、android:lineSpacingMultiplier 设置行间距的倍数,如”1.5″。
ImageView
- ScaleType
MATRIX、FIT_XY、FIT_START、FIT_CENTER、FIT_END、CENTER、CENTER_CROP、CENTER_INSIDE
LinearLayout
LinearLayout 子控件间divider
android:divider="?android:dividerVertical"
android:dividerPadding="8dp"
android:showDividers="middle"
ScrollView
- android:fillViewport 当ScrollView里的元素想填满ScrollView时,使用”fill_parent”是不管用的,必需为ScrollView设置:android:fillViewport=”true”。参考资料
ListView
- scrollbarStyle
**insideOverlay:**默认值,表示在padding区域内并且覆盖在view上
**insideInset**:表示在padding区域内并且插入在view后面
**outsideOverlay:**表示在padding区域外并且覆盖在view上,推荐这个
**outsideInset:**表示在padding区域外并且插入在view后面
EditText
android:imeOptions
条件一android:singleLine="true" 或 android:maxLines="1"
监听处理
etPw.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_GO) { return true ; } return false; } });
1.3 Activity
1.4 Fragment
最小案例分析
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
transaction.commit();
- 显示:add() replace() show() attach()
- 隐藏:remove() hide() detach()
- 调用
commit()
并不立刻执行事务,相反,而是采取预约方式,一旦activity的界面线程(主线程)准备好便可运行起来。然而,如果有必要的话,你可以从界面线程调用executePendingTransations()
立即执行由commit()提交的事务。 - 只能在activity保存状态(当用户离开activity时)之前用commit()提交事务。如果你尝试在那时之后提交,会抛出一个异常。这是因为如果activity需要被恢复,提交后的状态会被丢失。对于这类丢失提交的情况,可使用
commitAllowingStateLoss()
addToBackStack()
调用commit()之前的所有应用的变更被作为一个单独的事务添加到后台栈中,并且BACK键可以将它们一起回退。- 使用
popBackStack()
(模仿用户的BACK命令)从后台栈弹出fragment - 使用
addOnBackStackChangedListener()
注册一个监听后台栈变化的监听器。 - setCustomAnimations(R.anim.slide_right_in,R.anim.slide_left_out) 设置动画
- transaction addSharedElement 设置共享share view
参考
1.4.1 FragmentPagerAdapter更新问题
问题描述: Activity 嵌入FragmentPagerAdapter管理Fragment,当数据更新时,想同时更新的fragment,发现fragment没有更新
FragmentPagerAdapter mAdapter = new FragmentPagerAdapter(getFragmentManager()) {
@Override
public Fragment getItem(int position) {
return mCategoryfragments.get(position);
}
/**
*判断当前position里Fragment是否存在,如果存在,则不会创建亦不会更新
**/
@Override
public long getItemId(int position) {
return mCategoryfragments.get(position).hashCode();
}
/**
*通知刷新位置变化
**/
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
@Override
public int getCount() {
return mCategoryfragments == null ? 0 : mCategoryfragments.size();
}
@Override
public CharSequence getPageTitle(int position) {
return titles[position];
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
//如果注释这行,那么不管怎么切换,page都不会被销毁
super.destroyItem(container, position, object);
}
}
第2节 基础的布局知识
2.1 颜色
颜色透明度参照表
不透明度 | 16进制表示 |
---|---|
00% | FF(不透明) |
5% | F2 |
10% | E5 |
15% | D8 |
20% | CC |
25% | BF |
30% | B2 |
35% | A5 |
40% | 99 |
45% | 8c |
50% | 7F |
55% | 72 |
60% | 66 |
65% | 59 |
70% | 4c |
75% | 3F |
80% | 33 |
85% | 21 |
90% | 19 |
95% | 0c |
100% | 00(全透明) |
算法
30%不透明度也就是255*30%=76.5,四舍五入77,然后利用计算器转为16进制为4D
代码
public static void main(String[] args) {
for (int i = 0; i <= 100; i++) {
float temp = 255 * i * 1.0f / 100f;
int alpha = Math.round(temp);
String hexStr = Integer.toHexString(alpha);
if (hexStr.length() < 2)
hexStr = "0" + hexStr;
System.out.println(i + "%, " + hexStr.toUpperCase());
}
}
第3节 drawbable bitmap shape 等常用的绘图类
3.1 drawable
常用的子类分类
Bitmap:最简单的Drawable,一个PNG或JPEG图像。
Nine Patch:是对PNG格式的扩展允许它指定如何对其进行拉伸和放置的信息
Vector:向量:在XML文件中定义的可绘制的一组点,线和曲线以及相关的颜色信息。 这种类型的绘图可以缩放而不会损失显示质量。
Shape:形状:包含简单的绘图命令而不是原始位图,允许在某些情况下调整更好。
Layers:图层:一个可绘制的复合物,它在彼此顶部绘制多个底层可绘图。
States:状态,一个复合drawable,根据其状态选择一组drawable中的一个。
Levels:级别:一个复合drawable,根据其级别从一组drawable中选择一个。
Scale:比例尺:一个可绘制的单个子组合可绘制的组合,其整体大小根据当前级别进行修改。
第4节 动画相关
基本知识
- Android animations powered by RxJava
- 动画曲线库
- 自然动画的模拟演示
- facebook 令人惊艳的按键效果 rebound
- Lottie-android Render After Effects animations natively on Android and iOS 全平台支持, 动态动画更新;
- 从json文件到炫酷动画-Lottie实现思路和源码分析
- LottieFiles 对应的动画json文件下载
- Lottie 入门与实践
- lottie 文件编辑
案例
渐变实现
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="135"
android:endColor="#34e89e"
android:startColor="#0f3443" />
</shape>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/gradient_blue"
android:duration="5000"/>
</animation-list>
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linear_layout); // 布局background设置是上面的xml
AnimationDrawable animationDrawable = (AnimationDrawable) linearLayout.getBackground();
animationDrawable.setEnterFadeDuration(2500);
animationDrawable.setExitFadeDuration(5000);
animationDrawable.start();
参考
阅读过的博文
- android坐标
- 贝塞尔曲线
- Path相关方法讲解(一)
- Android中检测当前是否为主线程。 最可靠的解决方案:
public static boolean isInMainThread() { return Looper.myLooper() == Looper.getMainLooper(); }
```
- Drawable 着色的后向兼容方案
- VectorDrawable 和 AnimatedVectorDrawable 的兼容性问题 印象有备份
- Android动态布局入门及NinePatchChunk解密 有备份