渲染具有绝对大小和coordindate的ImageView

时间:2017-08-15 00:24:51

标签: java android imageview absolutelayout

我讨厌学习有关ImageView的所有复杂内容。我基本上想要这样的东西:

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

不得受重力影响。我想要的是绝对位置绝对大小(按像素)。

我能帮助我做到这一点,以简化Android编程,因为我之前有过2D计算机开发的经验吗?谢谢。

1 个答案:

答案 0 :(得分:0)

我必须预先缩放每个图像,我不会使用ImageView函数来缩放图像。

    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);

结果看似合理!这太棒了!

enter image description here