[UI][Detail Activity] Made background of viewpager is average color of ImageView, and scrolling down with parallax effect.

This commit is contained in:
ChronosX88 2019-02-10 21:01:41 +04:00
parent 1e20c8ebcc
commit 666e6b2186
No known key found for this signature in database
GPG Key ID: 8F92E090A87804AA
6 changed files with 193 additions and 14 deletions

Binary file not shown.

View File

@ -1,6 +1,9 @@
package ru.volgorobot.vrcatalog.additional; package ru.volgorobot.vrcatalog.additional;
import android.content.Context; import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter; import android.support.v4.view.PagerAdapter;
import android.view.View; import android.view.View;
@ -9,6 +12,9 @@ import android.widget.ImageView;
import com.squareup.picasso.Picasso; import com.squareup.picasso.Picasso;
import java.io.FileNotFoundException;
import java.io.IOException;
public class ImagePagerAdapter extends PagerAdapter { public class ImagePagerAdapter extends PagerAdapter {
/** /**
* This class is designed to control the ViewPager (images of item) in the activity of viewing details of items. * This class is designed to control the ViewPager (images of item) in the activity of viewing details of items.
@ -57,4 +63,14 @@ public class ImagePagerAdapter extends PagerAdapter {
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) { public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((View) object); container.removeView((View) object);
} }
public Bitmap getBitmap(int position) {
Bitmap bitmap = null;
try {
bitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver(), Uri.parse(imageUris[position]));
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
} }

View File

@ -1,22 +1,24 @@
package ru.volgorobot.vrcatalog.view; package ru.volgorobot.vrcatalog.view;
import android.content.Intent; import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle; import android.os.Bundle;
import android.support.design.widget.TabLayout; import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.text.method.LinkMovementMethod; import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log; import android.util.Log;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ListAdapter; import android.widget.ListAdapter;
import android.widget.ListView; import android.widget.ListView;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import java.util.HashMap;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import ru.volgorobot.vrcatalog.ItemPresenter; import ru.volgorobot.vrcatalog.ItemPresenter;
@ -29,11 +31,14 @@ public class DetailActivity extends AppCompatActivity implements MainContract.It
private MainContract.ItemPresenter mPresenter; private MainContract.ItemPresenter mPresenter;
private ViewPager viewPager; private ViewPager viewPager;
private ListView propertiesList; private ListView propertiesList;
private ImagePagerAdapter imagePagerAdapter;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail); setContentView(R.layout.activity_detail);
Toolbar toolbar = findViewById(R.id.itemactivity_toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true); getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false); getSupportActionBar().setDisplayShowTitleEnabled(false);
@ -135,11 +140,102 @@ public class DetailActivity extends AppCompatActivity implements MainContract.It
@Override @Override
public void setViewPagerContent(String[] uris) { public void setViewPagerContent(String[] uris) {
ImagePagerAdapter imagePagerAdapter = new ImagePagerAdapter(DetailActivity.this, uris); imagePagerAdapter = new ImagePagerAdapter(DetailActivity.this, uris);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabDots); TabLayout tabLayout = (TabLayout) findViewById(R.id.tabDots);
viewPager.setAdapter(imagePagerAdapter); viewPager.setAdapter(imagePagerAdapter);
setViewPagerScrollListener();
tabLayout.setupWithViewPager(viewPager); tabLayout.setupWithViewPager(viewPager);
imagePagerAdapter.notifyDataSetChanged(); imagePagerAdapter.notifyDataSetChanged();
} }
private int getImageAverageColor(Bitmap bitmap) {
if (null == bitmap) return Color.TRANSPARENT;
int redBucket = 0;
int greenBucket = 0;
int blueBucket = 0;
int alphaBucket = 0;
boolean hasAlpha = bitmap.hasAlpha();
int pixelCount = bitmap.getWidth() * bitmap.getHeight();
int[] pixels = new int[pixelCount];
bitmap.getPixels(pixels, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
for (int y = 0, h = bitmap.getHeight(); y < h; y++)
{
for (int x = 0, w = bitmap.getWidth(); x < w; x++)
{
int color = pixels[x + y * w]; // x + y * width
redBucket += (color >> 16) & 0xFF; // Color.red
greenBucket += (color >> 8) & 0xFF; // Color.greed
blueBucket += (color & 0xFF); // Color.blue
if (hasAlpha) alphaBucket += (color >>> 24); // Color.alpha
}
}
return Color.argb(
(hasAlpha) ? (alphaBucket / pixelCount) : 255,
redBucket / pixelCount,
greenBucket / pixelCount,
blueBucket / pixelCount);
}
private void setViewPagerScrollListener() {
ViewPager.OnPageChangeListener listener = new ViewPager.OnPageChangeListener() {
boolean firstImageProcessed = false;
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
if(position == 0 && !firstImageProcessed) {
viewPager.setBackgroundColor(getImageAverageColor(imagePagerAdapter.getBitmap(position)));
firstImageProcessed = true;
}
}
@Override
public void onPageSelected(int position) {
if(firstImageProcessed) {
viewPager.setBackgroundColor(getImageAverageColor(imagePagerAdapter.getBitmap(position)));
}
}
@Override
public void onPageScrollStateChanged(int state) {
//
}
};
viewPager.setOnPageChangeListener(listener);
}
public static boolean setListViewHeightBasedOnItems(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter != null) {
int numberOfItems = listAdapter.getCount();
// Get total height of all items.
int totalItemsHeight = 0;
for (int itemPos = 0; itemPos < numberOfItems; itemPos++) {
View item = listAdapter.getView(itemPos, null, listView);
item.measure(0, 0);
totalItemsHeight += item.getMeasuredHeight();
}
// Get total height of all item dividers.
int totalDividersHeight = listView.getDividerHeight() *
(numberOfItems - 1);
// Set list height.
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalItemsHeight + totalDividersHeight;
listView.setLayoutParams(params);
listView.requestLayout();
return true;
} else {
return false;
}
}
} }

View File

@ -5,7 +5,9 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content"> android:layout_height="wrap_content">
<LinearLayout
<!-- <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:orientation="vertical"> android:orientation="vertical">
@ -27,7 +29,7 @@
<TextView <TextView
android:id="@+id/itemName" android:id="@+id/itemName"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="40dp" android:layout_height="wrap_content"
android:text="Item Name" android:text="Item Name"
android:textAppearance="@style/TextAppearance.AppCompat.Body1" android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:textSize="30sp" android:textSize="30sp"
@ -46,9 +48,77 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content"/> android:layout_height="wrap_content"/>
</LinearLayout> </LinearLayout>-->
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|enterAlways">
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="wrap_content"
android:layout_height="311dp"
app:layout_collapseMode="parallax" />
<android.support.design.widget.TabLayout
android:id="@+id/tabDots"
android:layout_width="match_parent"
android:layout_height="62dp"
android:layout_alignParentBottom="true"
app:tabBackground="@drawable/tab_selector"
app:tabGravity="center"
app:tabIndicatorHeight="0dp" />
<android.support.v7.widget.Toolbar
android:id="@+id/itemactivity_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/AppTheme" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="619dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/itemName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="9dp"
android:text="Item Name"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:textSize="30sp" />
<TextView
android:id="@+id/itemPrice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="9dp"
android:text="Item Price"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:textSize="24sp" />
<ListView
android:id="@+id/propertiesList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:nestedScrollingEnabled="false"/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout> </android.support.design.widget.CoordinatorLayout>

View File

@ -5,16 +5,13 @@
<item name="windowNoTitle">true</item> <item name="windowNoTitle">true</item>
<item name="android:statusBarColor">@android:color/transparent</item> <item name="android:statusBarColor">@android:color/transparent</item>
</style> </style>
<style name="ItemActivityTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <style name="ItemActivityTheme" parent="AppTheme">
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="colorPrimary">@android:color/transparent</item>
<item name="actionBarStyle">@style/MyActionBar</item>
<item name="windowActionBarOverlay">true</item>
<item name="android:windowTranslucentStatus">true</item> <item name="android:windowTranslucentStatus">true</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style> </style>
<style name="MyActionBar" parent="Widget.AppCompat.ActionBar.Solid"> <style name="MyActionBar" parent="Widget.AppCompat.ActionBar.Solid">
<item name="android:background">@android:color/transparent</item> <item name="android:background">@android:color/transparent</item>
<!--For compatibility-->
<item name="background">@android:color/transparent</item> <item name="background">@android:color/transparent</item>
</style> </style>
</resources> </resources>