Skip to content

Instantly share code, notes, and snippets.

@rongi
Created April 2, 2014 10:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rongi/9931789 to your computer and use it in GitHub Desktop.
Save rongi/9931789 to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:id="@+id/big_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ImageView
android:id="@+id/small_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</merge>
package my.package;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
public class ExampleLayout extends ViewGroup {
public ExampleLayout(Context context, AttributeSet attrs) {
super(context, attrs);
inflate(context, R.layout.example_layout, this);
bigImageView = (ImageView) findViewById(R.id.big_image);
smallImageView = (ImageView) findViewById(R.id.small_image);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
final int width = r - l;
final int height = b - t;
final int bigImageViewHeight = Math.round(0.4f * height);
layoutChild(bigImageView,
Math.round(0.1f * width),
0,
Math.round(0.8f * width),
bigImageViewHeight);
final int bigImageViewCenterVertical = bigImageViewHeight / 2;
final int smallImageViewHeight = smallImageView.getMeasuredHeight(); // for example
layoutChild(smallImageView,
Math.round(0.3f * width),
bigImageViewCenterVertical - smallImageViewHeight / 2,
Math.round(0.4f * width),
smallImageViewHeight);
// etc
}
private static void layoutChild(View view, int left, int top, int width, int height) {
view.layout(left, top, left + width, top + height);
}
private final ImageView bigImageView;
private final ImageView smallImageView;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment