curioushikhov
curioushikhov

Reputation: 2830

Unable to fill imageview by bitmap, weird behavior

Simple layout

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0099cc"
tools:context="com.testzoom.app.FullscreenActivity">

<TextView android:id="@+id/fullscreen_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:keepScreenOn="true"
    android:textColor="#33b5e5"
    android:textStyle="bold"
    android:textSize="50sp"
    android:gravity="center"
    android:text="@string/dummy_content" />

    <Button android:id="@+id/dummy_button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/dummy_button"
            android:layout_gravity="bottom"
            />

    <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="fitXY"
            android:id="@+id/dummy_image"
            />

</FrameLayout>

Goal - by pressing button on the bottom, set imageview(which is same size as screen) with screenshot bitmap of half size. Result - weird rendering, why fitXY not working?

Click handler:

findViewById(R.id.dummy_button).setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            View view = getWindow().getDecorView();

            Bitmap bmp = Bitmap.createBitmap(view.getWidth()/2, view.getHeight()/2, Bitmap.Config.ARGB_8888);

            Canvas c = new Canvas(bmp);
            c.scale(0.5f,0.5f, bmp.getWidth(), bmp.getHeight());
            view.draw(c);

            ((ImageView)findViewById(R.id.dummy_image)).setImageBitmap(bmp);
        }
    });

Result on screenshots:

app launch After pressing


Note that this task is synthetic so don't ask why I am doing this, this is just demonstration of problem.

Upvotes: 1

Views: 376

Answers (2)

mkhan
mkhan

Reputation: 621

The problem lies within the call where you are scaling the canvas. You provide the pivot points which are bmp.getWidth() and bmp.getHeight() which are essentially in the middle of the screen.

findViewById(R.id.dummy_button).setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            View view = getWindow().getDecorView();

            Bitmap bmp = Bitmap.createBitmap(view.getWidth()/2, view.getHeight()/2, Bitmap.Config.ARGB_8888);

            Canvas c = new Canvas(bmp);
            c.scale(0.5f,0.5f);//no pivot points 
            view.draw(c);

            ((ImageView)findViewById(R.id.dummy_image)).setImageBitmap(bmp);
        }
    });

Upvotes: 1

Martin
Martin

Reputation: 4816

I don't think it's the rendering so much as the placement of the new ImageView. You have all three items in a FrameLayout which does nothing to determine the order or placement of view. Where is the code to place the newly created ImageView?

Upvotes: 0

Related Questions