Created
June 20, 2013 20:46
-
-
Save teppeihomma/5826494 to your computer and use it in GitHub Desktop.
横幅を最大に広げた状態で縦を調整するImageView。Androidのlayout_weight="1"のエリアの下に配置するフッター画像などで使用します。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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