图像绘制
//绘制图像(位图对象,位图左上角x坐标,位图左上角y坐标)
canvas.drawBitmap(mBitmap, 50, 10, null);
//第二个参数表示图像上的区域,可以null,null表示全图
//第三个参数表示画布上显示图像的区域
canvas.drawBitmap(gameBitmap, bitmapRect, showRect, null);
图像变换
需要有Matrix对象来处理
Matrix mMatrix = new Matrix(); //角度0 缩放倍数1 偏移0
set:
设置一个新状态
mMatrix.setTranslate(100,200);
post:
基于上一个状态累加新状态
mMatrix.postRotate(90);
pre:
前置状态,如需要对图片进行平移(100,200)放大2倍
mMatrix.postTranslate(100,200);
mMatrix.postScare(2,2);
mMatrix.postScare(2,2);
mMatrix.preTranslate(100,200);
//基于矩阵绘制图像
canvas.drawBitmap(mBitmap,mMatrix,null);
多点触控
int action = event.getAction();
switch (action & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
//单个触控按下
break;
case MotionEvent.ACTION_POINTER_DOWN:
//多个触控点按下
break;
case MotionEvent.ACTION_MOVE:
按下之后离开之前都会触发
break;
case MotionEvent.ACTION_UP:
单个触控离开
break;
case MotionEvent.ACTION_POINTER_UP:
多个触控离开
break;
}
获取精确的触控点个数
int count = event.getPointerCount(); //获取总个数
for(int i = 0 ; i < count ; i++){
int x = event.getX(i);
int y = event.getY(i);
}
自定义ViewGroup
onMeasure先measure孩子再measure自身
measureChildren(widthMeasureSpec,heightMeasureSpec);
或者
int count = getChildCount();
int totalHeight = MeasureSpec.getSize(heightMeasureSpec);
for (int i = 0; i < count; i++) {
View child = getChildAt(i);
measureChild(child, widthMeasureSpec,heightMeasureSpec);
}
measure自身
setMeasuredDimension(widthMeasureSpec, resolveSize(totalHeight, heightMeasureSpec));
onLayout布置孩子的显示
//对孩子进行布局的方法
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int width = getMeasuredWidth() / 2;
int count = getChildCount();
int currentHeight = 0;
for (int i = 0; i < count; i++) {
View child = getChildAt(i);
int left = 0;
if (i % 2 != 0) {
left = width;
}
child.layout(left, currentHeight,
left + width, currentHeight + child.getMeasuredHeight());
//累计高度
currentHeight += child.getMeasuredHeight();
}
}