Skip to content

Instantly share code, notes, and snippets.

@teppeihomma
Created June 20, 2013 20:46
Show Gist options
  • Save teppeihomma/5826494 to your computer and use it in GitHub Desktop.
Save teppeihomma/5826494 to your computer and use it in GitHub Desktop.
横幅を最大に広げた状態で縦を調整するImageView。Androidのlayout_weight="1"のエリアの下に配置するフッター画像などで使用します。
<package_name.widget.WidthAdjustImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentDescription="@null"
android:scaleType="fitCenter"
android:src="@drawable/foot_banner" />
import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;
public class WidthAdjustImageView extends ImageView {
public WidthAdjustImageView(Context context) {
this(context, null);
}
public WidthAdjustImageView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public WidthAdjustImageView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Drawable drawable = this.getDrawable();
Rect rect = drawable.getBounds();
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = (int) (rect.height() * ((float) width / (float) rect
.width()));
setMeasuredDimension(width, height);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment