Skip to content

Commit

Permalink
Implement: ViewGroup.addView(child, params)
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielBRDeveloper committed Jul 5, 2024
1 parent df1a71c commit b2504a7
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 103 deletions.
93 changes: 44 additions & 49 deletions res/ex-layout/main.xml
Original file line number Diff line number Diff line change
@@ -1,65 +1,60 @@
<?xml version="1.0" encoding="UTF-8" ?>
<FrameLayout
<LinearLayout
width="match_parent"
height="match_parent"
background="?colorSurface"
padding="20dp"
orientation="horizontal">
<CardLayout
height="wrap_content"
paddingBottom="8dp"
background="#AAA">

<LinearLayout
width="match_parent"
height="match_parent"
radius="20dp">
height="wrap_content"
orientation="horizontal"
padding="10dp"
background="#555"
gravity="center">

<ImageView
id="icon"
width="64dp"
height="64dp"
padding="10dp"
background="#F0F"/>

<LinearLayout
width="match_parent"
height="match_parent"
id="content">
height="wrap_content"
paddingLeft="20dp"
background="#F00">

<TextView
width="wrap_content"
tag="title"
width="match_parent"
height="wrap_content"
textSize="14sp"
text="Hello world"/>

<Button
width="300dp"
height="64dp"/>
textSize="19dp"
text="Title"/>

<TextView
width="wrap_content"
tag="subtitle"
width="match_parent"
height="wrap_content"
textSize="14sp"
text="That is a example of draw with card layout"/>

<Button
width="300dp"
height="64dp"
visibility="gone"/>
textSize="14dp"
textStyle="bold"
textColor="?colorPrimary"
paddingTop="4dp"
paddingBottom="3dp"
text="Title"/>

<TextView
id="animate"
width="wrap_content"
tag="desc"
width="match_parent"
height="wrap_content"
textSize="14sp"
background="#CC0000"
text="That allow create UIs a like Material3"/>

<Button
width="300dp"
height="64dp"/>
textSize="13dp"
alpha="0.5"
paddingTop="4dp"
paddingBottom="3dp"
text="Title"/>
</LinearLayout>

<EditText
width="128dp"
height="128dp"/>
</LinearLayout>

<EditText
width="128dp"
height="128dp"/>
<EditText
width="128dp"
height="128dp"/>
<EditText
width="128dp"
height="128dp"/>
</LinearLayout>
</CardLayout>
</FrameLayout>
</LinearLayout>
26 changes: 8 additions & 18 deletions src/br/nullexcept/mux/test/Example.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import br.nullexcept.mux.view.View;

public class Example extends Activity {
private static int layer = 0;
public static void main(String[] args) {
Application.initialize(Project.build(new Launch<>(Example.class), "br.nullexcept.test-master-ui"));
}
Expand All @@ -19,27 +20,16 @@ public static void main(String[] args) {
@Override
public void onCreate() {
super.onCreate();
setContentView("@ex-layout/main");
animate = findViewById("animate");
update();
}

void update() {
animate.getTransition().getTranslate().set((clamp(x, 100))-50,0);
animate.getTransition().getScale().set(clamp(z, 200)/100.0f, 1.0f);
animate.getTransition().setRotation(clamp(z+126, 360));
x++;
z++;
animate.invalidate();
postDelayed(this::update, 15);
if (layer == 0) {
postDelayed(() -> {
// startActivity(new Launch<>(Example.class).addFlags(FLAG_ACTIVITY_NEW_WINDOW));
}, 1);
}
layer++;
setContentView("@ex-layout/main");
}

public int clamp(int value, int max) {
int rest = value % max;
int div = value / max;

return div % 2 == 0 ? rest : (max - rest);
}

@Override
public void onDestroy() {
Expand Down
9 changes: 9 additions & 0 deletions src/br/nullexcept/mux/view/View.java
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,15 @@ public Object getTag() {
return tag;
}

@Override
public String toString() {
return getClass().getSimpleName()+" { " +
"id=" + id +";"+
"tag=" + tag +";" +
"attached="+ (getViewRoot() != null) +";"+
"}";
}

public interface OnClickListener {
void onClick(View view);
}
Expand Down
7 changes: 6 additions & 1 deletion src/br/nullexcept/mux/view/ViewGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,8 @@ public <T extends View> T getChildAt(int index){
return (T) children.get(index);
}

public void addChild(View view){
public void addChild(View view, LayoutParams params) {
view.setLayoutParams(params == null ? new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT) : params);
children.add(view);
view.setParent(this);
onChildAdded(view);
Expand All @@ -252,6 +253,10 @@ public void addChild(View view){
notifyTreeChanged();
}

public void addChild(View view){
addChild(view, view.getLayoutParams());
}

protected void requestFocus(View focused){
if (getParent() != null){
getParent().requestFocus(focused);
Expand Down
13 changes: 8 additions & 5 deletions src/br/nullexcept/mux/widget/AbsoluteLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ public AbsoluteLayout(Context context, AttributeList attrs) {
}

@Override
public void addChild(View view) {
if (!(view.getLayoutParams() instanceof LayoutParams)){
view.setLayoutParams(new LayoutParams(view.getLayoutParams().width, view.getLayoutParams().height));
public void addChild(View view, ViewGroup.LayoutParams params) {
if (params == null) {
params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
}
super.addChild(view);
if (!(params instanceof LayoutParams)) {
params = new LayoutParams(params.width, params.height);
}
super.addChild(view, params);
}

@Override
protected Point getChildLocation(View view) {
LayoutParams params = (LayoutParams) view.getLayoutParams();
Expand Down
13 changes: 8 additions & 5 deletions src/br/nullexcept/mux/widget/FrameLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,14 @@ protected Point getChildLocation(View view) {
}

@Override
public void addChild(View view) {
LayoutParams params = new LayoutParams(0,0);
params.from(view.getLayoutParams());
view.setLayoutParams(params);
super.addChild(view);
public void addChild(View view, ViewGroup.LayoutParams params) {
if (params == null) {
params = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
}
LayoutParams newParams = new LayoutParams(0,0);
newParams.from(params);

super.addChild(view, newParams);
}

public static class LayoutParams extends MarginLayoutParams {
Expand Down
125 changes: 100 additions & 25 deletions src/br/nullexcept/mux/widget/LinearLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import br.nullexcept.mux.res.AttributeList;
import br.nullexcept.mux.view.*;

import java.util.List;

public class LinearLayout extends ViewGroup {
public static int ORIENTATION_HORIZONTAL = 1;
public static int ORIENTATION_VERTICAL = 0;
Expand All @@ -25,54 +27,127 @@ public LinearLayout(Context context, AttributeList attrs) {
attrs = initialAttributes();
attrs.searchDimension(AttrList.dividerSize, value -> dividerSize = value.intValue());
attrs.searchRaw(AttrList.orientation, value -> {
if ("horizontal".equals(value)){
if ("horizontal".equals((value+"").toLowerCase())){
setOrientation(ORIENTATION_HORIZONTAL);
} else {
setOrientation(ORIENTATION_VERTICAL);
}
});
}


@Override
public void addChild(View view) {
LayoutParams params = new LayoutParams(0,0);
params.from(view.getLayoutParams());
view.setLayoutParams(params);
super.addChild(view);
public void addChild(View view, ViewGroup.LayoutParams params) {
if (params == null) {
params = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
}

LayoutParams newParams = new LayoutParams(0,0);
newParams.from(params);
view.setLayoutParams(newParams);

super.addChild(view, newParams);
}

@Override
protected Point getChildLocation(View view) {
Point point = new Point(getPaddingLeft(), getPaddingTop());
int mh = getMeasuredHeight() - getPaddingTop() - getPaddingBottom();
int mw = getMeasuredWidth() - getPaddingRight() - getPaddingLeft();
int index = getChildIndex(view);
Gravity.applyGravity(getGravity(),view.getMeasuredWidth(),view.getMeasuredHeight(),mw,mh,rect);
return orientation == ORIENTATION_VERTICAL ? getChildLocationVertical(view) : getChildLocationHorizontal(view);
}

for (int i = 0; i < index; i++){
private Size measureHorizontal() {
Size size = new Size();
for (int i = 0; i < getChildrenCount(); i++) {
View child = getChildAt(i);
if (child.getVisibility() == VISIBILITY_GONE) continue;
LayoutParams params = (LayoutParams) child.getLayoutParams();
if (orientation == ORIENTATION_HORIZONTAL){
point.x += child.getMeasuredWidth() + params.getMarginLeft() + params.getMarginRight() + dividerSize;
} else {
point.y += child.getMeasuredHeight() + params.getMarginTop() + params.getMarginBottom() + dividerSize;
}
size.width += child.getMeasuredWidth() + params.getMarginLeft() + params.getMarginRight();
size.height = Math.max(child.getMeasuredHeight() + params.getMarginTop() + params.getMarginBottom(), size.height);
if (i != getChildrenCount()) size.width += dividerSize;
}
return size;
}

private Size measureVertical() {
Size size = new Size();
for (int i = 0; i < getChildrenCount(); i++) {
View child = getChildAt(i);
if (child.getVisibility() == VISIBILITY_GONE) continue;
LayoutParams params = (LayoutParams) child.getLayoutParams();
size.width = Math.max(size.width, child.getMeasuredWidth() + params.getMarginLeft() + params.getMarginRight());
size.height += child.getMeasuredHeight() + params.getMarginTop() + params.getMarginBottom();
if (i != getChildrenCount()) size.height += dividerSize;
}
return size;
}

@Override
protected Size onMeasureContent(int parentWidth, int parentHeight) {
return super.onMeasureContent(parentWidth, parentHeight);
}

private Point getChildLocationVertical(View view) {
if (view.getVisibility() == VISIBILITY_GONE)
return new Point(0,0);
Size wrapSize = measureVertical();
LayoutParams params = (LayoutParams) view.getLayoutParams();

point.x += params.getMarginLeft() - params.getMarginRight();
point.y += params.getMarginTop() - params.getMarginBottom();
int w = Math.max(0, getMeasuredWidth() - getPaddingLeft() - getPaddingRight() - params.getMarginRight());
int h = Math.max(0, getMeasuredHeight() - getPaddingTop() - getPaddingBottom() - params.getMarginBottom());

if (orientation == ORIENTATION_HORIZONTAL) {
point.y += rect.top;
} else {
point.x += rect.left;
int posX = Gravity.apply(Gravity.horizontal(getGravity()),w,wrapSize.width);
int posY = Gravity.apply(Gravity.vertical(getGravity()),h, wrapSize.height);

posX = Math.max(0, posX);
posY = Math.max(0, posY);

Point pos = new Point(0, posY);
for (int i = 0; i < getChildrenCount(); i++) {
View child = getChildAt(i);
if (child.equals(view)) break;
if (child.getVisibility() == VISIBILITY_GONE) continue;
LayoutParams childParams = (LayoutParams) child.getLayoutParams();
pos.y += childParams.getMarginTop() + childParams.getMarginBottom() + child.getMeasuredHeight();
}

pos.x = posX + Gravity.apply(Gravity.horizontal(getGravity()),wrapSize.width,view.getMeasuredWidth());
pos.x += params.getMarginLeft();

pos.y += getPaddingTop();
pos.x += getPaddingLeft();

return pos;
}

private Point getChildLocationHorizontal(View view) {
if (view.getVisibility() == VISIBILITY_GONE)
return new Point(0,0);
Size wrapSize = measureHorizontal();

LayoutParams params = (LayoutParams) view.getLayoutParams();

int w = Math.max(0, getMeasuredWidth() - getPaddingLeft() - getPaddingRight() - params.getMarginRight());
int h = Math.max(0, getMeasuredHeight() - getPaddingBottom() - getPaddingTop() - params.getMarginBottom());

int posX = Gravity.apply(Gravity.horizontal(getGravity()),w,wrapSize.width);
int posY = Gravity.apply(Gravity.vertical(getGravity()),h, wrapSize.height);

posX = Math.max(0, posX);
posY = Math.max(0, posY);

Point pos = new Point(posX, 0);
for (int i = 0; i < getChildrenCount(); i++) {
View child = getChildAt(i);
if (child.equals(view)) break;
if (child.getVisibility() == VISIBILITY_GONE) continue;
LayoutParams childParams = (LayoutParams) child.getLayoutParams();
pos.x += dividerSize;
pos.x += childParams.getMarginLeft() + childParams.getMarginRight() + child.getMeasuredWidth();
}

pos.y = posY + Gravity.apply(Gravity.vertical(getGravity()),wrapSize.height,view.getMeasuredHeight());
pos.y += params.getMarginTop() + getPaddingTop();
pos.x += getPaddingLeft();

return point;
return pos;
}

public void setOrientation(int orientation) {
Expand Down

0 comments on commit b2504a7

Please sign in to comment.