From d87c95b7b28d3db35e02d4479570c3d33d53ea9c Mon Sep 17 00:00:00 2001 From: h4h13 Date: Thu, 16 Apr 2020 23:43:12 +0530 Subject: [PATCH] Replayce adapterposition to layoutposition --- .../monkey/retromusic/adapter/GenreAdapter.kt | 2 +- .../retromusic/adapter/SearchAdapter.kt | 4 +- .../retromusic/adapter/SongFileAdapter.kt | 6 +- .../adapter/album/AlbumFullWidthAdapter.kt | 4 +- .../adapter/artist/ArtistAdapter.kt | 8 +- .../adapter/playlist/PlaylistAdapter.kt | 20 +- .../adapter/song/AbsOffsetSongAdapter.kt | 8 +- .../adapter/song/PlayingQueueAdapter.kt | 2 +- .../retromusic/adapter/song/SongAdapter.kt | 8 +- .../mainactivity/PlaylistsFragment.kt | 4 +- .../util/AutoGeneratedPlaylistBitmap.java | 12 +- .../monkey/retromusic/util/BitmapEditor.java | 340 +++++++++--------- .../retromusic/util/MergedImageUtils.kt | 121 +++++++ .../retromusic/util/SwipeAndDragHelper.java | 2 +- 14 files changed, 342 insertions(+), 199 deletions(-) create mode 100644 app/src/main/java/code/name/monkey/retromusic/util/MergedImageUtils.kt diff --git a/app/src/main/java/code/name/monkey/retromusic/adapter/GenreAdapter.kt b/app/src/main/java/code/name/monkey/retromusic/adapter/GenreAdapter.kt index d83516f1..570912a3 100644 --- a/app/src/main/java/code/name/monkey/retromusic/adapter/GenreAdapter.kt +++ b/app/src/main/java/code/name/monkey/retromusic/adapter/GenreAdapter.kt @@ -49,7 +49,7 @@ class GenreAdapter( inner class ViewHolder(itemView: View) : MediaEntryViewHolder(itemView) { override fun onClick(v: View?) { super.onClick(v) - val genre = dataSet[adapterPosition] + val genre = dataSet[layoutPosition] NavigationUtil.goToGenre(activity, genre) } } diff --git a/app/src/main/java/code/name/monkey/retromusic/adapter/SearchAdapter.kt b/app/src/main/java/code/name/monkey/retromusic/adapter/SearchAdapter.kt index 00255a13..0b257978 100644 --- a/app/src/main/java/code/name/monkey/retromusic/adapter/SearchAdapter.kt +++ b/app/src/main/java/code/name/monkey/retromusic/adapter/SearchAdapter.kt @@ -113,7 +113,7 @@ class SearchAdapter( menu?.visibility = View.VISIBLE menu?.setOnClickListener(object : SongMenuHelper.OnClickSongMenu(activity) { override val song: Song - get() = dataSet!![adapterPosition] as Song + get() = dataSet!![layoutPosition] as Song }) } else { menu?.visibility = View.GONE @@ -130,7 +130,7 @@ class SearchAdapter( } override fun onClick(v: View?) { - val item = dataSet!![adapterPosition] + val item = dataSet!![layoutPosition] when (itemViewType) { ALBUM -> { val options = ActivityOptions.makeSceneTransitionAnimation( diff --git a/app/src/main/java/code/name/monkey/retromusic/adapter/SongFileAdapter.kt b/app/src/main/java/code/name/monkey/retromusic/adapter/SongFileAdapter.kt index 9f139adc..4909ad37 100644 --- a/app/src/main/java/code/name/monkey/retromusic/adapter/SongFileAdapter.kt +++ b/app/src/main/java/code/name/monkey/retromusic/adapter/SongFileAdapter.kt @@ -146,7 +146,7 @@ class SongFileAdapter( init { if (menu != null && callbacks != null) { menu?.setOnClickListener { v -> - val position = adapterPosition + val position = layoutPosition if (isPositionInRange(position)) { callbacks.onFileMenuClicked(dataSet[position], v) } @@ -158,7 +158,7 @@ class SongFileAdapter( } override fun onClick(v: View?) { - val position = adapterPosition + val position = layoutPosition if (isPositionInRange(position)) { if (isInQuickSelectMode) { toggleChecked(position) @@ -169,7 +169,7 @@ class SongFileAdapter( } override fun onLongClick(v: View?): Boolean { - val position = adapterPosition + val position = layoutPosition return isPositionInRange(position) && toggleChecked(position) } diff --git a/app/src/main/java/code/name/monkey/retromusic/adapter/album/AlbumFullWidthAdapter.kt b/app/src/main/java/code/name/monkey/retromusic/adapter/album/AlbumFullWidthAdapter.kt index 08bb5736..8c3b9df8 100644 --- a/app/src/main/java/code/name/monkey/retromusic/adapter/album/AlbumFullWidthAdapter.kt +++ b/app/src/main/java/code/name/monkey/retromusic/adapter/album/AlbumFullWidthAdapter.kt @@ -97,9 +97,9 @@ class AlbumFullWidthAdapter( val activityOptions = ActivityOptions.makeSceneTransitionAnimation( activity, imageContainerCard ?: image, - "${activity.getString(R.string.transition_album_art)}_${dataSet[adapterPosition].id}" + "${activity.getString(R.string.transition_album_art)}_${dataSet[layoutPosition].id}" ) - NavigationUtil.goToAlbumOptions(activity, dataSet[adapterPosition].id, activityOptions) + NavigationUtil.goToAlbumOptions(activity, dataSet[layoutPosition].id, activityOptions) } } } \ No newline at end of file diff --git a/app/src/main/java/code/name/monkey/retromusic/adapter/artist/ArtistAdapter.kt b/app/src/main/java/code/name/monkey/retromusic/adapter/artist/ArtistAdapter.kt index ae566b65..e54aa833 100644 --- a/app/src/main/java/code/name/monkey/retromusic/adapter/artist/ArtistAdapter.kt +++ b/app/src/main/java/code/name/monkey/retromusic/adapter/artist/ArtistAdapter.kt @@ -144,21 +144,21 @@ class ArtistAdapter( override fun onClick(v: View?) { super.onClick(v) if (isInQuickSelectMode) { - toggleChecked(adapterPosition) + toggleChecked(layoutPosition) } else { val activityOptions = ActivityOptions.makeSceneTransitionAnimation( activity, imageContainerCard ?: image, - "${activity.getString(R.string.transition_artist_image)}_${dataSet[adapterPosition].id}" + "${activity.getString(R.string.transition_artist_image)}_${dataSet[layoutPosition].id}" ) NavigationUtil.goToArtistOptions( - activity, dataSet[adapterPosition].id, activityOptions + activity, dataSet[layoutPosition].id, activityOptions ) } } override fun onLongClick(v: View?): Boolean { - toggleChecked(adapterPosition) + toggleChecked(layoutPosition) return super.onLongClick(v) } } diff --git a/app/src/main/java/code/name/monkey/retromusic/adapter/playlist/PlaylistAdapter.kt b/app/src/main/java/code/name/monkey/retromusic/adapter/playlist/PlaylistAdapter.kt index 146f41d2..a550a1fb 100755 --- a/app/src/main/java/code/name/monkey/retromusic/adapter/playlist/PlaylistAdapter.kt +++ b/app/src/main/java/code/name/monkey/retromusic/adapter/playlist/PlaylistAdapter.kt @@ -33,6 +33,7 @@ import code.name.monkey.retromusic.model.smartplaylist.LastAddedPlaylist import code.name.monkey.retromusic.util.AutoGeneratedPlaylistBitmap import code.name.monkey.retromusic.util.MusicUtil import code.name.monkey.retromusic.util.NavigationUtil +import code.name.monkey.retromusic.util.RetroColorUtil import java.util.* class PlaylistAdapter( @@ -89,7 +90,7 @@ class PlaylistAdapter( } else { holder.menu?.show() } - PlaylistBitmapLoader(this, holder, playlist).execute() + //PlaylistBitmapLoader(this, holder, playlist).execute() } private fun getIconRes(playlist: Playlist): Drawable { @@ -182,7 +183,7 @@ class PlaylistAdapter( } menu?.setOnClickListener { view -> - val playlist = dataSet[adapterPosition] + val playlist = dataSet[layoutPosition] val popupMenu = PopupMenu(activity, view) popupMenu.inflate( if (itemViewType == SMART_PLAYLIST) R.menu.menu_item_smart_playlist @@ -202,7 +203,7 @@ class PlaylistAdapter( } } PlaylistMenuHelper.handleMenuClick( - activity, dataSet[adapterPosition], item + activity, dataSet[layoutPosition], item ) } popupMenu.show() @@ -216,15 +217,15 @@ class PlaylistAdapter( override fun onClick(v: View?) { if (isInQuickSelectMode) { - toggleChecked(adapterPosition) + toggleChecked(layoutPosition) } else { - val playlist = dataSet[adapterPosition] + val playlist = dataSet[layoutPosition] NavigationUtil.goToPlaylistNew(activity, playlist) } } override fun onLongClick(v: View?): Boolean { - toggleChecked(adapterPosition) + toggleChecked(layoutPosition) return true } } @@ -243,6 +244,13 @@ class PlaylistAdapter( override fun onPostExecute(result: Bitmap?) { super.onPostExecute(result) viewHolder.image?.setImageBitmap(result) + val color = RetroColorUtil.getColor( + RetroColorUtil.generatePalette( + result + ), + ATHUtil.resolveColor(adapter.activity, R.attr.colorSurface) + ) + viewHolder.paletteColorContainer?.setBackgroundColor(color) } } diff --git a/app/src/main/java/code/name/monkey/retromusic/adapter/song/AbsOffsetSongAdapter.kt b/app/src/main/java/code/name/monkey/retromusic/adapter/song/AbsOffsetSongAdapter.kt index 89d3e511..d1141341 100644 --- a/app/src/main/java/code/name/monkey/retromusic/adapter/song/AbsOffsetSongAdapter.kt +++ b/app/src/main/java/code/name/monkey/retromusic/adapter/song/AbsOffsetSongAdapter.kt @@ -55,19 +55,19 @@ abstract class AbsOffsetSongAdapter( override // could also return null, just to be safe return empty song val song: Song - get() = if (itemViewType == OFFSET_ITEM) Song.emptySong else dataSet[adapterPosition - 1] + get() = if (itemViewType == OFFSET_ITEM) Song.emptySong else dataSet[layoutPosition - 1] override fun onClick(v: View?) { if (isInQuickSelectMode && itemViewType != OFFSET_ITEM) { - toggleChecked(adapterPosition) + toggleChecked(layoutPosition) } else { - MusicPlayerRemote.openQueue(dataSet, adapterPosition - 1, true) + MusicPlayerRemote.openQueue(dataSet, layoutPosition - 1, true) } } override fun onLongClick(v: View?): Boolean { if (itemViewType == OFFSET_ITEM) return false - toggleChecked(adapterPosition) + toggleChecked(layoutPosition) return true } } diff --git a/app/src/main/java/code/name/monkey/retromusic/adapter/song/PlayingQueueAdapter.kt b/app/src/main/java/code/name/monkey/retromusic/adapter/song/PlayingQueueAdapter.kt index 04a998e6..deba43f3 100644 --- a/app/src/main/java/code/name/monkey/retromusic/adapter/song/PlayingQueueAdapter.kt +++ b/app/src/main/java/code/name/monkey/retromusic/adapter/song/PlayingQueueAdapter.kt @@ -158,7 +158,7 @@ class PlayingQueueAdapter( override fun onSongMenuItemClick(item: MenuItem): Boolean { when (item.itemId) { R.id.action_remove_from_playing_queue -> { - removeFromQueue(adapterPosition) + removeFromQueue(layoutPosition) return true } } diff --git a/app/src/main/java/code/name/monkey/retromusic/adapter/song/SongAdapter.kt b/app/src/main/java/code/name/monkey/retromusic/adapter/song/SongAdapter.kt index a39e10b8..17b06be8 100644 --- a/app/src/main/java/code/name/monkey/retromusic/adapter/song/SongAdapter.kt +++ b/app/src/main/java/code/name/monkey/retromusic/adapter/song/SongAdapter.kt @@ -150,7 +150,7 @@ open class SongAdapter( open inner class ViewHolder(itemView: View) : MediaEntryViewHolder(itemView) { protected open var songMenuRes = SongMenuHelper.MENU_RES protected open val song: Song - get() = dataSet[adapterPosition] + get() = dataSet[layoutPosition] init { setImageTransitionName(activity.getString(R.string.transition_album_art)) @@ -186,14 +186,14 @@ open class SongAdapter( override fun onClick(v: View?) { if (isInQuickSelectMode) { - toggleChecked(adapterPosition) + toggleChecked(layoutPosition) } else { - MusicPlayerRemote.openQueue(dataSet, adapterPosition, true) + MusicPlayerRemote.openQueue(dataSet, layoutPosition, true) } } override fun onLongClick(v: View?): Boolean { - return toggleChecked(adapterPosition) + return toggleChecked(layoutPosition) } } diff --git a/app/src/main/java/code/name/monkey/retromusic/fragments/mainactivity/PlaylistsFragment.kt b/app/src/main/java/code/name/monkey/retromusic/fragments/mainactivity/PlaylistsFragment.kt index 7472625b..8aaf99fd 100644 --- a/app/src/main/java/code/name/monkey/retromusic/fragments/mainactivity/PlaylistsFragment.kt +++ b/app/src/main/java/code/name/monkey/retromusic/fragments/mainactivity/PlaylistsFragment.kt @@ -40,14 +40,14 @@ class PlaylistsFragment : } override fun createLayoutManager(): GridLayoutManager { - return GridLayoutManager(activity, 2) + return GridLayoutManager(requireContext(), 1) } override fun createAdapter(): PlaylistAdapter { return PlaylistAdapter( mainActivity, ArrayList(), - R.layout.item_card, + R.layout.item_list, mainActivity ) } diff --git a/app/src/main/java/code/name/monkey/retromusic/util/AutoGeneratedPlaylistBitmap.java b/app/src/main/java/code/name/monkey/retromusic/util/AutoGeneratedPlaylistBitmap.java index 9d793eca..1e5764c7 100644 --- a/app/src/main/java/code/name/monkey/retromusic/util/AutoGeneratedPlaylistBitmap.java +++ b/app/src/main/java/code/name/monkey/retromusic/util/AutoGeneratedPlaylistBitmap.java @@ -48,6 +48,8 @@ public class AutoGeneratedPlaylistBitmap { if (bitmap != null) art.add(bitmap); if (art.size() == 6) break; } + return MergedImageUtils.INSTANCE.joinImages(art); + /* long start3 = System.currentTimeMillis() - start2 - start; Bitmap ret; @@ -71,7 +73,7 @@ public class AutoGeneratedPlaylistBitmap { return BitmapEditor.GetRoundedBitmapWithBlurShadow(context, ret, w / 24, w / 24, w / 24, w / 24, 0, 200, w / 40, 1); Log.d(TAG, "getBitmap: time = " + (System.currentTimeMillis() - start) + ", start2 = " + start2 + ", start3 = " + start3); - return ret; + return ret;*/ } private static Bitmap getBitmapCollection(ArrayList art, boolean round) { @@ -177,14 +179,6 @@ public class AutoGeneratedPlaylistBitmap { } catch (Exception e) { return null; } - - /* try { - return Picasso.get().load(Util.getAlbumArtUri(id)).get(); - } catch (Exception e) { - return null; - }*/ - - // return ImageLoader.getInstance().loadImageSync(Util.getAlbumArtUri(song.albumId).toString()); } public static Bitmap getDefaultBitmap(@NonNull Context context, boolean round) { diff --git a/app/src/main/java/code/name/monkey/retromusic/util/BitmapEditor.java b/app/src/main/java/code/name/monkey/retromusic/util/BitmapEditor.java index 78edbb2c..1329e9ef 100644 --- a/app/src/main/java/code/name/monkey/retromusic/util/BitmapEditor.java +++ b/app/src/main/java/code/name/monkey/retromusic/util/BitmapEditor.java @@ -75,7 +75,7 @@ public final class BitmapEditor { int h = bitmap.getHeight(); int[] pix = new int[w * h]; - // Log.e("pix", w + " " + h + " " + pix.length); + // Log.e("pix", w + " " + h + " " + pix.length); bitmap.getPixels(pix, 0, w, 0, 0, w, h); int wm = w - 1; @@ -286,16 +286,16 @@ public final class BitmapEditor { } } - // Log.e("pix", w + " " + h + " " + pix.length); + // Log.e("pix", w + " " + h + " " + pix.length); bitmap.setPixels(pix, 0, w, 0, 0, w, h); return (bitmap); } - public static boolean PerceivedBrightness( int will_White, int[] c) - { - double TBT= Math.sqrt(c[0] * c[0] * .241 +c[1] * c[1] * .691 +c[2] * c[2] * .068); - // Log.d("themee",TBT+""); - return (TBT > will_White) ? false: true; + + public static boolean PerceivedBrightness(int will_White, int[] c) { + double TBT = Math.sqrt(c[0] * c[0] * .241 + c[1] * c[1] * .691 + c[2] * c[2] * .068); + // Log.d("themee",TBT+""); + return (TBT > will_White) ? false : true; } public static int[] getAverageColorRGB(Bitmap bitmap) { @@ -320,10 +320,11 @@ public final class BitmapEditor { r /= size; g /= size; b /= size; - return new int[] { + return new int[]{ r, g, b }; } + public static Bitmap updateSat(Bitmap src, float settingSat) { int w = src.getWidth(); @@ -342,21 +343,22 @@ public final class BitmapEditor { canvasResult = null; return bitmapResult; } + /** * Stack Blur v1.0 from * http://www.quasimondo.com/StackBlurForCanvas/StackBlurDemo.html * Java Author: Mario Klingemann * http://incubator.quasimondo.com - * + *

* created Feburary 29, 2004 * Android port : Yahel Bouaziz * http://www.kayenko.com * ported april 5th, 2012 - * + *

* This is A compromise between Gaussian Blur and Box blur * It creates much better looking blurs than Box Blur, but is * 7x faster than my Gaussian Blur implementation. - * + *

* I called it Stack Blur because this describes best how this * filter works internally: it creates A kind of moving stack * of colors whilst scanning through the image. Thereby it @@ -365,7 +367,7 @@ public final class BitmapEditor { * colors on the topmost layer of the stack are either added on * or reduced by one, depending on if they are on the right or * on the x side of the stack. - * + *

* If you are using this algorithm in your code please add * the following line: * Stack Blur Algorithm by Mario Klingemann @@ -375,21 +377,19 @@ public final class BitmapEditor { Bitmap afterscaleSentBitmap; Bitmap bitmap; - if(scale!=1) { + if (scale != 1) { int width = Math.round(sentBitmap.getWidth() * scale); //lấy chiều rộng làm tròn int height = Math.round(sentBitmap.getHeight() * scale); // lấy chiều cao làm tròn afterscaleSentBitmap = Bitmap.createScaledBitmap(sentBitmap, width, height, false); // tạo bitmap scaled bitmap = afterscaleSentBitmap.copy(afterscaleSentBitmap.getConfig(), true); afterscaleSentBitmap.recycle(); + } else { + bitmap = sentBitmap.copy(sentBitmap.getConfig(), true); // đơn giản chỉ copy } - else { - bitmap = sentBitmap.copy(sentBitmap.getConfig(),true); // đơn giản chỉ copy - } - if (radius < 1) { - return (sentBitmap.copy(sentBitmap.getConfig(),true)); + return (sentBitmap.copy(sentBitmap.getConfig(), true)); } int w = bitmap.getWidth(); // w is the width of sample bitmap @@ -537,7 +537,7 @@ public final class BitmapEditor { stackpointer = radius; for (y = 0; y < h; y++) { // Preserve alpha channel: ( 0xff000000 & pix[yi] ) - pix[yi] = ( 0xff000000 & pix[yi] ) | ( dv[rsum] << 16 ) | ( dv[gsum] << 8 ) | dv[bsum]; + pix[yi] = (0xff000000 & pix[yi]) | (dv[rsum] << 16) | (dv[gsum] << 8) | dv[bsum]; rsum -= routsum; gsum -= goutsum; @@ -596,25 +596,28 @@ public final class BitmapEditor { final int color = 0xff424242; final Paint paint = new Paint(); final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); - // final ScreenSize rectF = new ScreenSize(rect); + // final ScreenSize rectF = new ScreenSize(rect); final float roundPx = pixels; paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); - // canvas.drawRoundRect(rectF, roundPx, roundPx, paint); - canvas.drawPath(BitmapEditor.RoundedRect(0,0,bitmap.getWidth(),bitmap.getHeight(),roundPx,roundPx,false),paint); + // canvas.drawRoundRect(rectF, roundPx, roundPx, paint); + canvas.drawPath(BitmapEditor.RoundedRect(0, 0, bitmap.getWidth(), bitmap.getHeight(), roundPx, roundPx, false), paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint); return output; } - /** getResizedBitmap method is used to Resized the Image according to custom width and height + + /** + * getResizedBitmap method is used to Resized the Image according to custom width and height + * * @param image * @param newHeight (new desired height) - * @param newWidth (new desired Width) + * @param newWidth (new desired Width) * @return image (new resized image) - * */ + */ public static Bitmap getResizedBitmap(Bitmap image, int newHeight, int newWidth) { int width = image.getWidth(); int height = image.getHeight(); @@ -629,93 +632,95 @@ public final class BitmapEditor { matrix, false); return resizedBitmap; } - public static boolean TrueIfBitmapBigger(Bitmap bitmap, int size) - { - int sizeBitmap= (bitmap.getHeight()>bitmap.getWidth()) ? bitmap.getHeight() : bitmap.getWidth(); - if(sizeBitmap>size) return true; + + public static boolean TrueIfBitmapBigger(Bitmap bitmap, int size) { + int sizeBitmap = (bitmap.getHeight() > bitmap.getWidth()) ? bitmap.getHeight() : bitmap.getWidth(); + if (sizeBitmap > size) return true; return false; } + public static Bitmap GetRoundedBitmapWithBlurShadow(Bitmap original, int paddingTop, int paddingBottom, int paddingLeft, int paddingRight) { - int original_width= original.getWidth(); - int orginal_height= original.getHeight(); - int bitmap_width= original_width+paddingLeft+paddingRight; - int bitmap_height= orginal_height+ paddingTop+paddingBottom; - Bitmap bitmap = Bitmap.createBitmap(bitmap_width,bitmap_height,Bitmap.Config.ARGB_8888); + int original_width = original.getWidth(); + int orginal_height = original.getHeight(); + int bitmap_width = original_width + paddingLeft + paddingRight; + int bitmap_height = orginal_height + paddingTop + paddingBottom; + Bitmap bitmap = Bitmap.createBitmap(bitmap_width, bitmap_height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); Paint paint = new Paint(); paint.setStyle(Paint.Style.FILL); //paint.setAlpha(60); - // canvas.drawRect(0,0,bitmap_width,bitmap_height,paint); + // canvas.drawRect(0,0,bitmap_width,bitmap_height,paint); paint.setAntiAlias(true); - canvas.drawBitmap(original,paddingLeft,paddingTop,paint); - Bitmap blurred_bitmap= getBlurredWithGoodPerformance(bitmap,1,6,4); + canvas.drawBitmap(original, paddingLeft, paddingTop, paint); + Bitmap blurred_bitmap = getBlurredWithGoodPerformance(bitmap, 1, 6, 4); canvas.setBitmap(null); bitmap.recycle(); return blurred_bitmap; } - // Activity. - // | Original bitmap. - // | | To make the blur background, the original must to padding. - // | | | | | | + // Activity. + // | Original bitmap. + // | | To make the blur background, the original must to padding. + // | | | | | | // V V V V V V public static Bitmap GetRoundedBitmapWithBlurShadow(Context context, Bitmap original, int paddingTop, int paddingBottom, int paddingLeft, int paddingRight, int TopBack // this value makes the overview bitmap is higher or belower the background. - , int alphaBlurBackground // this is the alpha of the background Bitmap, you need A number between 0 -> 255, the value recommend is 180. - , int valueBlurBackground // this is the value used to blur the background Bitmap, the recommended one is 12. - , int valueSaturationBlurBackground // this is the value used to background Bitmap more colorful, if valueBlur is 12, the valudeSaturation should be 2. + , int alphaBlurBackground // this is the alpha of the background Bitmap, you need A number between 0 -> 255, the value recommend is 180. + , int valueBlurBackground // this is the value used to blur the background Bitmap, the recommended one is 12. + , int valueSaturationBlurBackground // this is the value used to background Bitmap more colorful, if valueBlur is 12, the valudeSaturation should be 2. ) { - int original_width= original.getWidth(); - int orginal_height= original.getHeight(); - int bitmap_width= original_width+paddingLeft+paddingRight; - int bitmap_height= orginal_height+ paddingTop+paddingBottom; - Bitmap bitmap = Bitmap.createBitmap(bitmap_width,bitmap_height,Bitmap.Config.ARGB_8888); + int original_width = original.getWidth(); + int orginal_height = original.getHeight(); + int bitmap_width = original_width + paddingLeft + paddingRight; + int bitmap_height = orginal_height + paddingTop + paddingBottom; + Bitmap bitmap = Bitmap.createBitmap(bitmap_width, bitmap_height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); Paint paint = new Paint(); paint.setStyle(Paint.Style.FILL); paint.setAntiAlias(true); - canvas.drawBitmap(original,paddingLeft,paddingTop,paint); - Bitmap blurred_bitmap= getBlurredWithGoodPerformance(context, bitmap,1,valueBlurBackground,valueSaturationBlurBackground); - // Bitmap blurred_bitmap= getBlurredWithGoodPerformance(context, bitmap,1,15,3); - Bitmap end_bitmap = Bitmap.createBitmap(bitmap_width,bitmap_height, Bitmap.Config.ARGB_8888); + canvas.drawBitmap(original, paddingLeft, paddingTop, paint); + Bitmap blurred_bitmap = getBlurredWithGoodPerformance(context, bitmap, 1, valueBlurBackground, valueSaturationBlurBackground); + // Bitmap blurred_bitmap= getBlurredWithGoodPerformance(context, bitmap,1,15,3); + Bitmap end_bitmap = Bitmap.createBitmap(bitmap_width, bitmap_height, Bitmap.Config.ARGB_8888); canvas.setBitmap(end_bitmap); paint.setAlpha(alphaBlurBackground); - canvas.drawBitmap(blurred_bitmap,new Rect(0,0,blurred_bitmap.getWidth(),blurred_bitmap.getHeight()),new Rect(0,0,bitmap_width,bitmap_height),paint); + canvas.drawBitmap(blurred_bitmap, new Rect(0, 0, blurred_bitmap.getWidth(), blurred_bitmap.getHeight()), new Rect(0, 0, bitmap_width, bitmap_height), paint); paint.setAlpha(255); - canvas.drawBitmap(bitmap,0,TopBack,paint); // drawVisualWave cái lớn - canvas.setBitmap(null); + canvas.drawBitmap(bitmap, 0, TopBack, paint); // drawVisualWave cái lớn + canvas.setBitmap(null); blurred_bitmap.recycle(); - bitmap.recycle(); + bitmap.recycle(); return end_bitmap; } - public static void setBitmapforImageView(ImageView imv,Bitmap apply) - { - Bitmap old = ((BitmapDrawable)imv.getDrawable()).getBitmap(); + + public static void setBitmapforImageView(ImageView imv, Bitmap apply) { + Bitmap old = ((BitmapDrawable) imv.getDrawable()).getBitmap(); imv.setImageBitmap(apply); - if(old!=null) - old.recycle(); + if (old != null) + old.recycle(); } - public static Bitmap getBlurredWithGoodPerformance(Bitmap bitmap,int scale,int radius,int saturation) - { + + public static Bitmap getBlurredWithGoodPerformance(Bitmap bitmap, int scale, int radius, int saturation) { BitmapFactory.Options options = new BitmapFactory.Options(); - Bitmap bitmap1=getResizedBitmap(bitmap,50,50); - Bitmap updateSatBitmap = updateSat(bitmap1,saturation); - Bitmap blurredBitmap= FastBlurSupportAlpha(updateSatBitmap,scale,radius); + Bitmap bitmap1 = getResizedBitmap(bitmap, 50, 50); + Bitmap updateSatBitmap = updateSat(bitmap1, saturation); + Bitmap blurredBitmap = FastBlurSupportAlpha(updateSatBitmap, scale, radius); updateSatBitmap.recycle(); bitmap1.recycle(); return blurredBitmap; } + static public Path RoundedRect(float left, float top, float right, float bottom, float rx, float ry, boolean conformToOriginalPost) { Path path = new Path(); if (rx < 0) rx = 0; if (ry < 0) ry = 0; float width = right - left; float height = bottom - top; - if (rx > width/2) rx = width/2; - if (ry > height/2) ry = height/2; + if (rx > width / 2) rx = width / 2; + if (ry > height / 2) ry = height / 2; float widthMinusCorners = (width - (2 * rx)); // do dai phan "thang" cua chieu rong float heightMinusCorners = (height - (2 * ry)); // do dai phan "thang" cua chieu dai @@ -729,8 +734,7 @@ public final class BitmapEditor { path.rLineTo(0, ry); path.rLineTo(width, 0); path.rLineTo(0, -ry); - } - else { + } else { path.rQuadTo(0, ry, rx, ry);//bottom-x corner path.rLineTo(widthMinusCorners, 0); @@ -743,38 +747,37 @@ public final class BitmapEditor { return path; } - public static int mixTwoColors( int color1, int color2, float amount ) - { + + public static int mixTwoColors(int color1, int color2, float amount) { final byte ALPHA_CHANNEL = 24; - final byte RED_CHANNEL = 16; - final byte GREEN_CHANNEL = 8; - final byte BLUE_CHANNEL = 0; + final byte RED_CHANNEL = 16; + final byte GREEN_CHANNEL = 8; + final byte BLUE_CHANNEL = 0; final float inverseAmount = 1.0f - amount; - int a = ((int)(((float)(color1 >> ALPHA_CHANNEL & 0xff )*amount) + - ((float)(color2 >> ALPHA_CHANNEL & 0xff )*inverseAmount))) & 0xff; - int r = ((int)(((float)(color1 >> RED_CHANNEL & 0xff )*amount) + - ((float)(color2 >> RED_CHANNEL & 0xff )*inverseAmount))) & 0xff; - int g = ((int)(((float)(color1 >> GREEN_CHANNEL & 0xff )*amount) + - ((float)(color2 >> GREEN_CHANNEL & 0xff )*inverseAmount))) & 0xff; - int b = ((int)(((float)(color1 & 0xff )*amount) + - ((float)(color2 & 0xff )*inverseAmount))) & 0xff; + int a = ((int) (((float) (color1 >> ALPHA_CHANNEL & 0xff) * amount) + + ((float) (color2 >> ALPHA_CHANNEL & 0xff) * inverseAmount))) & 0xff; + int r = ((int) (((float) (color1 >> RED_CHANNEL & 0xff) * amount) + + ((float) (color2 >> RED_CHANNEL & 0xff) * inverseAmount))) & 0xff; + int g = ((int) (((float) (color1 >> GREEN_CHANNEL & 0xff) * amount) + + ((float) (color2 >> GREEN_CHANNEL & 0xff) * inverseAmount))) & 0xff; + int b = ((int) (((float) (color1 & 0xff) * amount) + + ((float) (color2 & 0xff) * inverseAmount))) & 0xff; return a << ALPHA_CHANNEL | r << RED_CHANNEL | g << GREEN_CHANNEL | b << BLUE_CHANNEL; } - public static Bitmap getBlurredWithGoodPerformance(Context context,Bitmap bitmap,int scale,int radius,float saturation) - { - Bitmap bitmap1=getResizedBitmap(bitmap,150,150); - Bitmap updateSatBimap = updateSat(bitmap1,saturation); - Bitmap blurredBitmap= BlurBitmapWithRenderScript(context,updateSatBimap,radius); + + public static Bitmap getBlurredWithGoodPerformance(Context context, Bitmap bitmap, int scale, int radius, float saturation) { + Bitmap bitmap1 = getResizedBitmap(bitmap, 150, 150); + Bitmap updateSatBimap = updateSat(bitmap1, saturation); + Bitmap blurredBitmap = BlurBitmapWithRenderScript(context, updateSatBimap, radius); updateSatBimap.recycle(); bitmap1.recycle(); return blurredBitmap; } - public static Bitmap getBlurredBimapWithRenderScript(Context context, Bitmap bitmapOriginal,float radius) - { + public static Bitmap getBlurredBimapWithRenderScript(Context context, Bitmap bitmapOriginal, float radius) { //define this only once if blurring multiple times RenderScript rs = RenderScript.create(context); @@ -788,7 +791,8 @@ public final class BitmapEditor { output.copyTo(bitmapOriginal); return bitmapOriginal; } - public static Bitmap BlurBitmapWithRenderScript(Context context, Bitmap bitmap, float radius){ + + public static Bitmap BlurBitmapWithRenderScript(Context context, Bitmap bitmap, float radius) { //Let's create an empty bitmap with the same size of the bitmap we want to blur Bitmap outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888); @@ -838,11 +842,11 @@ public final class BitmapEditor { drawable.draw(canvas); return bitmap; } - public static Bitmap changeBitmapColor(Bitmap sourceBitmap, int color) - { - Bitmap resultBitmap = sourceBitmap.copy(sourceBitmap.getConfig(),true); + + public static Bitmap changeBitmapColor(Bitmap sourceBitmap, int color) { + Bitmap resultBitmap = sourceBitmap.copy(sourceBitmap.getConfig(), true); Paint paint = new Paint(); - ColorFilter filter = new PorterDuffColorFilter(color,PorterDuff.Mode.SRC_IN); + ColorFilter filter = new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_IN); paint.setColorFilter(filter); Canvas canvas = new Canvas(resultBitmap); canvas.drawBitmap(resultBitmap, 0, 0, paint); @@ -850,75 +854,90 @@ public final class BitmapEditor { } /** - * * @param mode - * @return - * 0 : CLEAR - *
1 : SRC - *
2 : DST - *
3 : SRC_OVER - *
4 : DST_OVER - *
5 : SRC_IN - *
6 : DST_IN - *
7 : SRC_OUT - *
8 : DST_OUT - *
9 : SRC_ATOP - *
10 : DST_ATOP - *
11 : XOR - *
12 : ADD - *
13 : MULTIPLY - *
14 : SCREEN - *
15 : OVERLAY - *
16 : DARKEN - *
17 : LIGHTEN + * @return 0 : CLEAR + *
1 : SRC + *
2 : DST + *
3 : SRC_OVER + *
4 : DST_OVER + *
5 : SRC_IN + *
6 : DST_IN + *
7 : SRC_OUT + *
8 : DST_OUT + *
9 : SRC_ATOP + *
10 : DST_ATOP + *
11 : XOR + *
12 : ADD + *
13 : MULTIPLY + *
14 : SCREEN + *
15 : OVERLAY + *
16 : DARKEN + *
17 : LIGHTEN */ - public static PorterDuff.Mode getPorterMode(int mode) - { + public static PorterDuff.Mode getPorterMode(int mode) { switch (mode) { default: - case 0: return PorterDuff.Mode.CLEAR; - case 1: return PorterDuff.Mode.SRC; - case 2: return PorterDuff.Mode.DST; - case 3: return PorterDuff.Mode.SRC_OVER; - case 4: return PorterDuff.Mode.DST_OVER; - case 5: return PorterDuff.Mode.SRC_IN; - case 6: return PorterDuff.Mode.DST_IN; - case 7: return PorterDuff.Mode.SRC_OUT; - case 8: return PorterDuff.Mode.DST_OUT; - case 9: return PorterDuff.Mode.SRC_ATOP; - case 10: return PorterDuff.Mode.DST_ATOP; - case 11: return PorterDuff.Mode.XOR; - case 16: return PorterDuff.Mode.DARKEN; - case 17: return PorterDuff.Mode.LIGHTEN; - case 13: return PorterDuff.Mode.MULTIPLY; - case 14: return PorterDuff.Mode.SCREEN; - case 12: return PorterDuff.Mode.ADD; - case 15: return PorterDuff.Mode.OVERLAY; + case 0: + return PorterDuff.Mode.CLEAR; + case 1: + return PorterDuff.Mode.SRC; + case 2: + return PorterDuff.Mode.DST; + case 3: + return PorterDuff.Mode.SRC_OVER; + case 4: + return PorterDuff.Mode.DST_OVER; + case 5: + return PorterDuff.Mode.SRC_IN; + case 6: + return PorterDuff.Mode.DST_IN; + case 7: + return PorterDuff.Mode.SRC_OUT; + case 8: + return PorterDuff.Mode.DST_OUT; + case 9: + return PorterDuff.Mode.SRC_ATOP; + case 10: + return PorterDuff.Mode.DST_ATOP; + case 11: + return PorterDuff.Mode.XOR; + case 16: + return PorterDuff.Mode.DARKEN; + case 17: + return PorterDuff.Mode.LIGHTEN; + case 13: + return PorterDuff.Mode.MULTIPLY; + case 14: + return PorterDuff.Mode.SCREEN; + case 12: + return PorterDuff.Mode.ADD; + case 15: + return PorterDuff.Mode.OVERLAY; } } - public static void applyNewColor4Bitmap(Context context,int[] idBitmaps,ImageView[] imageViews,int color,float alpha) - { - android.content.res.Resources resource = context.getResources(); - int size= idBitmaps.length; - Bitmap usingBitmap,resultBitmap; - for(int i=0;i): Bitmap { + assertBackgroundThread() + + val arranged = arrangeBitmaps(list.shuffled()) + + val mergedImage = create( + arranged, + IMAGE_SIZE, + PARTS + ) + val finalImage = rotate( + mergedImage, + IMAGE_SIZE, + DEGREES + ) + mergedImage.recycle() + return finalImage + } + + private fun arrangeBitmaps(list: List): List { + return when { + list.size == 1 -> { + val item = list[0] + listOf(item, item, item, item, item, item, item, item, item) + } + list.size == 2 -> { + val item1 = list[0] + val item2 = list[1] + listOf(item1, item2, item1, item2, item1, item2, item1, item2, item1) + } + list.size == 3 -> { + val item1 = list[0] + val item2 = list[1] + val item3 = list[2] + listOf(item1, item2, item3, item3, item1, item2, item2, item3, item1) + } + list.size == 4 -> { + val item1 = list[0] + val item2 = list[1] + val item3 = list[2] + val item4 = list[3] + listOf(item1, item2, item3, item4, item1, item2, item3, item4, item1) + } + list.size < 9 -> { // 5 to 8 + val item1 = list[0] + val item2 = list[1] + val item3 = list[2] + val item4 = list[3] + val item5 = list[4] + listOf(item1, item2, item3, item4, item5, item2, item3, item4, item1) + } + else -> list // case 9 + } + } + + private fun create(images: List, imageSize: Int, parts: Int): Bitmap { + val result = Bitmap.createBitmap(imageSize, imageSize, Bitmap.Config.ARGB_8888) + val canvas = Canvas(result) + val paint = Paint(Paint.ANTI_ALIAS_FLAG) + val onePartSize = imageSize / parts + + images.forEachIndexed { i, bitmap -> + val bit = Bitmap.createScaledBitmap(bitmap, onePartSize, onePartSize, true) + canvas.drawBitmap( + bit, + (onePartSize * (i % parts)).toFloat(), + (onePartSize * (i / parts)).toFloat(), + paint + ) + bit.recycle() + } + + paint.color = Color.WHITE + paint.strokeWidth = 10f + + val oneThirdSize = (IMAGE_SIZE / 3).toFloat() + val twoThirdSize = (IMAGE_SIZE / 3 * 2).toFloat() + // vertical lines + canvas.drawLine(oneThirdSize, 0f, oneThirdSize, imageSize.toFloat(), paint) + canvas.drawLine(twoThirdSize, 0f, twoThirdSize, imageSize.toFloat(), paint) + // horizontal lines + canvas.drawLine(0f, oneThirdSize, imageSize.toFloat(), oneThirdSize, paint) + canvas.drawLine(0f, twoThirdSize, imageSize.toFloat(), twoThirdSize, paint) + + return result + } + + private fun rotate(bitmap: Bitmap, imageSize: Int, degrees: Float): Bitmap { + val matrix = Matrix() + matrix.postRotate(degrees) + + val rotated = Bitmap.createBitmap(bitmap, 0, 0, imageSize, imageSize, matrix, true) + bitmap.recycle() + val cropStart = imageSize * 25 / 100 + val cropEnd: Int = (cropStart * 1.5).toInt() + val cropped = Bitmap.createBitmap( + rotated, + cropStart, + cropStart, + imageSize - cropEnd, + imageSize - cropEnd + ) + rotated.recycle() + + return cropped + } + + +} \ No newline at end of file diff --git a/app/src/main/java/code/name/monkey/retromusic/util/SwipeAndDragHelper.java b/app/src/main/java/code/name/monkey/retromusic/util/SwipeAndDragHelper.java index a5f740db..1c82c7aa 100644 --- a/app/src/main/java/code/name/monkey/retromusic/util/SwipeAndDragHelper.java +++ b/app/src/main/java/code/name/monkey/retromusic/util/SwipeAndDragHelper.java @@ -36,7 +36,7 @@ public class SwipeAndDragHelper extends ItemTouchHelper.Callback { @Override public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) { - contract.onViewMoved(viewHolder.getAdapterPosition(), target.getAdapterPosition()); + contract.onViewMoved(viewHolder.getLayoutPosition(), target.getLayoutPosition()); return true; }