I define a view. This view contains a picture and the user can interact with. I define a FrameLayout in my main layout and i add this view programmatically in this FrameLayout.
In the View constructor, i define a MarginLayutParams to put this view on the center of te screen. The code is :
mLayoutParams = new MarginLayoutParams(picture.getWidth(), picture.getHeight);
mLayoutParams.setMargins(toCenterX, toCenterY, 0, 0);
setLayoutParams(mLayoutParams);
The margin values don’t works… The view is correctly resize to width and height defines but the view isn’t scale to the center of the view by the margin values…
Edit :
Schemas of my views :
-> View1 (i want sclae this view to the center)
Activity -> FrameLayout -> View2 (i want scale this view under the view 1)
-> View3 (i want scale this view at the top right)
Here a sample of my ViewGroup. I implement a ViewGroup to manage all views :
public class MainView extends ViewGroup {
public BackgroundView mBackgroundWheelArea;
public BackgroundView mBackgroundLabelArea;
public WheelCoreView wheelCoreArea;
public LabelView labelArea;
public WheelOfFortuneActivity mActivity;
public MainView(Context pContext) {
super(pContext);
mActivity = (WheelOfFortuneActivity) pContext;
mBackgroundWheelArea = new BackgroundView(pContext, R.drawable.menu_background);
wheelCoreArea = new WheelCoreView(pContext, mActivity.fromPixelToDp(mBackgroundWheelArea
.getBackgroundHeight()), mActivity.fromPixelToDp(mBackgroundWheelArea
.getBackgroundWidth()));
labelArea = new LabelView(pContext, "Web");
mBackgroundLabelArea = new BackgroundView(pContext, R.drawable.menu_background_label);
this.addView(wheelCoreArea, 0);
}
@Override
protected void onLayout(boolean pChanged, int pL, int pT, int pR, int pB) {
final int count = getChildCount();
for (int i = 0; i < count; i++) {
View child = getChildAt(i);
child.layout(100, 100, 0, 0);
}
}
@Override
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
child.draw(canvas);
return true;
}
}
But the wheelCoreArea view no move from the top left corner ! 🙁
Can you help me ?
Regards.
You should override
onLayout()method and set the view bounds manually instead of setting the margins.Call
view.layout(top, left, right, bottom);Maintain the bounds in member variables.If the size of this view also changes you may need to override the
onMeasuremethod too.Edit
Try this formula to calculate the view placement points.
Call
view.layout(viewLeft , viewTop , viewLeft + viewWidth, viewTop + viewHeight);