Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class DynamicHeightImageView extends ImageView {
- private double mHeightRatio;
- public DynamicHeightImageView(Context context, AttributeSet attrs) {
- super(context, attrs);
- }
- public DynamicHeightImageView(Context context) {
- super(context);
- }
- public void setHeightRatio(double ratio) {
- if (ratio != mHeightRatio) {
- mHeightRatio = ratio;
- requestLayout();
- }
- }
- public double getHeightRatio() {
- return mHeightRatio;
- }
- @Override
- protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
- if (mHeightRatio > 0.0) {
- int width = MeasureSpec.getSize(widthMeasureSpec);
- int height = (int) (width * mHeightRatio);
- setMeasuredDimension(width, height);
- }
- else {
- super.onMeasure(widthMeasureSpec, heightMeasureSpec);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement