Dividing the screen into equal parts

283
November 25, 2017, at 7:27 PM

I'm working on a project where I want to divide the screen into 3 equal parts, so that I can place the merged image that fit the single image in each part. so that I cannot worry about the size of the image.

Thank you very much for your time and assistance in this matter.

Main Activity

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private ImageView img;
ArrayList<Bitmap> listBmp;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    img=(ImageView) findViewById(R.id.img);
}
public void btnClick(View v){
 listBmp=new ArrayList<Bitmap>();

listBmp.add(BitmapFactory.decodeResource(getResources(),R.drawable.images));
listBmp.add(BitmapFactory.decodeResource(getResources(),R.drawable.img1))     
listBmp.add(BitmapFactory.decodeResource(getResources(),R.drawable.img2));      
listBmp.add(BitmapFactory.decodeResource(getResources(),R.drawable.img3));      
listBmp.add(BitmapFactory.decodeResource(getResources(),R.drawable.img4));    
listBmp.add(BitmapFactory.decodeResource(getResources(),R.drawable.img4));   
listBmp.add(BitmapFactory.decodeResource(getResources(),R.drawable.img4));     
listBmp.add(BitmapFactory.decodeResource(getResources(),R.drawable.img4))     
listBmp.add(BitmapFactory.decodeResource(getResources(),R.drawable.img4));
    Bitmap mergedImg= combineImageIntoOne(listBmp);
    img.setImageBitmap(mergedImg);
}
private Bitmap combineImageIntoOne(ArrayList<Bitmap> bitmap) {
    int w = 0, h = 0;
    for (int i = 0; i < bitmap.size(); i++) {
        if (i < bitmap.size() - 1) {
            w = bitmap.get(i).getWidth() > bitmap.get(i + 1).getWidth() ?bitmap.get(i).getWidth() : bitmap.get(i + 1).getWidth();
        }
        h += bitmap.get(i).getHeight();
    }

    Bitmap temp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(temp);
    int top = 0;

    for (int i = 0; i < bitmap.size(); i++) {
        Log.d("HTML", "Combine: "+i+"/"+bitmap.size()+1);

        top = (i == 0 ? 0 : top+bitmap.get(i).getHeight());
        canvas.drawBitmap(bitmap.get(i), 0f, top, null);
    }
    return temp;

}

it is working well but as add more images the merged is getting small. so I wanted to fix the size on screen.

Answer 1

set the imageview weight under Linear layout in your layout file.

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content
android:layout_weight="3">
<ImageView 
android:layout_width="match_parent"
android:layout_height="match_parent
android:layout_weight="1"
/>
<ImageView 
android:layout_width="match_parent"
android:layout_height="match_parent
android:layout_weight="1"
/>
<ImageView 
android:layout_width="match_parent"
android:layout_height="match_parent
android:layout_weight="1"
/>
</LinearLayout>
Answer 2

Well your question isn't enough clear here. But I think you're trying to set 3 images in single layout activity vertically. You can try as follows

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"/>
        <ImageView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:src="@drawable/your_img1"
                        android:scaleType="fitXY"
                        android:layout_weight="2"/>
        <ImageView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:src="@drawable/your_img2"
                        android:scaleType="fitXY"                            
                        android:layout_weight="2"/>
        <ImageView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:src="@drawable/your_img3"
                        android:scaleType="fitXY"
                        android:layout_weight="2"/>
</LinearLayout>

The android:scaleType="fitXY" will adjust the size. The scaleType will adjust the image as you have given the length and width of your view.

Let me know if you've problem with this.

Rent Charter Buses Company
READ ALSO
Heterogenous Layouts inside the RecyclerView Adapter issue?

Heterogenous Layouts inside the RecyclerView Adapter issue?

Chat application having vertical and horizontal RecyclerviewWhen we set the horizontal adapter inside main adapter class replacing all horizontal recycle view with same data

262
Load all types of local files in webview

Load all types of local files in webview

I need to open all type of files from web and local storageIf user downloaded that file than display from local storage otherwise display from URL

264
Android app receive mutiple notification

Android app receive mutiple notification

I have trouble when i put some libraries from UI android template app to my app that had installed FMS alreadyDo you know why my app receives multiple notification from Firebase

207
RecyclerView() Adapter

RecyclerView() Adapter

I am trying to create adapter for RecyclerView(), but it does not go to Overrided methodsHere is my code of Adapter

271