Resize image to screen size

Asked

Viewed 1,666 times

1

created a Viewa class where I take two pictures of the drawable folder and draw on the screen with draw method();

public class ViewA extends View {

private Bitmap img1;
private Bitmap img2;

public ViewA(Context context){
    super(context);
    img1 = BitmapFactory.decodeResource(getResources(), R.drawable.imagem01);
    img2 = BitmapFactory.decodeResource(getResources(), R.drawable.imagem02);
}

public void draw(Canvas canvas){
    super.draw(canvas);
    canvas.drawBitmap(img1, 0, 0, null);
    canvas.drawBitmap(img2, 0, 0, null);
}

}

and here is Mainacttivity

public class MainActivity extends AppCompatActivity {

private ViewA view;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    view = new ViewA(this);
    setContentView(view);

    getWindow().getDecorView().setSystemUiVisibility(
            View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
                    View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
                    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
                    View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
                    View.SYSTEM_UI_FLAG_FULLSCREEN |
                    View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);

}

}

is full screen however at the time of taking the images and drawing on the screen, assuming that it is an image of resolution 1920x1080 the images is cropped and only part of the image appears. I wanted to know if there is a way to put these images on the screen !! without being cropped.

  • img1 = Bitmap.createScaledBitmap(img1, 1300, 800, true); I found this solution, but if changing device does not fill the whole screen ...

1 answer

1

If the idea is to put a background image in Activity do the following:

sua_activity.xml

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/imgBack"
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        android:scaleType= "fitCenter"          <- escolha aqui o tipo de ajuste
        android:src="@drawable/imagem"/>

     <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        ...         <- controles que vão sobrepor a imagem de fundo

     </LinearLayout>

</FrameLayout>

See documentation of adjustment types on Imageview.Scaletype

If you want to have more control over the image, create a class derived from Imageview

  • then in the case the image has already been put by the drawing() médodo; the problem is that if it is a small image it does not fill the whole screen and if it is large it is cut. i didn’t use an imageview on Activity

  • It would be easier in the Activity layout. But do the following: derive Nomeclass of Imageview and not of View and does not overlap the method onDraw. From there only use the methods that the Imageview offers as for example setScaleType

  • To set the image use setImageResource(R.drawable.image) no need to decode to Bitmap

  • scaleType does not adjust the image to occupy the whole screen. I tried to use in Activity and did not fill stays an edge yet ! Note: the application this full screen

  • Edit the question, add the source code and detail to the shape you are trying to make.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.