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

Apps How put two image in a View and auto-scale them?

panthe

Lurker
Hi guys,
I'm trying learn to develop in Android.
I want create a view with two image that auto-scale them.
Someone can help me?
I'm newbye...

Bye
Panthe
 
Hi,
I want have two images that fit most screen possible with the correct normal ratio.
The best will be:

landscape
------------------
- img - img -
-----------------

portrait
----------
- img -
----------
- img -
----------

with the single image that fit the single layout.
 
Make 2 vieuw's each taking half of the screen and use the following:

Var types are:
mPreviewSize = Size
mSupportedPreviewSizes = List<Size>

use this to call the optimalisation
Code:
mPreviewSize = getOptimalPreviewSize(mSupportedPreviewSizes, width, height);
This will resize keeping the aspect ratio
Code:
   private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {
        final double ASPECT_TOLERANCE = 0.1;
        double targetRatio = (double) w / h;
        if (sizes == null) return null;

        Size optimalSize = null;
        double minDiff = Double.MAX_VALUE;

        int targetHeight = h;

        // Try to find an size match aspect ratio and size
        for (Size size : sizes) {
            double ratio = (double) size.width / size.height;
            if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;
            if (Math.abs(size.height - targetHeight) < minDiff) {
                optimalSize = size;
                minDiff = Math.abs(size.height - targetHeight);
            }
        }

        // Cannot find the one match the aspect ratio, ignore the requirement
        if (optimalSize == null) {
            minDiff = Double.MAX_VALUE;
            for (Size size : sizes) {
                if (Math.abs(size.height - targetHeight) < minDiff) {
                    optimalSize = size;
                    minDiff = Math.abs(size.height - targetHeight);
                }
            }
        }
        return optimalSize;
    }

I'ts from my camera app so it probably needs a little ajusting.
mSupportedpreviewsizes should hold the sizes you can use for the image
 
Scuse me for the stupid question but I'm very very newbie with Android and I don't know how to set a view for take only half of the screen....

Sorry man ;-)
 
Hi,
I've used the follow code, now appears two images but these images aren't at the center of the display, I don't understand how I can center this view.

Code:
import android.app.Activity;
import android.os.Bundle;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Gallery.LayoutParams;

public class TwoImageView extends Activity {
    LinearLayout mLinearLayout;    
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
     // Create a LinearLayout in which to add the ImageView
        mLinearLayout = new LinearLayout(this);

        // Instantiate an ImageView and define its properties
        ImageView i = new ImageView(this);
        i.setImageResource(R.drawable.luca);
        i.setAdjustViewBounds(true); // set the ImageView bounds to match the Drawable's dimensions
        i.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        
     // Instantiate an ImageView and define its properties
        ImageView i2 = new ImageView(this);
        i2.setImageResource(R.drawable.luca);
        i2.setAdjustViewBounds(true); // set the ImageView bounds to match the Drawable's dimensions
        i2.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        
        // Add the ImageView to the layout and set the layout as the content view
        mLinearLayout.addView(i);
        mLinearLayout.addView(i2);
        setContentView(mLinearLayout);
        
    }
}

thanks in advance ;-)
 
OK,
for center the images I've used the weight in this way:

LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,2);
i.setLayoutParams(lp); // image1
i2.setLayoutParams(lp); // image2

Now works perfectly!!
 
Back
Top Bottom