Custom Android Views in Eclipse Visual Editor

前端 未结 3 1079
萌比男神i
萌比男神i 2020-12-09 12:22

In my applications, I often rely on custom build views, such as in the following example.




        
3条回答
  •  無奈伤痛
    2020-12-09 12:48

    I'm using Android Studio so I'm not sure this answer will apply to your case.

    I think you could override onDraw method in the custom view, like this exemple keeping the aspect ratio of an inner image:

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // TODO: consider storing these as member variables to reduce
        // allocations per draw cycle.
        int paddingLeft = getPaddingLeft();
        int paddingTop = getPaddingTop();
        int paddingRight = getPaddingRight();
        int paddingBottom = getPaddingBottom();
    
        int w = getWidth() - paddingLeft - paddingRight;
        int h = getHeight() - paddingTop - paddingBottom;
    
        w = w

    This method runs both in the emulator and the designer.

    It runs as well for any event that redraws the view (onSizeChanged, onLayout, etc...)

提交回复
热议问题