Skip to content

Commit

Permalink
CardView support added
Browse files Browse the repository at this point in the history
  • Loading branch information
musenkishi committed May 14, 2015
1 parent 37fbc6d commit 29eaf92
Show file tree
Hide file tree
Showing 27 changed files with 271 additions and 40 deletions.
11 changes: 0 additions & 11 deletions example/src/main/res/layout/activity_main.xml

This file was deleted.

6 changes: 0 additions & 6 deletions example/src/main/res/values/strings.xml

This file was deleted.

1 change: 1 addition & 0 deletions lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ android {
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:palette-v7:21.0.0'
compile 'com.android.support:cardview-v7:22.1.1'
compile 'org.apache.commons:commons-collections4:4.0'
}

Expand Down
39 changes: 23 additions & 16 deletions lib/src/main/java/com/musenkishi/paletteloader/PaletteLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import android.os.HandlerThread;
import android.os.Message;
import android.support.v7.graphics.Palette;
import android.support.v7.widget.CardView;
import android.util.Pair;
import android.view.View;
import android.widget.ImageView;
Expand Down Expand Up @@ -103,7 +104,7 @@ public boolean handleMessage(Message message) {

Message uiMessage = uiHandler.obtainMessage();
uiMessage.what = MSG_DISPLAY_PALETTE;
uiMessage.obj = new Pair<Palette, PaletteTarget>(palette, pair.second);
uiMessage.obj = new Pair<>(palette, pair.second);
uiMessage.arg1 = FALSE;

uiHandler.sendMessage(uiMessage);
Expand Down Expand Up @@ -185,7 +186,7 @@ public void into(View view) {
} else {
Message bgMessage = backgroundHandler.obtainMessage();
bgMessage.what = MSG_RENDER_PALETTE;
bgMessage.obj = new Pair<Bitmap, PaletteTarget>(bitmap, paletteTarget);
bgMessage.obj = new Pair<>(bitmap, paletteTarget);
backgroundHandler.sendMessage(bgMessage);
}
}
Expand All @@ -204,6 +205,9 @@ private static void applyColorToView(final PaletteTarget target, int color, bool
if (target.getView() instanceof TextView) {
applyColorToView((TextView) target.getView(), color, fromCache);
return;
} else if (target.getView() instanceof CardView) {
applyColorToCardView((CardView) target.getView(), color, fromCache);
return;
}
if (fromCache) {
if (target.getView() instanceof ImageView && target.shouldMaskDrawable()) {
Expand Down Expand Up @@ -291,6 +295,23 @@ public void onAnimationUpdate(ValueAnimator animator) {
}
}

private static void applyColorToCardView(final CardView cardView, int color, boolean fromCache) {
if (fromCache) {
cardView.setCardBackgroundColor(color);
} else {
Integer colorFrom = Color.parseColor("#FFFAFAFA"); //Default light CardView color.
Integer colorTo = color;
ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, colorTo);
colorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animator) {
cardView.setCardBackgroundColor((Integer) animator.getAnimatedValue());
}
});
colorAnimation.start();
}
}

/**
* Is it null? Is that null? What about that one? Is that null too? What about this one?
* And this one? Is this null? Is null even null? How can null be real if our eyes aren't real?
Expand Down Expand Up @@ -445,18 +466,4 @@ private static void callListener(Palette palette, OnPaletteRenderedListener onPa
onPaletteRenderedListener.onRendered(palette);
}
}

// /**
// * Will return a {@link android.support.v7.graphics.Palette} if it's available in the cache
// * @param id The URL or ID that was used to render the {@link android.support.v7.graphics.Palette}.
// * @return The desired {@link android.support.v7.graphics.Palette} if available, otherwise null.
// */
// public static Palette getPaletteFromCache(String id) {
// if (paletteCache.containsKey(id)){
// return paletteCache.get(id);
// } else {
// return null;
// }
// }

}
File renamed without changes.
6 changes: 5 additions & 1 deletion example/build.gradle → sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ android {
buildToolsVersion "22.0.1"

defaultConfig {
applicationId "com.musenkishi.paletteloader"
applicationId "com.musenkishi.paletteloader.sample"
minSdkVersion 15
targetSdkVersion 22
versionCode 1
Expand All @@ -21,6 +21,10 @@ android {

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:support-v4:22.1.1'
compile 'com.android.support:appcompat-v7:22.1.1'
compile 'com.android.support:recyclerview-v7:21.0.0'
compile 'com.android.support:cardview-v7:22.1.1'
compile 'com.github.bumptech.glide:glide:3.6.0'
compile project(':lib')
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.musenkishi.paletteloader" >
package="com.musenkishi.paletteloader.sample" >

<uses-permission android:name="android.permission.INTERNET"/>

<application
android:allowBackup="true"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.musenkishi.paletteloader.sample;

import java.util.ArrayList;
import java.util.Arrays;

/**
* Sample countries
* Created by frelus on 13/05/15.
*/
public class Countries {

public static final ArrayList<Country> countries = new ArrayList<>(Arrays.asList(new Country[]{
new Country("Sweden", "https://i.imgur.com/7VWkxxU.jpg"),
new Country("Greece", "https://i.imgur.com/sH1yrJD.jpg"),
new Country("France", "https://i.imgur.com/1Gt5ldY.jpg"),
new Country("Japan", "https://i.imgur.com/ypGnx2q.jpg"),
new Country("U.S.A.", "https://i.imgur.com/qfQGh.jpg"),
new Country("China", "https://i.imgur.com/fpgPo.jpg"),
new Country("Australia", "https://i.imgur.com/zW30U2O.jpg"),
new Country("Egypt", "https://i.imgur.com/mXlezUD.jpg"),
new Country("Haiti", "https://i.imgur.com/QS6Dx.jpg"),
new Country("Zambia", "https://gp1.wac.edgecastcdn.net/806614/photos/photos.500px.net/86372017/b367fd0ce34e41f1b357b11942629e7c68dcfb8a/2048.jpg"),
new Country("Chile", "https://i.imgur.com/DcTmjg4.jpg"),
new Country("United Kingdom", "https://i.imgur.com/V0A6N3s.jpg"),
new Country("Canada", "https://i.imgur.com/PLehguA.jpg")
}));

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.musenkishi.paletteloader.sample;

/**
* Created by frelus on 13/05/15.
*/
public class Country {

private String name, url;

public Country(String name, String url) {
this.name = name;
this.url = url;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package com.musenkishi.paletteloader.sample;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.GlideBitmapDrawable;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.musenkishi.paletteloader.PaletteLoader;
import com.musenkishi.paletteloader.PaletteRequest;

/**
* A simple adapter for loading countries names and images.
* Created by Freddie (Musenkishi) Lust-Hed on 14/05/15.
*/
public class CountryAdapter extends RecyclerView.Adapter<CountryAdapter.ViewHolder> {

@Override
public CountryAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int position) {
LayoutInflater inflater = (LayoutInflater) viewGroup.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final CardView view = (CardView) inflater.inflate(R.layout.list_item, viewGroup, false);
return new ViewHolder(view);
}

@Override
public void onBindViewHolder(final CountryAdapter.ViewHolder viewHolder, int position) {

Country country = Countries.countries.get(position);

viewHolder.textView.setText(country.getName());

viewHolder.rootView.setCardBackgroundColor(Color.parseColor("#FFFAFAFA"));
viewHolder.textView.setTextColor(Color.BLACK);

RequestListener<String, GlideDrawable> glideDrawableRequestListener = new RequestListener<String, GlideDrawable>() {
@Override
public boolean onException(Exception e,
String url,
Target<GlideDrawable> target,
boolean isFirstResource) {
return false;
}

@Override
public boolean onResourceReady(GlideDrawable resource,
String url,
Target<GlideDrawable> target,
boolean isFromMemoryCache,
boolean isFirstResource) {
Bitmap bitmap = ((GlideBitmapDrawable) resource).getBitmap();
if (bitmap != null) {
Context context = viewHolder.rootView.getContext();
PaletteLoader.with(context, url)
.load(bitmap)
.setPaletteRequest(new PaletteRequest(
PaletteRequest.SwatchType.REGULAR_VIBRANT,
PaletteRequest.SwatchColor.BACKGROUND))
.into(viewHolder.rootView);
PaletteLoader.with(context, url)
.load(bitmap)
.setPaletteRequest(new PaletteRequest(
PaletteRequest.SwatchType.REGULAR_VIBRANT,
PaletteRequest.SwatchColor.TEXT_TITLE))
.into(viewHolder.textView);
}
return false;
}
};

Glide.with(viewHolder.rootView.getContext())
.load(country.getUrl())
.fitCenter()
.placeholder(Color.TRANSPARENT)
.listener(glideDrawableRequestListener)
.into(viewHolder.imageView);

}

@Override
public int getItemCount() {
return Countries.countries.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {
CardView rootView;
ImageView imageView;
TextView textView;

public ViewHolder(CardView rootView) {
super(rootView);
this.rootView = rootView;
imageView = (ImageView) rootView.findViewById(R.id.image);
textView = (TextView) rootView.findViewById(R.id.text);
}
}

}
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
package com.musenkishi.paletteloader;
package com.musenkishi.paletteloader.sample;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends ActionBarActivity {
public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(new CountryAdapter());
}

@Override
Expand Down
16 changes: 16 additions & 0 deletions sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemstyleas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" tools:context=".MainActivity">

<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:clipToPadding="false"
/>

</RelativeLayout>
34 changes: 34 additions & 0 deletions sample/src/main/res/layout/list_item.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
card_view:cardCornerRadius="6dp"
card_view:cardUseCompatPadding="true">

<LinearLayout
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">

<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="250dp"
android:scaleType="centerCrop" />

<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@string/font_fontFamily_medium"
android:gravity="center"
android:padding="16dp"
android:textAllCaps="true"
android:textSize="16sp" />

</LinearLayout>
</android.support.v7.widget.CardView>
File renamed without changes.
4 changes: 4 additions & 0 deletions sample/src/main/res/values-v21/fonts.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="font_fontFamily_medium">sans-serif-medium</string>
</resources>
File renamed without changes.
4 changes: 4 additions & 0 deletions sample/src/main/res/values/fonts.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="font_fontFamily_medium">sans-serif</string>
</resources>
Loading

0 comments on commit 29eaf92

Please sign in to comment.