Reputation: 41
I wanna to create an app that view an image/poster, I want the imageView in square. Before this, i try to get the screen width and assign it to image height and width, something like this
Bitmap newicon = Bitmap.CreateBitmap (int x, int y, int width, int height, matrix, false/true);
But there is an error occur "x + width <= bitmap.width()". This is the example that i create so far, but i set the height to 400dp. I want the height and width is flexible depend on phone size. Any ideas?
<ImageView
android:src="@android:drawable/ic_menu_gallery"
android:layout_width="match_parent"
android:layout_height="400dp"
android:adjustViewBounds="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:scaleType="centerCrop"
android:id="@+id/ivPoster" />
Upvotes: 1
Views: 3898
Reputation: 2423
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!
Upvotes: 3