美文网首页
自定义控件的小知识

自定义控件的小知识

作者: NullPointe_8e82 | 来源:发表于2019-01-11 16:02 被阅读0次

canvas.drawBitmap(mBitmap,srcRect,desRect,mPaint);

第二个参数srcRect指原图将要显示内容的多少(即是如何裁剪),如果

srcRect=new Rect(0,0,mBitmap.getWidth()/2,mBitmap.getHeight()/2);

则显示原图的1/4的区域,如果为null则表示不裁剪,完全显示原图

第三个参数desRect指裁剪后的图片将要显示在什么位置和显示在屏幕有多大,如果

desRect=new Rect(500,500,mBitmap.getWidth(),mBitmap.getHeight());

则表示图片将要在坐标点(500,500)显示一张与原图大小的图片

自身中心大小进行放大缩小的动画

ScaleAnimation scaleAnimation=new ScaleAnimation(1.0f,1.5f,1.0f,1.5f,

Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);

获取自定义控件宽高大小的方法有两种

方法一、

@Override

protected void onSizeChanged(int w,int h,int oldw,int oldh) {

super.onSizeChanged(w,h,oldw,oldh);

mTotalWidth =w;

mTotalHeight =h;

Log.e("test","mTotalWidth="+mTotalWidth+",mTotalHeight="+mTotalHeight);

}

方法二、

@Override

protected void onMeasure(int widthMeasureSpec,int heightMeasureSpec) {

super.onMeasure(widthMeasureSpec,heightMeasureSpec);

int widthSize =MeasureSpec.getSize(widthMeasureSpec);

int heightSize =MeasureSpec.getSize(heightMeasureSpec);

Log.e("test","widthSize="+widthSize+",heightSize="+heightSize);

}

需要注意view的坐标是相对父容器而言的,包括:getTop()、getBottom(),getLeft(),getRight()

getRawX(),getRawY() 相对于屏幕位置坐标

getX(),getY() 相对于容器的位置坐标




相关文章

网友评论

      本文标题:自定义控件的小知识

      本文链接:https://www.haomeiwen.com/subject/hkvxdqtx.html