From 666e6b21861dea8b02692ba052b1f173a1f4a37d Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Sun, 10 Feb 2019 21:01:41 +0400 Subject: [PATCH] [UI][Detail Activity] Made background of viewpager is average color of ImageView, and scrolling down with parallax effect. --- .idea/caches/gradle_models.ser | Bin 108688 -> 108688 bytes app/build.gradle | 2 +- .../additional/ImagePagerAdapter.java | 16 +++ .../vrcatalog/view/DetailActivity.java | 104 +++++++++++++++++- app/src/main/res/layout/activity_detail.xml | 76 ++++++++++++- app/src/main/res/values-v21/styles.xml | 9 +- 6 files changed, 193 insertions(+), 14 deletions(-) diff --git a/.idea/caches/gradle_models.ser b/.idea/caches/gradle_models.ser index 6fe1fd92dc8a445cc873f37c81c56ff4e4f8dad3..66f54b50f3c8106e34b7058c3ecf723064d86caa 100644 GIT binary patch delta 71 zcmbPmk!`|7wha?hIkP%@moR|9WX5dm%|}!_?O3KvnC#jdn6f=Eh4GLS+k~YL{22?T aD{3(cZ`Y}3yvD}j-y-vDJ6juLml6P{_!&q5 delta 71 zcmbPmk!`|7wha?hIlI3cDPsVE$&A_Bn~$h=+OhmN_Wyo!V9NHu6vjhRY;G?+G8qe| aD{3(cZ`Y}3yvD|&xXqepJ6juLml6Q^nHoU= diff --git a/app/build.gradle b/app/build.gradle index 5be5893..bb15bf1 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -34,4 +34,4 @@ dependencies { implementation 'com.squareup.retrofit2:converter-gson:2.4.0' implementation 'com.android.support:recyclerview-v7:27.1.1' implementation 'com.squareup.picasso:picasso:2.71828' -} +} \ No newline at end of file diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/additional/ImagePagerAdapter.java b/app/src/main/java/ru/volgorobot/vrcatalog/additional/ImagePagerAdapter.java index 5c111d5..69a63bb 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/additional/ImagePagerAdapter.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/additional/ImagePagerAdapter.java @@ -1,6 +1,9 @@ package ru.volgorobot.vrcatalog.additional; import android.content.Context; +import android.graphics.Bitmap; +import android.net.Uri; +import android.provider.MediaStore; import android.support.annotation.NonNull; import android.support.v4.view.PagerAdapter; import android.view.View; @@ -9,6 +12,9 @@ import android.widget.ImageView; import com.squareup.picasso.Picasso; +import java.io.FileNotFoundException; +import java.io.IOException; + public class ImagePagerAdapter extends PagerAdapter { /** * 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) { 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; + } } diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/view/DetailActivity.java b/app/src/main/java/ru/volgorobot/vrcatalog/view/DetailActivity.java index ab0629e..992e744 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/view/DetailActivity.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/view/DetailActivity.java @@ -1,22 +1,24 @@ package ru.volgorobot.vrcatalog.view; import android.content.Intent; +import android.graphics.Bitmap; +import android.graphics.Color; import android.os.Bundle; import android.support.design.widget.TabLayout; import android.support.v4.view.ViewPager; 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.view.MenuItem; import android.view.View; import android.view.ViewGroup; -import android.widget.EditText; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; -import java.util.HashMap; import java.util.LinkedHashMap; import ru.volgorobot.vrcatalog.ItemPresenter; @@ -29,11 +31,14 @@ public class DetailActivity extends AppCompatActivity implements MainContract.It private MainContract.ItemPresenter mPresenter; private ViewPager viewPager; private ListView propertiesList; + private ImagePagerAdapter imagePagerAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_detail); + Toolbar toolbar = findViewById(R.id.itemactivity_toolbar); + setSupportActionBar(toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setHomeButtonEnabled(true); getSupportActionBar().setDisplayShowTitleEnabled(false); @@ -135,11 +140,102 @@ public class DetailActivity extends AppCompatActivity implements MainContract.It @Override 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); viewPager.setAdapter(imagePagerAdapter); + setViewPagerScrollListener(); tabLayout.setupWithViewPager(viewPager); 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; + } + + } } diff --git a/app/src/main/res/layout/activity_detail.xml b/app/src/main/res/layout/activity_detail.xml index d7f98b3..b57713f 100644 --- a/app/src/main/res/layout/activity_detail.xml +++ b/app/src/main/res/layout/activity_detail.xml @@ -5,7 +5,9 @@ android:layout_width="match_parent" android:layout_height="wrap_content"> - @@ -27,7 +29,7 @@ - + --> + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values-v21/styles.xml b/app/src/main/res/values-v21/styles.xml index a005d20..1e3a2a3 100644 --- a/app/src/main/res/values-v21/styles.xml +++ b/app/src/main/res/values-v21/styles.xml @@ -5,16 +5,13 @@ true @android:color/transparent -