Last active
January 4, 2016 18:39
-
-
Save niusounds/8662056 to your computer and use it in GitHub Desktop.
読み込む画像の大きさに合わせてImageViewの大きさを拡大する方法。バナーを画面幅いっぱいに拡大して、その高さに合わせてImageViewの高さを調整したい場合に使えます。(Android 4.4からは scaleType="centerCrop" adjustViewBounds="true" とするだけで良いのですが)
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
ImageView view = new ImageView(context) { | |
final RectF r = new RectF(); | |
@Override | |
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | |
Drawable d = getDrawable(); | |
if (d != null) { | |
r.set(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight()); | |
getImageMatrix().mapRect(r); | |
setMeasuredDimension((int) r.width(), (int) r.height()); | |
} else { | |
super.onMeasure(widthMeasureSpec, heightMeasureSpec); | |
} | |
} | |
}; | |
view.setScaleType(ScaleType.CENTER_CROP); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment