0

I'm creating several ImageViews programmatically, but I'm running into an issue where there are different ImageView sizes on different displays. I want the ImageView size to be fixed on all screens. Here is how I am generating those ImageViews:

for (int i = 0; i < myImageList.size(); i++) {
    ImageView iv = new ImageView(this);
    iv.setImageResource(myImageList.get(i));
    FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(420, 210);
    lp.gravity = Gravity.CENTER;
    iv.setLayoutParams(lp);
    float angleDeg = i * 360.0f / myImageList.size() - 70.0f;
    float angleRad = (float) (angleDeg * Math.PI / 180.0f);
    iv.setTranslationX(320 * (float) Math.cos(angleRad));
    iv.setTranslationY(320 * (float) Math.sin(angleRad));
    iv.setRotation(angleDeg + 80.0f);
    main.addView(iv);
    final int finalI = i;
}
colm.anseo
  • 19,337
  • 4
  • 43
  • 52

3 Answers3

0

You could introduce new class derived from ImageView and make all those adjustments in that class. Then just replaces ImageView to your class implementation in layouts etc

Dmytro Batyuk
  • 957
  • 8
  • 15
0

There are multiple interpretations of your questions.

  1. The ImageView should have same size on all the devices - use dp. You are using pixels right now in the LayoutParams.
  2. The ImageView should look that it has same size on all screens - find the width and height of the screen, set an aspect ration, eg. 0.33 and use that factored width and height in LayoutParams.
  3. Use MATCH_PARENT in LayoutParams for width as well as height and the ImageView will take the full size (of the parent).

This question has a lot of great explanation about dp, px, sp that could be useful - What is the difference between "px", "dip", "dp" and "sp"?

Froyo
  • 17,947
  • 8
  • 45
  • 73
0

To find screen sizes

    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    // these are sizes in pixels
    metrics.widthPixels, metric.heightPixels

Then, you can use this

iv.setScaleType(ImageView.ScaleType.FIT_XY);

See this link for all ScaleType values

Android ImageView ScaleType: A Visual Guide

Ferran
  • 1,442
  • 1
  • 6
  • 10