ausgeorge
ausgeorge

Reputation: 1015

Scale ImageView and clip bottom only when outside parent view?

how does one scale an ImageView, yet still have it clip (bottom only) when it/parts of it are outside its parent view?

My code;

XML has 3 elements; image in raw size, view 200px high, view 150px high.

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="1000px"
android:layout_height="850px"
android:background="#FFFEC6"
android:orientation="vertical">

<!-- single image in raw size -->
<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/fauxcard"
    />

<!-- should be SCALED with NO CLIPPING -->
<RelativeLayout
    android:id="@+id/Wrapper1"
    android:layout_width="1000px"
    android:layout_height="200px"
    android:background="#cccccc">
</RelativeLayout>

<!-- should be SCALED with the BOTTOM CLIPPED -->
<RelativeLayout
    android:id="@+id/Wrapper2"
    android:layout_width="1000px"
    android:layout_height="150px"
    android:background="#C6FFD1"
    android:clipChildren="true">
</RelativeLayout>
</LinearLayout>

java;

    RelativeLayout r1 = (RelativeLayout) findViewById(R.id.Wrapper1);
    RelativeLayout r2 = (RelativeLayout) findViewById(R.id.Wrapper2);

    int marginLeft = 0;
    for (int i = 0; i < 10; i++)
    {
        // IV 1 - the parent view for these is the correct 200 high, so these will be unclipped/unscaled
        ImageView myImg1 = new ImageView(this);
        myImg1.setImageResource(R.drawable.fauxcard);
        RelativeLayout.LayoutParams layout1 = new RelativeLayout.LayoutParams(100, 200);
        layout1.setMargins(marginLeft, 0, 0, 0);
        r1.addView(myImg1, layout1);

        // IV 2 - the parent view for these is NOT high enough (only 150 high), so I want these SCALED but CLIPPED (bottom of image clipped)
        ImageView myImg2 = new ImageView(this);
        myImg2.setImageResource(R.drawable.fauxcard);

        // scaling
        // myImg2.setScaleType(ImageView.ScaleType.CENTER); // NOT scaled - clipped
        // myImg2.setScaleType(ImageView.ScaleType.CENTER_INSIDE); //  scaled - NOT clipped
        myImg2.setScaleType(ImageView.ScaleType.CENTER_CROP); // scaled - clipped at TOP and bottom
        // myImg2.setScaleType(ImageView.ScaleType.FIT_END); // scaled TOO MUCH - clip not needed
        // myImg2.setScaleType(ImageView.ScaleType.FIT_START); // scaled TOO MUCH - clip not needed
        // myImg2.setScaleType(ImageView.ScaleType.FIT_CENTER); // scaled TOO MUCH - clip not needed
        // myImg2.setScaleType(ImageView.ScaleType.FIT_XY); // scaled incorrect ratio - clip not needed
        // myImg2.setScaleType(ImageView.ScaleType.MATRIX); // NOT scaled - clipped

        RelativeLayout.LayoutParams layout2 = new RelativeLayout.LayoutParams(100, 200);
        layout2.setMargins(marginLeft, 0, 0, 0);
        r2.addView(myImg2, layout2);

        // I am using marginLeft as my real scenario is more complex - there are varying gaps between the images based on other logic
        // the important issue is CLIPPING the images whilst scaling them
        marginLeft += 100;
    }

To both of the RelativeLayouts I'm adding 10 images. I want both sets of images to be scaled the same, but as the 2nd RL isn't high enough I want those images CLIPPED (the bottom of the image gone).

I've tried all ScaleTypes, but none of them achieve this. The closest is CENTER_CROP, which has the correct scaling, but both the top and bottom are clipped (and, again, I just want the bottom clipped).

Here's a image; https://imgur.com/a/QLJkm

Upvotes: 0

Views: 505

Answers (1)

Rahul
Rahul

Reputation: 5049

put this code in viewtreeobserver or you can have custom imageview

    Matrix matrix = imageView.getImageMatrix();
    float scale;
    final int viewWidth = getWidth() - getPaddingLeft() - getPaddingRight();
    final int drawableWidth = drawable.getIntrinsicWidth();

    scale = (float) viewWidth / (float) drawableWidth;
    matrix.setScale(scale, scale);
    setImageMatrix(matrix);

Upvotes: 1

Related Questions