0

我必须在下面绘制一个自定义视图

在 DaTTab 横幅下方我必须在我的 xml 中绘制矩形视图

  1. 主要的.xml
<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 ;
    }

}

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

和其他链接,但到目前为止还没有运气

4

1 回答 1

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"
        />
于 2012-09-18T17:31:20.930 回答