用于绘制自定义视图的剩余视图的大小

时间:2012-09-18 16:36:59

标签: android android-layout

  

我必须在横幅下方绘制一个自定义视图,就像附加的PICK

一样      

在DaTTab横幅下面我必须绘制矩形视图my xml

     
      
  1. main.xml
  2.   
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/relative_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

  <include android:id="@+id/banner" layout="@layout/upper_border" />

<com.example.ui_1.Border_Portrait
        android:id="@+id/custom_display_view1"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/banner" 
        />
  

Draw_Border.jav

class Border_Portrait extends View{
     private static final int DEFAULT_SIZE = 100;

     int mViewHeight , mViewWidth ;
     Paint mPaint ;
     Canvas mCanvas ;
     int margin ;
     float [] mPoints ;
     int mLogoHeight ;
     int mLogoWidth ;
     int dy,dx ;

    public Border_Portrait(Context context) {
        super(context);

    }
    public Border_Portrait(Context context, AttributeSet attrs) {
        super(context, attrs);
        margin = 7 ;
        mPaint = new Paint();
        mPaint.setColor(Color.rgb(0,188,226));  
        mPaint.setStrokeWidth(3);
        mLogoHeight = ScreenUtils.convertDIPToPixels(getContext(), 50 + margin );
        mLogoWidth = ScreenUtils.convertDIPToPixels(getContext(), 66 + margin); 
        dy = ScreenUtils.convertDIPToPixels(getContext(),1) ;
        dx= ScreenUtils.convertDIPToPixels(getContext(),1) ;
    }

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

        mPoints  = new float [] { 
                    margin + mLogoWidth ,margin,//1
                    getWidth()-margin ,margin ,//1
                    getWidth()-margin ,margin ,//2      
                    getWidth()-margin , getHeight()-margin ,//2 
                    getWidth()-margin , getHeight()-margin  , //3
                    margin , getHeight()-margin , //3
                    margin , getHeight() - margin , //4 
                    margin , mLogoHeight + dy ,//4
                    margin , mLogoHeight + dy , // 5
                    margin + mLogoWidth + dx ,mLogoHeight + dy, //5
                    margin + mLogoWidth ,mLogoHeight + dy, //6
                    margin + mLogoWidth ,margin, //6
                    } ;
        canvas.drawLines(mPoints, mPaint);
        canvas.restore();
    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
        int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
        this.setMeasuredDimension(parentWidth, parentHeight);
    }

    private int calculateMeasure(int measureSpec) {
        return 0 ;
    }

}

我的问题是如何在我参考的运行时测量边框的宽度

THIS

和其他链接,但直到现在没有运气

1 个答案:

答案 0 :(得分:0)

  

刚刚在main.xml中添加了边距

<com.example.ui_1.Border_Portrait
        android:id="@+id/custom_display_view1"
        android:layout_marginTop="50dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />