package io.github.qichaochen.android50;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.graphics.Palette;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
public
class MainActivity extends ActionBarActivity {
private ImageView image, iv1, iv2, iv3, iv4, iv5, iv6;
private Palette palette;
private Palette.Swatch s1,s2,s3,s4,s5,s6;
private
int index =
0;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv1 = (ImageView) findViewById(R.id.iv1);
iv2 = (ImageView) findViewById(R.id.iv2);
iv3 = (ImageView) findViewById(R.id.iv3);
iv4 = (ImageView) findViewById(R.id.iv4);
iv5 = (ImageView) findViewById(R.id.iv5);
iv6 = (ImageView) findViewById(R.id.iv6);
image = (ImageView) findViewById(R.id.image);
GetPalette(R.drawable.a11);
}
private
void
GetPalette(
int imageId) {
image.setImageResource(imageId);
Palette.generateAsync(BitmapFactory.decodeResource(getResources(), imageId),
new Palette.PaletteAsyncListener() {
@Override
public
void
onGenerated(Palette palette) {
s1 = palette.getVibrantSwatch();
s2 = palette.getDarkVibrantSwatch();
s3 = palette.getLightVibrantSwatch();
s4 = palette.getMutedSwatch();
s5 = palette.getDarkMutedSwatch();
s6 = palette.getLightMutedSwatch();
if (s1 !=
null) {
iv1.setBackgroundColor(s1.getRgb());
s1.getPopulation();
}
if (s2 !=
null) {
iv2.setBackgroundColor(s2.getRgb());
}
if (s3 !=
null) {
iv3.setBackgroundColor(s3.getRgb());
}
if (s4 !=
null) {
iv4.setBackgroundColor(s4.getRgb());
}
if (s5 !=
null) {
iv5.setBackgroundColor(s5.getRgb());
}
if (s6 !=
null) {
iv6.setBackgroundColor(s6.getRgb());
}
}
});
}
@Override
public
boolean
onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main,menu);
return
true;
}
@Override
public
boolean
onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_settings){
index++;
if (index%
3 ==
0){
GetPalette(R.drawable.a11);
}
else
if (index %
3 ==
1){
GetPalette(R.drawable.a12);
}
else
if (index %
3 ==
2){
GetPalette(R.drawable.a13);
}
}
return
true;
}
}