android继承View实现复杂的自定义控件(1)

刚接触自定义控件这块内容,想和大家交流下,所以做了这个Demo(程序有些问题会在下面和大家说明)


语言表达不是太好,我就简单说下我做的Demo


1、新建类文件并继承View类,子类复写了onDraw()和onMeasure()方法,实现代码如下:

public class PubView extends View{

private Paint mPaint;
private Paint mPaint1;
private Paint mPaint2;
private Paint mPaint3;


public PubView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setStrokeWidth(1);
mPaint1 = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint1.setStrokeWidth(1);
mPaint1.setStyle(Style.STROKE);
mPaint2 = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint2.setStrokeWidth(1);
mPaint2.setTextSize(24);
mPaint3 = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint3.setStyle(Style.FILL);
mPaint3.setColor(Color.RED);

}


public PubView(Context context) {
super(context);
// TODO Auto-generated constructor stub

}

@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);

Rect mRect = getComRect();   //获取绘图矩形范围

//绘制线段
float[] lines = {0,0,mRect.right,0,
0,mRect.bottom,mRect.right,mRect.bottom,
40,mRect.bottom/2,mRect.right-40,mRect.bottom/2,
mRect.right/2,40,mRect.right/2,mRect.bottom-40};
canvas.drawLines(lines,mPaint);


//绘制矩形
Rect rect = new Rect(40,40,mRect.right-40,mRect.bottom-40);
canvas.drawRect(rect, mPaint1);

//绘制x轴数值
canvas.drawText("-500", 40, mRect.bottom, mPaint2);
canvas.drawText("0", mRect.right/2, mRect.bottom, mPaint2);
canvas.drawText("500", mRect.right-40, mRect.bottom, mPaint2);

//绘制y轴数值
canvas.drawText("1", mRect.right-40, 40, mPaint2);
canvas.drawText("0", mRect.right-40, mRect.bottom/2, mPaint2);
canvas.drawText("-1", mRect.right-40, mRect.bottom-40, mPaint2);

//绘制灯光默认显示区域(圆)
canvas.drawCircle(rect.centerX(), rect.centerY(), 40, mPaint3);

}


      /**
* 定义组建显示区域
* @return
*/

private Rect getComRect(){
//int width = this.getMetries().widthPixels;
//int height = this.getMetries().heightPixels;
Rect mBound = new Rect(0,0,600,420);
return mBound;
}


@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
int width = 0;
int height = 0;
if(widthMode == MeasureSpec.EXACTLY){
width = widthSize;
}else if(widthMode == MeasureSpec.AT_MOST){
width = getComRect().right - getComRect().left;
}

if(heightMode == MeasureSpec.EXACTLY){
height = heightSize;
}else if(heightMode == MeasureSpec.AT_MOST){
height = getComRect().bottom-getComRect().top;
}

setMeasuredDimension(width, height);
}


}



2、在mian.xml中引入自定义组件,如下

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" >
    <com.hxw.carinspection.PubView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/heightView"/>
    
</RelativeLayout>


3、运行后效果图如下:


问题:在设置自定义组件layout_width和layout_height时,无论是设置wrap_content还是match_parent,效果都是一样的,不能完全填充整个手机屏幕,我搞了好长时间也没找到问题所在,希望有大神能够给我解惑~~





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值