Monday 30 November 2015

View Pager for show Fullscreen image with customize width & height of activity

1) FullScreenViewActivity.java
=============================
package com.bluegreen.masmas.customer.activities;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import com.bluegreen.masmas.customer.R;
import com.bluegreen.masmas.customer.Utility.Consts;
import com.bluegreen.masmas.customer.adapter.FullScreenImageAdapter;
import com.bluegreen.masmas.customer.model.DealDetailImagesModel;
import java.util.ArrayList;

public class FullScreenViewActivity extends Activity implements OnClickListener {
   public FullScreenImageAdapter adapter;
   private ViewPager viewPager;
   public static final String VIEW_PAGER_OBJECT_TAG = "image#";
   int position;
   public static int intPagePosition;
   private ImageView imgClose;
   private ArrayList<DealDetailImagesModel> alstDealImages;

   @Override   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      requestWindowFeature(Window.FEATURE_NO_TITLE);
      getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
      FullScreencall();
      setContentView(R.layout.activity_fullscreen_view);
      int width = this.getResources().getDisplayMetrics().widthPixels;
      int height = this.getResources().getDisplayMetrics().heightPixels;
      height = height-150;
      
      WindowManager.LayoutParams params = getWindow().getAttributes();  
      params.x = -20;  
      params.height = width;
      params.width = width;  
      params.y = -10;  
      this.getWindow().setAttributes(params);
      alstDealImages = new ArrayList<DealDetailImagesModel>();

      viewPager = (ViewPager) findViewById(R.id.pager);
      viewPager.setPageMargin(20);
      viewPager.setPageMarginDrawable(R.color.black);
      imgClose = (ImageView) findViewById(R.id.imgClose);
      imgClose.setOnClickListener(this);

      Intent intent = getIntent();
      position = intent.getExtras().getInt(Consts.IMAGE_DEAL_POS, 0);
      viewPager.setCurrentItem(position);
      Bundle args = intent.getBundleExtra(Consts.IMAGE_BUNDLE);
      alstDealImages = (ArrayList<DealDetailImagesModel>) args.getSerializable(Consts.IMAGE_DEAL_ARRAY);
      adapter = new FullScreenImageAdapter(FullScreenViewActivity.this,alstDealImages, width, height);
      viewPager.setAdapter(adapter);
      viewPager.setCurrentItem(position);
   }

   @Override   protected void onResume() {
      super.onResume();
      FullScreencall();
   }

   public void FullScreencall() {
      if (Build.VERSION.SDK_INT < 19) {
         View v = this.getWindow().getDecorView();
         v.setSystemUiVisibility(View.GONE);
      } else {
         View decorView = getWindow().getDecorView();
         int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
         decorView.setSystemUiVisibility(uiOptions);
      }
   }

   @Override   public void onClick(View v) {
      switch (v.getId()) {
      case R.id.imgClose:
         finish();
         break;

      default:
         break;
      }
   }
}


2) FullScreenImageAdapter.java
==============================
package com.bluegreen.masmas.customer.adapter;

import android.content.Context;
import android.graphics.Matrix;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bluegreen.masmas.customer.R;
import com.bluegreen.masmas.customer.model.DealDetailImagesModel;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;

public class FullScreenImageAdapter extends PagerAdapter {
    private ArrayList<DealDetailImagesModel> alstImages;
    private LayoutInflater inflater;
    private Context mContext;
    ImageView imgBig;
    public static ImageView imgClose = null;
    public int width, height;

    public FullScreenImageAdapter(Context mContext, ArrayList<DealDetailImagesModel> alstImages, int width, int height) {
        this.mContext = mContext;
        this.alstImages = alstImages;
        this.width = width;
        this.height = height;
    }

    public int getCount() {
        return this.alstImages.size();
    }

    public boolean isViewFromObject(View view, Object object) {
        return view == ((RelativeLayout) object);
    }

    Matrix imageMatrix;
    public Object instantiateItem(ViewGroup container, int position) {
        inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.layout_fullscreen_image, container, false);

        imgBig = (ImageView) view.findViewById(R.id.imgBig);
        imgBig.getLayoutParams().width = (int) width;
        imgBig.getLayoutParams().height = (int) (width);

        Picasso.with(mContext).load(alstImages.get(position).getStrImage()).into(imgBig);

        ((ViewPager) container).addView(view);
        return view;
    }

    @Override    public void destroyItem(ViewGroup container, int position, Object object) {
        ((ViewPager) container).removeView((View) object);
    }
}

3) activity_fullscreen_view.xml
===============================
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:background="@color/black"    android:orientation="vertical" >

    <FrameLayout        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:layout_centerInParent="true"        android:gravity="center" >

        <android.support.v4.view.ViewPager            android:id="@+id/pager"            android:layout_width="fill_parent"            android:layout_height="fill_parent" >
        </android.support.v4.view.ViewPager>

        <ImageView            android:id="@+id/imgClose"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_gravity="right|top"            android:contentDescription="@string/app_name"            android:src="@drawable/delete" />
    </FrameLayout>

</RelativeLayout>

4) layout_fullscreen_image.xml
===============================
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/layout_root"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:background="@color/black" >
    <ImageView        android:id="@+id/imgBig"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:layout_alignParentLeft="true"        android:layout_alignParentTop="true"        android:adjustViewBounds="true"        android:contentDescription="@string/app_name"        android:gravity="center"        android:scaleType="fitXY"        android:src="@drawable/list_noimg" />

</RelativeLayout>