开发者

position of custom Views within a RelativeLayout

开发者 https://www.devze.com 2023-02-04 00:36 出处:网络
I have a custom View: ImageZoomView.java which has a setImage, and overrides onL开发者_StackOverflow社区ayout and onDraw, as following:

I have a custom View: ImageZoomView.java which has a setImage, and overrides onL开发者_StackOverflow社区ayout and onDraw, as following:

public void setImage(Bitmap bitmap) {
    mBitmap = bitmap; ...
}


@Override
protected void onDraw(Canvas canvas) {
    if (mBitmap != null && mState != null) {
        ...
        canvas.drawBitmap(mBitmap, mRectSrc, mRectDst, mPaint);
    }
}

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    super.onLayout(changed, left, top, right, bottom);...
}

And i want to position several ImageZoomViews within a RelativeLayout, using layoutParams rules, but all my views are stuck to position (0,0), I don't know why.

       ImageZoomView mZoomView = new ImageZoomView(getApplicationContext());

 mZoomView.setImage(mBitmap);
 mZoomView.setId(index+1);

 RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.RelativeLayout01);
 RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(screenWidth/2, screenHeight/2);
 if(index > 0)
  lp.addRule(RelativeLayout.RIGHT_OF, index);

 relativeLayout.addView(mZoomView, lp);

I've tried the exact same code with normal ImageViews, it works and every view is positionned on the right of the (n-1) view...

Any ideas please? Thanks.


I suspect your variable index might be staying at value 0 - make sure it's being incremented.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号