Display comic strip in Imageview - android

i use Universal image loader to load some imageview(large height) to listview.
this is listview(custom for zoom in/out) in activity:
<noname.test1.CustomListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listviewImg" />
this is list_img.xml for listview:
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/imageview1">
</ImageView>
this is image i want to load (size 400px X 5312px):
link
this is result:
I want image when show is full screen or larger.
How to fix it?

create this class and use this instead of imageView
/**
* Created by farazkhonsari on 1/26/16.
*/
public class ScaledImageView extends ImageView {
public ScaledImageView(Context context) {
super(context);
}
public ScaledImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ScaledImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
try {
Drawable drawable = getDrawable();
if (drawable == null) {
setMeasuredDimension(0, 0);
} else {
int measuredWidth = MeasureSpec.getSize(widthMeasureSpec);
int measuredHeight = MeasureSpec.getSize(heightMeasureSpec);
int width = measuredWidth;
int height = width * drawable.getIntrinsicHeight() / drawable.getIntrinsicWidth();
setMeasuredDimension(width, height);
}
} catch (Exception e) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
and use this class in layout like this:
<ir.farazGroup.YeJoke.tools.ScaledImageView
android:id="#+id/image"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="#drawable/c"
android:scaleType="fitCenter"
/>

you should change scaleType of imageViwe and set adjustViewBound true:
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/imageview1"
android:scaleType="centerCrop"
android:adjustViewBounds="true">
</ImageView>
you can try other option for scaleType and see which one is good for you!
and you can read about scaleType here:
scaleTypes

Try this solution: [Set this inside onCreate or onCreateView after inflating the list]
imageView = (ImageView) findViewById(R.id. imageview1);
Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics metrics = new DisplayMetrics();
display.getMetrics(metrics);
float screenWidth = metrics.widthPixels;
imageView.getLayoutParams().width = (int) screenWidth; // Takes the whole screen width

try this i'll hope i will help you
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/imageView"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:src="#drawable/locked_image"
android:scaleType="fitXY"
/>

Related

How to set ImageView height and width before image is loaded with Picasso inside Recycerview StaggeredGridLayout

I am looking to set the ImageView height before loading the image with Picasso. Trying to prevent the parent view from changing its height to the image. I am getting a height and width of the image and setting it inside of of Picasso and then scaling it down.
How do I scale the ImageView based on the "webformatHeight" and "webformatWidth" before loading the image with Picasso to prevent resizing.
Data
{
...
"hits":[
{
"webformatHeight":426,
"webformatWidth":640,
"webformatURL":"https://pixabay.com/get/eb32b5062dfd013ed95c4518b74a4291ea77ead204b0144190f8c478a2ebb3_640.jpg",
...
},
...
]
}
Recycler Implementation
RecyclerView recycler = (RecyclerView) findViewById(R.id.recycler);
mPixabayAdapter = new PixabayAdapter();
recycler.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
recycler.setAdapter(mPixabayAdapter);
ViewHolder
class ViewHolder extends RecyclerView.ViewHolder {
private ImageView mImage;
private TextView mTitle;
private ViewHolder(View view) {
super(view);
mImage = view.findViewById(R.id.image);
mTitle = view.findViewById(R.id.title);
}
void bindView() {
ColorDrawable[] shotLoadingPlaceholders = new ColorDrawable[]{new ColorDrawable(ContextCompat.getColor(itemView.getContext(), R.color.background_light))};
Hit hit = mHitList.get(getAdapterPosition());
mTitle.setText(hit.getUser());
Picasso
.with(mImage.getContext())
.load(hit.getWebformatURL())
.placeholder(shotLoadingPlaceholders[0])
.resize(hit.getImageWidth(), hit.getImageHeight())
.priority(Picasso.Priority.HIGH)
.onlyScaleDown()
.into(mImage);
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="4dp">
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true" />
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_below="#+id/image"
android:background="#color/background_light"
android:fontFamily="sans-serif-medium"
android:gravity="center_vertical"
android:maxLines="1"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:textColor="#color/white_light"
android:textSize="13sp" />
</RelativeLayout>
Calc the aspect ratio of your image by width/height and wrap your ImageView inside something like this:
public class RelativeSizeLayout extends FrameLayout {
private float aspectRatio = 1.77f;
public RelativeSizeLayout(Context context) {
this(context,null);
}
public RelativeSizeLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public RelativeSizeLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
public boolean shouldDelayChildPressedState() {
return false;
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
final int width = MeasureSpec.getSize(widthMeasureSpec);
heightMeasureSpec = MeasureSpec.makeMeasureSpec((int) (width / aspectRatio), MeasureSpec.EXACTLY);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
public void setAspectRatio(float ratio) {
aspectRatio = ratio;
}
}
you can set the layoutParams of the ImageView on the bindView with the width and hight the Hit class like this.
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(hit.getImageWidth(), hit.getImageHeight());
mImage.setLayoutParams(layoutParams);
regards.

create the RelativeLayout with the same height and width when using the weight property for width

I have a RelativeLayout with the weight property for width , and using the 130dp for height , but it seem's like a rectangle and now i want height equal to width (i want to achieve to square style) , like this image :
this is my xml :
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="130dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:background="#00c4ff"
android:onClick="blockClick">
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="130dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:background="#00c4ff"
android:onClick="blockClick">
</RelativeLayout>
</LinearLayout>
Make your own implementation of RelativeLayout
Code sample for aspect ratio ImageView
Code example:
public class MyRelativeLayout extends RelativeLayout{
[...]
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int newWidth = getMeasuredWidth();
int newHeight = newWidth;
setMeasuredDimension(newWidth, newHeight);
}
[...]
}
Use custom RelativeLayout...
public class SquareRelativeLayout extends RelativeLayout {
public SquareRelativeLayout(Context context) {
super(context);
}
public SquareRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SquareRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = getMeasuredWidth();
setMeasuredDimension(width, width);
}
}
Thinking about different screen of device, there is one solution:
Get the width of the screen. W
divide the W: W/2
Set every height of the RelativeLayout with W/2
I have used this solution to set the image View with fix ratio on different screen.
layout.width = getResources().getDisplayMetrics().widthPixels;
layout.height = (int) (layout.width / (2.0));
mRelativeView.setLayoutParams(layout);

Android Invisible CustomView

I've created a custom RelativeLayout that has two ImageView in it, one on top of the other, The first one is supposed to show the user selected image and the second on is shown up after he selected and acts as a delete button (small X icon), the problem that i have is even at the start of the application this custom view is invisible, but i can still click on it. and if i use a background for it i can see it.
Here is my Custom View class and XML
public class ImageViewWithIcon extends RelativeLayout{
public ImageView image, icon;
private final Context context;
public ImageViewWithIcon(final Context context)
{
super(context);
this.context = context;
}
public ImageViewWithIcon(final Context context, final AttributeSet attrs)
{
super(context, attrs);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.image_view_with_icon, this, true);
RelativeLayout rl = (RelativeLayout) getChildAt(0);
icon = (ImageView) findViewById(R.id.ImageViewWithIcon_icon);
image = (ImageView) findViewById(R.id.ImageViewWithIcon_image);
// image.setLayoutParams(new LayoutParams(400, 400));
this.context = context;
}
public ImageViewWithIcon(final Context context, final AttributeSet attrs, final int defStyle)
{
super(context, attrs, defStyle);
this.context = context;
}
#Override
protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec)
{
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.new_message_picture_button);
int width;
if (getLayoutParams().width == android.view.ViewGroup.LayoutParams.WRAP_CONTENT)
width = bmp.getWidth();
else
if (getLayoutParams().width == android.view.ViewGroup.LayoutParams.MATCH_PARENT || getLayoutParams().width == android.view.ViewGroup.LayoutParams.FILL_PARENT)
width = MeasureSpec.getSize(widthMeasureSpec);
else
width = getLayoutParams().width;
int height = 100;
if (getLayoutParams().height == android.view.ViewGroup.LayoutParams.WRAP_CONTENT)
height = bmp.getHeight();
else
if (getLayoutParams().height == android.view.ViewGroup.LayoutParams.MATCH_PARENT || getLayoutParams().height == android.view.ViewGroup.LayoutParams.FILL_PARENT)
height = MeasureSpec.getSize(heightMeasureSpec);
else
height = getLayoutParams().height;
bmp.recycle();
setMeasuredDimension(width | MeasureSpec.EXACTLY, height | MeasureSpec.EXACTLY);
}}
<merge xmlns:android="http://schemas.android.com/apk/res/android" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/ImageViewWithIcon_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:background="#drawable/new_message_picture_button" />
<ImageView
android:id="#+id/ImageViewWithIcon_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#id/ImageViewWithIcon_image"
android:visibility="visible"
android:src="#drawable/new_message_close_picture"/>
</RelativeLayout>
</merge>
The first line in onMessure has bmp with width and height of 234px
even if I create a method like setImage() to call it from the Activity, the image is set but it is still invisible.
Do you mean?
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageViewWithIcon
android:id="#+id/ImageViewWithIcon_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:background="#drawable/new_message_picture_button" />
<ImageViewWithIcon
android:id="#+id/ImageViewWithIcon_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#id/ImageViewWithIcon_image"
android:visibility="visible"
android:src="#drawable/new_message_close_picture"/>

Square cells in GridView

I'm using GridView in my app:
<GridView
android:id="#+id/main_grid_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="4" >
</GridView>
Every cell in this GridView is ImageView:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/img"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:contentDescription="#string/img"
android:scaleType="centerCrop" />
</RelativeLayout>
All cells in my GridView must be square (height must equals width) and image must fit cell. But it always looks like rectangle... How I can to implements square cells in GridView?
Make sure your cell is square.
For this, you can set the width and height of the RelativeLayout programmatically with the value screen_width/4.
This can do the trick
#Override public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
int size = width > height ? height : width;
setMeasuredDimension(size, size);
}
As mentioned in the answer here - Gridview with two columns and auto resized images
You can make a custom ImageView for this
public class SquareImageView extends ImageView {
public SquareImageView(Context context) {
super(context);
}
public SquareImageView(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
}
public SquareImageView(Context context, AttributeSet attributeSet, int defStyle) {
super(context, attributeSet, defStyle);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth());
} else {
setMeasuredDimension(getMeasuredHeight(), getMeasuredHeight());
}
}
}
and then use this in your grid view cell layout as
<package_containing_SquareImageView.SquareImageView
android:layout_width="match_parent"
android:layout_height="match_parent" />

Android ImageView adjusting parent's height and fitting width

Update : I solved this issue by using the method described in this answer
I'm a bit stuck with this issue, which I think should be pretty simple.
So my app downloads an image, and renders the bitmap in an ImageView, a child element of a RelativeLayout.
I would like the ImageView to fit the parent width, and to adapt it's size to keep the aspect ratio.
Here is my XML :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<RelativeLayout android:id="#+id/banner" android:layout_width="fill_parent" android:layout_height="wrap_content"></RelativeLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
/>
</LinearLayout>
And the code :
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
RelativeLayout banner = (RelativeLayout) findViewById(R.id.banner);
ImageView imgV = new ImageView(this);
imgV.setScaleType(ImageView.ScaleType.CENTER_CROP);
// I tried all the scale types : CENTER_INSIDE : same effect, FIT_CENTER : same effect...
imgV.setBackgroundColor(0x00FFFF00);
imgV.setAdjustViewBounds(Color.BLUE);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
banner.addView(imgV,params);
// Some code downloading the image stream
bitmap = BitmapFactory.decodeStream(stream);
imgV.setImageBitmap(bitmap);
}
Desired :
Result :
Thanks to #Julien and #js.
Here is complete solution of ImageView that will stretch bitmap height preserving aspect ratio even if bitmap is smaller than ImageView.
public class ResizableImageView extends ImageView {
public ResizableImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
Drawable d = getDrawable();
if(d!=null){
// ceil not round - avoid thin vertical gaps along the left/right edges
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = (int) Math.ceil((float) width * (float) d.getIntrinsicHeight() / (float) d.getIntrinsicWidth());
setMeasuredDimension(width, height);
}else{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
}
You can use this class in your xml layouts instead ImageView.
<com.example.ResizableImageView
android:id="#+id/banner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/banner" />
You're probably looking for android:adjustViewBounds="true" in xml or imageView.setAdjustViewBounds(true) in Java.
As I mentioned in a comment, I subclassed the ImageView. I found my code, here you go :
protected class ResizableImageView extends ImageView
{
private Bitmap mBitmap;
// Constructor
public ResizableImageView(Context context)
{
super(context);
}
// Overriden methods
#Override
protected void onMeasure(int widthMeasureSpec,
int heightMeasureSpec) {
if(mBitmap != null)
{
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = width * mBitmap.getHeight() / mBitmap.getWidth();
setMeasuredDimension(width, height);
}
else
{
super.onMeasure(widthMeasureSpec,
heightMeasureSpec);
}
}
#Override
public void setImageBitmap(Bitmap bitmap)
{
mBitmap = bitmap;
super.setImageBitmap(bitmap);
}
}
I think you should change your imgV width to "match_parent"
You should change the scale type to imgV.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
I made a slight change to #Seraphim S's solution to account for cases where the image may be wide, and the view's bounds also wide (for example, rotating the device to landscape mode).
public class ResizableImageView extends ImageView {
public ResizableImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Drawable d = getDrawable();
if (d != null) {
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
if (width >= height) {
width = (int) Math.ceil((float) height * (float) d.getIntrinsicWidth() / (float) d.getIntrinsicHeight());
} else {
height = (int) Math.ceil((float) width * (float) d.getIntrinsicHeight() / (float) d.getIntrinsicWidth());
}
setMeasuredDimension(width, height);
} else {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
}

Categories

Resources