adb命令来源于\system\core\toolbox 和 \frameworks\base\cmds
Android中每个控件都会在界面中占得一块矩形区域 ViewParent-ViewGroup/View 树形结构
findViewById()深度优先遍历 setContentView()
Activity(PhoneWindow(DecorView(TitleView+ContentView))) 其中DecorView为根View WindowManagerService接收所有View的监听事件
ContentView是一个id为content的FrameLayout TitleView一般为ActionBar Container requestWindowFeature(Window.FEATURE_NO_TITLE)要在setContentView()之前
ActivityManagerService进行Activity的生命周期回调
View的测量:onMeasure()
MeasureSpec是一个32位的int值 高2位为测量模式 低30位为测量的大小
测量模式:
EXACTLY 适用于 android:layout_width="100"或者match_parent
AT_MOST 适用于wrap_content
UNSPECIFIED 适用于自定义View时 想多大就多大
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
//super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(measureWidth(widthMeasureSpec), measureHeight(heightMeasureSpec));
}
private int measureWidth(int measureSpec){
int result = 100;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
if(specMode == MeasureSpec.EXACTLY){
result = specSize;
}else if(specMode == MeasureSpec.AT_MOST){
result = Math.min(result, specSize);
}
return result;
}
View的绘制:onDraw()
Canvas对象 其它地方Canvas canvas = new Canvas(bitmap); canvas.drawXXX();
ViewGroup的测量:遍历子View
ViewGroup的绘制:dispatchDraw()
自定义View:扩展现有控件/组合实现控件/重写View实现
onFinishInflate()/onSizeChanged()/onMeasure()/onLayout()/onDraw()/onTouchEvent()
定义属性:res/values/attrs.xml
string|dimension|reference|color|boolean|enum
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TopBar);
ta.recycle();
xmlns:custom="http://schemas.android.com/apk/res-auto"
重绘:invalidate();/postInvalidateDelayed(300);
自定义ViewGroup:int count = getChildCount(); View childView = getChildAt(i);
事件拦截处理机制
MotionEvent ACTION_DOWN ACTION_MOVE ACTION_UP
public boolean dispatchTouchEvent(MotionEvent ev);
public boolean onInterceptTouchEvent(MotionEvent ev);//View没有此方法
public boolean onTouchEvent(MotionEvent ev);
流程:Adispatch->Aintercept->Bdispatch->Bintercept->Viewdispatch->ViewonTouch->BonTouch->AouTouch
谁拦截谁处理,处理完就回调
public class DataAdapter extends BaseAdapter{
private ArrayList
private LayoutInflater mInflater;
public DataAdapter(Context context){
mDatas = new ArrayList<>();
mInflater = LayoutInflater.from(context);
}
@Override
public int getCount(){return mDatas.size();}
@Override
public Object getItem(int position){return mData.get(position);}
@Override
public long getItemId(int position){return position;}
@Override
public View getView(int position, View convertView, ViewGroup ){
ViewHolder holder = null;
if(convertView == null){
convertView = mInflater.inflate(R.layout.item_content, null);
holder = new ViewHolder();
holder.content = (TextView)convertView.findViewById(R.id.content);
convertView.setTag(holder);
}else{
holder = (ViewHolder)convertView.getTag();
}
holder.content.setText("use ViewHolder cache");
return convertView;
}
public final class ViewHolder{
public TextView content;
}
}
设置分隔线:android:divider="@android:color/darker_grey" android:dividerHeight="10dp"
隐藏分隔线:android:divider="@null"
隐藏滚动条:android:scrollbars="none"
取消Item的点击效果:android:listSelector="#00000000" 或 android:listSelector="@android:color/transparent"
显示在第几项:listView.setSelection(N);listView.smoothScrollXX();
notifyDataSetChanged();
空ListView可以设置setEmptyView()来显示内容为空时的提示
ListView的滑动监听:GestureDetector/VelocityTracker onTouchListener/onScrollListener
firstVisibleItem/visibleItemCount/totalItemCount
overScrollBy()中maxOverScrollY改为一个数字就可以弹性十足了
自动显示隐藏的ListView
//添加头防止ActionBar引藏了第一个Item
View header = new View(this);
header.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, (int)getResources().getDimension(R.dimen_abc_action_bar_default_height_material)));
mListView.setHeader(header);
mTouchSlop = ViewConfiguration.get(this).getScaledTouchSlop();//获取最低滑动距离
View.OnTouchListener mOnTouchListener = new View.OnTouchListener(){……};//动画用属性动画
不同布局的ListView
getItemViewType()/getViewTypeCount()
滑动一个View本质上就是移动一个View改变其当前所处位置
Android坐标系:屏幕左上角(0, 0) 右 X正 下 Y正 getLocationOnScreen(int[] location); getRawX()/getRawY()
视图坐标系:相对于父视图 getX()/getY()
MotionEvent ACTION_DOWN/ACTION_UP/ACTION_MOVE/ACTION_CANCEL/ACTION_OUTSIDE/ACTION_POINTER_DOWN/ACTION_POINTER_UP
View提供的获取坐标的方法:getTop()/getLeft()/getRight()/getBottom()
MotionEvent提供的方法:getX()/getY()/getRawX()/getRawY()
实现滑动的八种方法:
layout()/offsetLeftAndRight()和offsetTopAndBottom()/LayoutParams(getLayoutParams()/setLayoutParams)/scrollTo与scrollBy
Scroller/属性动画/ViewDragHelper(说好八种只有七种还有一种,你想想)
屏幕大小:屏幕对角线的长度,单位寸,如5.36寸
分辨率:屏幕的像素点数,720*1280指宽有720个像素点高有1280个像素点
PPI:Pixels Per Inch或DPI,Dots Per Inch 对角线像素点数除以屏幕大小
hdpi:240 / 480 X 800
dp/sp
public class DisplayUtil{
public static int px2dip(Context context, float pxValue){
final float scale = context.getResources().getDisplayMetrics().density;
return (int)(pxValue / scale + 0.5f);
}
public static int dip2px(Context context, float dipValue){
final float scale = context.getResources().getDisplayMetrics().density;
return (int)(dipValue * scale + 0.5f);
}
public static int px2sp(Context context, float pxValue){
final float fontScale = context.getResources().getDisplayMetrics().density;
return (int)(pxValue / fontScale + 0.5f);
}
public static int sp2px(Context context, float spValue){
final float fontScale = context.getResources().getDisplayMetrics().density;
return (int)(spValue * fontScale + 0.5f);
}
}
//TypedValue类也可以进行转换
protected dp2px(int dp){
return (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics());
}
protected sp2px(int sp){
return (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, getResources().getDisplayMetrics());
}
2D绘图:Canvas/Paint
Android XML绘图:
Canvas.save()//类似保存图层
Canvas.restore()//类似合并图层
Canvas.translate()//平移坐标原点
Canvas.rotate()//翻转坐标原点
saveLayer()/saveLayerAlpha()/restore()/restoreToCount()
Bitmap位图ARGB
色彩矩阵:ColorMatrix 4X5 一维数组存储 矩阵乘法 RGBA 改变系数 改变偏移量
色调:setRotate()/饱和度:setSaturation()/亮度:setScale()
mImageView.setImageBitmap(ImageHelper.handleImageEffect());
paint.setColorFilter(new ColorMatrixColorFilter(imageMatrix));
像素点分析:
bitmap.getPixels(pixels, offset, stride, x, y, width, height);
bitmap.setPixels(newPx, 0, width, 0, 0, width, height);
Android图像处理之图形特效处理Matrix 3x3
Translate/Rotate/Scale/Skew
像素块分析:drawBitmapMesh()
画笔特效处理:PorterDuffXfermode
Shader着色器:BitmapShader/LinearGradient/RadialGradient/SweepGradient/ComposeShader
PathEffect
public class SurfaceViewTemplate extends SurfaceView implements SurfaceHolder.Callback, Runnable
surfaceCreated()/surfaceChanged()/surfaceDestroyed()/run()
private SurfaceHolder mHolder;
private Canvas mCanvas;
private boolean isDrawing;
mHolder = this.getHolder();
mHolder.addCallback(this);
mCanvas = mHolder.lockCanvas();
mHolder.unlockCanvasAndPost(mCanvas);