How To Delete Additional Space Imageview In Android
I want use ImageView in my project, but it shows additional space in the layout. How can I remove the additional space? This image shows the problem: XML code:
Solution 1:
Try this custom ImageView class
publicclassResizableImageViewextendsImageView {
publicResizableImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@OverrideprotectedvoidonMeasure(int widthMeasureSpec, int heightMeasureSpec){
Drawabled= getDrawable();
if(d!=null){
// ceil not round - avoid thin vertical gaps along the left/right edgesintwidth= View.MeasureSpec.getSize(widthMeasureSpec);
intheight= (int) Math.ceil((float) width * (float) d.getIntrinsicHeight() / (float) d.getIntrinsicWidth());
setMeasuredDimension(width, height);
}else{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
}
And use this class as below
<your_package.ResizableImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentTop="true"android:src="@drawable/cover_menu_bg" />
Solution 2:
Use the imageview like these
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="@drawable/icon"
android:id="@+id/imv"
android:scaleType="fitCenter" />
Solution 3:
For anyone looking for solution for both orientations. Here is my approach with samples:
First, my xml:
<?xml version="1.0" encoding="utf-8"?><com.example.michal.fillimageview.CustomLinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"android:background="#f0f000"android:orientation="vertical"><com.example.michal.fillimageview.ResizableImageViewandroid:layout_width="wrap_content"android:layout_height="0dp"android:layout_weight="1"android:adjustViewBounds="true"android:src="@drawable/p600_900" /><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:background="@android:color/background_dark"android:text="Hello World!"android:textColor="@android:color/holo_red_dark"android:textSize="30sp" /><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:background="@android:color/background_dark"android:text="Hello World!"android:textColor="@android:color/holo_red_dark"android:textSize="30sp" /></com.example.michal.fillimageview.CustomLinearLayout>
Class:
publicclassResizableImageViewextendsImageView {
publicResizableImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@OverrideprotectedvoidonMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Drawabled= getDrawable();
if (d != null) {
// ceil not round - avoid thin vertical gaps along the left/right edgesintwidth= View.MeasureSpec.getSize(widthMeasureSpec);
intheight= View.MeasureSpec.getSize(heightMeasureSpec);
if (height > width) {//portraitif (d.getIntrinsicWidth() > d.getIntrinsicHeight()) {
height = (int) Math.ceil((float) width * (float) d.getIntrinsicHeight() / (float) d.getIntrinsicWidth());
setMeasuredDimension(width, height);
} else {
width = (int) Math.ceil((float) height * (float) d.getIntrinsicWidth() / (float) d.getIntrinsicHeight());
setMeasuredDimension(width, height);
}
} else {//landscape
width = (int) Math.ceil((float) height * (float) d.getIntrinsicWidth() / (float) d.getIntrinsicHeight());
setMeasuredDimension(width, height);
}
} else {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
}
Samples for specific images:
- 600x900 portrait
- 600x900 landscape
- 800x600 portrait
- 800x600 landscape
Post a Comment for "How To Delete Additional Space Imageview In Android"