public class CustomView extends View { private Bitmap mBitmap; @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); // Resize the bitmap to the new size mBitmap = Bitmap.createScaledBitmap(mBitmap, w, h, true); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); // Draw the bitmap to the canvas canvas.drawBitmap(mBitmap, 0, 0, null); } }
public class CustomView extends ViewGroup { ... @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); // Iterate through all subviews and update their layout for (int i = 0; i < getChildCount(); i++) { View child = getChildAt(i); LayoutParams params = child.getLayoutParams(); params.width = w / 2; // Set width to half of the new width params.height = h / 2; // Set height to half of the new height child.setLayoutParams(params); } } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { // Layout the subviews as usual ... } }Both of these examples use the java android.view.View package library.