Android 绘制前测量RelativeView的所有子视图

Android 绘制前测量RelativeView的所有子视图,android,android-layout,bitmap,android-relativelayout,measure,Android,Android Layout,Bitmap,Android Relativelayout,Measure,我有一个方法,它应该从RelativeLayout创建一个位图。My RelativeLayout动态创建并将所有输入视图放置到圆中。看起来是这样的: public class CircleView extends RelativeLayout { static final int centerId = 111; private final int radius; Bitmap returnedBitmap; private RelativeLayout.LayoutParams createN

我有一个方法,它应该从RelativeLayout创建一个位图。My RelativeLayout动态创建并将所有输入视图放置到圆中。看起来是这样的:

public class CircleView extends RelativeLayout {
static final int centerId = 111;
private final int radius;
Bitmap returnedBitmap;
private RelativeLayout.LayoutParams createNewRelativeLayoutParams() {
          RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                             RelativeLayout.LayoutParams.WRAP_CONTENT,
                             RelativeLayout.LayoutParams.WRAP_CONTENT);
          lp.addRule(RelativeLayout.ABOVE, centerId);
          lp.addRule(RIGHT_OF, centerId);
          return lp;
}

private View prepareElementForCircle(View elem, int distX, int distY) {
          RelativeLayout.LayoutParams lp = createNewRelativeLayoutParams();

          elem.measure(0, 0);
          int deltaX = elem.getMeasuredWidth() / 2;
          int deltaY = elem.getMeasuredHeight() / 2;
          lp.setMargins(distX - deltaX, 0, 0, radius - distY - deltaY);
          elem.setLayoutParams(lp);
          return elem;
}

public CircleView(Context context, int radius, View[] elements) {
          super(context);
          this.radius = radius;

          RelativeLayout.LayoutParams lpView = new RelativeLayout.LayoutParams(
                             RelativeLayout.LayoutParams.FILL_PARENT,
                             RelativeLayout.LayoutParams.FILL_PARENT);
          this.setLayoutParams(lpView);

          View center = new View(context);
          center.setId(centerId);
          RelativeLayout.LayoutParams lpcenter = new RelativeLayout.LayoutParams(
                             0, 0);
          lpcenter.addRule(CENTER_HORIZONTAL);
          lpcenter.addRule(CENTER_VERTICAL);
          center.setLayoutParams(lpcenter);
          this.addView(center);

          this.addView(prepareElementForCircle(elements[0], 0, 0));
          if (elements.length % 2 == 0) {
                   this.addView(prepareElementForCircle(elements[elements.length / 2],
                                      0, 2 * radius));
          }
          if (elements.length > 2) {
                   for (int i = 1; i <= (elements.length - 1) / 2; i++) {
                             int y = i * 4 * radius / elements.length;
                             int x = (int) Math.sqrt(Math.pow(radius, 2)
                                               - Math.pow((radius - y), 2));
                             this.addView(prepareElementForCircle(elements[i], x, y));
                             this.addView(prepareElementForCircle(elements[elements.length
                                               - i], -x, y));
                   }
          }
}
结果我有了自己的观点,但看起来很丑陋。因此,并非每个视图都显示,并且它们不在圆圈中。所以,我的猜测是,什么是对视图的度量,而不是它应该是什么。也许,我应该测量一下所有的孩子?或者在
onPreDraw()中调用位图转换方法?这两种解决方案我都试过了,但都不适合我

我通过

 public  Bitmap getBitmapFromView() {
        DisplayMetrics dm = context.getApplicationContext().getResources().getDisplayMetrics(); 
        this.measure(MeasureSpec.makeMeasureSpec(dm.widthPixels, MeasureSpec.EXACTLY),
                MeasureSpec.makeMeasureSpec(dm.heightPixels, MeasureSpec.EXACTLY));
        this.layout(0, 0, this.getMeasuredWidth(), this.getMeasuredHeight());
        this.setGravity(Gravity.CENTER);
        Bitmap returnedBitmap = Bitmap.createBitmap(this.getMeasuredWidth(),
                this.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(returnedBitmap);
        this.draw(c);
        return returnedBitmap;
    } 
所以
DisplayMetrics
解决了我的问题。

我通过

 public  Bitmap getBitmapFromView() {
        DisplayMetrics dm = context.getApplicationContext().getResources().getDisplayMetrics(); 
        this.measure(MeasureSpec.makeMeasureSpec(dm.widthPixels, MeasureSpec.EXACTLY),
                MeasureSpec.makeMeasureSpec(dm.heightPixels, MeasureSpec.EXACTLY));
        this.layout(0, 0, this.getMeasuredWidth(), this.getMeasuredHeight());
        this.setGravity(Gravity.CENTER);
        Bitmap returnedBitmap = Bitmap.createBitmap(this.getMeasuredWidth(),
                this.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(returnedBitmap);
        this.draw(c);
        return returnedBitmap;
    } 
所以
DisplayMetrics
解决了我的问题