0

I hate to learn all complicated stuff about ImageView. I basically want something like this:

image_view.setX(200);
image_view.setY(200);
image_view.setWidth(50);
image_view.setHeight(50);

It must not be affected by gravity. What I want is absolute position and absolute size (by pixel).

Can you help me do that to simplify Android programming since I had previous experience with 2D computer development before? Thank you.

  • "What I want is absolute position and absolute size (by pixel)" -- that is not a particularly good plan for mobile devices, particularly Android ones. Devices have a wide range of screen resolutions and densities, and your approach will not take that into account. You may have better luck using a 2D game engine (e.g., AndEngine). – CommonsWare Aug 15 '17 at 00:29
  • It is not a good plan, for the time being, I will use android screen width and screen height to scale my images accordingly. Is it acceptable for you? When my experience improves, I can learn more complicated things. –  Aug 15 '17 at 00:31
  • If you are going to use the standard Android `View`-based UI framework, I strongly encourage you to use it properly. Absolute sizes and absolute positions were abandoned as techniques in 2009. – CommonsWare Aug 15 '17 at 00:35
  • "Absolute sizes and absolute positions" were abandoned, it is still a technique. I used it to develop 2D games (pretty effectively), so I am familiar with it. It is harder to get things done (if you meant Android), but it is not impossible. –  Aug 15 '17 at 00:38
  • At least, I want to satisfy my demands to render ImageView(s) on the screen the way I want. I can learn more elegant ways to accomplish the same thing later. –  Aug 15 '17 at 00:39
  • @CommonsWare - I found an answer for my own, is it something considered admirable as a new Android learner like me? –  Aug 15 '17 at 01:32
  • I would not consider it to be admirable. – CommonsWare Aug 15 '17 at 11:28
  • @CommonsWare - That is so mean of you from someone with a high reputation. You should encourage beginners to learn, why are you blaming me all the time? –  Aug 15 '17 at 15:03
  • "You should encourage beginners to learn" -- I do, and I did, if you review my comments. "why are you blaming me all the time?" -- you ask questions, and I give answers. My [first comment](https://stackoverflow.com/questions/45684895/rendering-an-imageview-with-absolute-size-and-coordindate?noredirect=1#comment78327715_45684895) was in response to your top-level question. Each subsequent comment, including this one, was in response to a question that you posed in a comment. I will take steps to avoid commenting on your questions in the future. – CommonsWare Aug 15 '17 at 15:34
  • @CommonsWare - If you do (encourage me) that is fine. I am sorry that I can't take ideal knowledge from you right now, I suggest that we had better be more amicable. Let's be more friendly, shall we? –  Aug 15 '17 at 17:29
  • But please remember that you were actually of no help. I am the one to answer my own question. Your advice will be considered later when I have more knowledge and practical experience. –  Aug 15 '17 at 17:30

1 Answers1

0

I will have to scale each image beforehand, I will not use ImageView functions to scale the images.

    final int square_size = 150;
    Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.android_img);
    Bitmap bm2 = BitmapFactory.decodeResource(getResources(), R.drawable.android_img2);

    ImageView image_view1 = new ImageView(this);        
    image_view1.setImageBitmap(Bitmap.createScaledBitmap(bm, square_size, square_size, false));
    image_view1.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    image_view1.setX(0);
    image_view1.setY(0);
    image_view1.setScaleType(ImageView.ScaleType.MATRIX);

    ImageView image_view2 = new ImageView(this);        
    image_view2.setImageBitmap(Bitmap.createScaledBitmap(bm2, square_size, square_size, false));
    image_view2.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    image_view2.setX(square_size + 20);
    image_view2.setY(0);
    image_view2.setScaleType(ImageView.ScaleType.MATRIX);

    ImageView image_view3 = new ImageView(this);        
    image_view3.setImageBitmap(Bitmap.createScaledBitmap(bm2, square_size, square_size, false));
    image_view3.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    image_view3.setX(0);
    image_view3.setY(square_size + 20);
    image_view3.setScaleType(ImageView.ScaleType.MATRIX);

    ImageView image_view4 = new ImageView(this);        
    image_view4.setImageBitmap(Bitmap.createScaledBitmap(bm, square_size, square_size, false));
    image_view4.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    image_view4.setX(square_size + 20);
    image_view4.setY(square_size + 20);
    image_view4.setScaleType(ImageView.ScaleType.MATRIX);

    RelativeLayout views = new RelativeLayout(this);

    views.addView(image_view1);
    views.addView(image_view2);
    views.addView(image_view3);
    views.addView(image_view4);

    setContentView(views);

And the result looks plausible! How wonderful it can be!

enter image description here