• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps How can I resize an image of 720x1080 to display on every device ?

I have an image that I'm using as a background for an app. I really like how it looks. I would like the android device to resize it automatically.

I tried to use Picasso library with :
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
this will work but it takes time to display it and I don't like that.

After that I used android:scaleType="fitXY" which works on my Note3 device, but it won't work on a galaxy Nexus. I'm getting "Bitmap too large to be uploaded into a texture (2880x4320, max=4096x4096)". My image is 720x1080 px. I know that the android is converting it and it is somehow exceeding the dp limit.

My image is a png file. I have it in a photoshop project so I can save it as any file if it could help me.

I would like some ideas on how to scale it automatically on every device if it is possible, if not I would be happy with suggestions on what resolutions are every drawable-mhdpi,hdpi,xhdpi etc folder and how to resize my image without damaging the aspect. Thank you :D
 
In which resource folder you put your background image? Try to put into folder drawable-nodpi and remove from all other folders (drawable-hdpi, drawable-xxdpi, etc)
 
I have it in my drawable folder. I forgot to mention that I tried the nodpi folder too. It will work on galaxy nexus, but it will cover only 1/3 of note3
 
I have also problems with bitmaps, and now use this class for some backgrounds. This draws most part of Bitmap on canvas. You can put here bitmap via BitmapFactory.decodeResource
Java:
package com.jbak.superbrowser.ui.themes;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

public class MyBitmapDrawable extends Drawable {
    Bitmap mBmp;
    Drawable mDrw;
    Rect mDest = new Rect();
    Rect mSrc = new Rect();
    Point mSize;
    public MyBitmapDrawable(Context c,Bitmap bmp)
    {
        set(bmp);
    }
    public MyBitmapDrawable(Context c, Drawable drw) {
        set(drw);
    }
    public void set(Bitmap bmp)
    {
        mBmp = bmp;
        mSize = new Point(bmp.getWidth(), bmp.getHeight());
    }
    public void set(Drawable drw)
    {
        if(drw instanceof BitmapDrawable)
        {
            set(((BitmapDrawable)drw).getBitmap());
            return;
        }
        mDrw = drw;
        mSize = new Point(drw.getIntrinsicWidth(), drw.getIntrinsicHeight());
    }
    @Override
    public void draw(Canvas canvas) {
        if(mDrw!=null)
            mDrw.draw(canvas);
        if(mBmp==null||mBmp.isRecycled())
            return;
        canvas.drawBitmap(mBmp, mSrc, mDest, null);
    }
    @Override
    public void setBounds(int left, int top, int right, int bottom) {
        mDest.set(left, top, right, bottom);
        if(!mDest.isEmpty()&&mSize.x>0&&mSize.y>0)
        {
            float dw = ((float)mDest.width())/((float)mSize.x);
            float dh = ((float)mDest.height())/((float)mSize.y);
            int x = 0,y=0;
            if(dw>dh)
            {
                y= (int) (((float)mSize.y)*dh/dw);
                int sy = mSize.y/2-y/2;
                mSrc.set(0, sy, mSize.x, sy+y);
            }
            else
            {
                x = (int) (((float)mSize.x)*dw/dh);
                int sx = mSize.x/2-x/2;
                mSrc.set(sx, 0, sx+x, mSize.y);
            }
        }
        super.setBounds(left, top, right, bottom);
    }
    @Override
    public int getOpacity() {
        return 0;
    }
    @Override
    public void setAlpha(int alpha) {
       
    }
    @Override
    public void setColorFilter(ColorFilter cf) {
       
    }
}
 
You need to set in code - setBackgroundDrawable , setImageDrawable, depends where you need to set it. Yes, its free to use
 
I want to set it in this imageView which wraps the entire layout :

<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/background"
android:scaleType="fitXY"
android:visibility="visible" />
 
Try this in activity class after setContentView:
Code:
((ImageView)findViewById(R.id.imageView1))
.setImageDrawable(new MyBitmapDrawable(BitmapFactory.decodeResource(getResources(),R.drawable.background)));
 
I added it after setContentView as you said but it had some errors because it did not have a context.So I replaced it with this(I just added the word "this" to give the context, as the constructor says) :

Code:
setContentView(layout.activity_main);

        ((ImageView)findViewById(R.id.imageView1))
                .setImageDrawable(new MyBitmapDrawable(this,BitmapFactory.decodeResource(getResources(), R.drawable.background)));

But it isn't working. I think it is because the context of the constructor from your class is never used.

Code:
public MyBitmapDrawable(Context c,Bitmap bmp)
    {
        set(bmp);
    }
 
Context not needed at all for this class. Try to remove src and scaleType from ImageView in xml , and change
android:layout_width="wrap_content"
android:layout_height="wrap_content"
to match_parent

Maybe, after changing to match_parent - all will works through xml only :-)
 
Back
Top Bottom