ImageView be a square with dynamic width?
The best option is to subclass ImageView
yourself, overriding the measure pass:
public class SquareImageView extends ImageView {
...
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = getMeasuredWidth();
setMeasuredDimension(width, width);
}
...
}
Square ImageView
Set the Image view height at run time but first get the display width with this code
Display display = getWindowManager().getDefaultDisplay();
int swidth = display.getWidth();
and now set the height of image view like this
LayoutParams params = eventImage.getLayoutParams();
params.width = LayoutParams.FILL_PARENT;
params.height = swidth ;
eventImage.setLayoutParams(params);
How do I make android imageview square?
The best way for square image is use ConstraintLayout with constraintDimensionRatio Without give fix height/width.
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/image"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="H,1:1"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
I want to have an imageView be a perfect square regardless of the height
You can do something like this:
public class FixedAspectRatioFrameLayout extends FrameLayout {
private float ratio;
public FixedAspectRatioFrameLayout(@NonNull Context context) {
super(context);
}
public FixedAspectRatioFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
private void init(Context context, AttributeSet attributeSet) {
fillFromAttrs(context, attributeSet);
}
private void fillFromAttrs(Context context, AttributeSet attributeSet) {
TypedArray array = context.obtainStyledAttributes(attributeSet, R.styleable.FixedAspectRatioFrameLayout);
ratio = array.getFloat(R.styleable.FixedAspectRatioFrameLayout_ratio, 0);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int originalWidth = MeasureSpec.getSize(widthMeasureSpec);
int originalHeight = MeasureSpec.getSize(heightMeasureSpec);
int finalWidth = originalWidth;
int finalHeight = originalHeight;
if (ratio != 0) {
if (originalHeight == 0) {
finalHeight = (int) (originalWidth / ratio);
} else if (originalWidth == 0) {
finalWidth = (int) (originalHeight * ratio);
}
}
super.onMeasure(
MeasureSpec.makeMeasureSpec(finalWidth, MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(finalHeight, MeasureSpec.EXACTLY)
);
}
}
You also need to specify attribute "ratio" in your res/values/attrs.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="FixedAspectRatioFrameLayout">
<attr name="ratio" format="float"/>
</declare-styleable>
</resources>
So now you can specify, for example, height of this FrameLayout as you want, set width to 0dp and ratio to 1 and put your ImageView inside this FrameLayout
Also, in ConstraintLayout if you set height to match constraints, you can specify ratio for this view:
How to set an imageView to square view in android?
You need to use custom imageview for this, take a look at this:
public class SquareImageView extends ImageView {
public SquareImageView(Context context) {
super(context);
}
public SquareImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SquareImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth()); // Snap to width
}
}
Use it instead of ImageView in your layout file.
Hope this helped!
Android change imageview size dynamically by image size
set width of imageView to match_parent in the xml and calculate height of the image at runtime to maintain the aspect ratio using below code:
Display display = getActivity().getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int newWidth = size.x;
//Get actual width and height of image
int width = bitmap.getWidth();
int height = bitmap.getHeight();
// Calculate the ratio between height and width of Original Image
float ratio = (float) height / (float) width;
float scale = getApplicationContext().getResources().getDisplayMetrics().density;
int newHeight = (int) (width * ratio)/scale;
Pass this newHeight and newWidth to resize your image by calling .resize function of your image processing library.
Related Topics
How to Retrieve the Android Sdk Version
Extending Application to Share Variables Globally
Bitmap Recycle with Largeheap Enabled
Updating Progress Dialog in Activity from Asynctask
How to Make an Android App to Always Run in Background
Best Way to Have Paid and Free Version of an Android App
How to Size an Android View Based on Its Parent's Dimensions
Android: How to Convert String to Date
Scale Image to Fill Imageview Width and Keep Aspect Ratio
How to Create a Http Server in Android
Android Failed to Load Js Bundle
Android: How to Create a Dialog Without a Title
Recyclerview - How to Smooth Scroll to Top of Item on a Certain Position
Android Fragment Lifecycle Over Orientation Changes
How to Use Icons and Symbols from "Font Awesome" on Native Android Application