-
Notifications
You must be signed in to change notification settings - Fork 12
/
MainActivity.java
60 lines (46 loc) · 2.2 KB
/
MainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
Showing text over Images in appropriate colors.
android.graphics.Bitmap and
android.support.v7.graphics.Palette classes are used.
*/
package com.blikoon.app17;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.support.v4.graphics.ColorUtils;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import android.support.v7.graphics.Palette;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
((ImageView) findViewById(R.id.first_photo)).setImageResource(R.drawable.first_photo);
Bitmap firstPhoto = BitmapFactory.decodeResource(getResources(), R.drawable.first_photo);
setTextColorForImage((TextView) findViewById(R.id.first_text), firstPhoto);
((ImageView) findViewById(R.id.second_photo)).setImageResource(R.drawable.second_photo);
Bitmap secondPhoto = BitmapFactory.decodeResource(getResources(), R.drawable.second_photo);
setTextColorForImage(((TextView) findViewById(R.id.second_text)), secondPhoto);
}
private void setTextColorForImage(final TextView textView, Bitmap firstPhoto) {
Palette.from(firstPhoto)
.generate(new Palette.PaletteAsyncListener() {
@Override
public void onGenerated(Palette palette) {
Palette.Swatch swatch = palette.getVibrantSwatch();
if (swatch == null && palette.getSwatches().size() > 0) {
swatch = palette.getSwatches().get(0);
}
int titleTextColor = Color.WHITE;
if (swatch != null) {
titleTextColor = swatch.getTitleTextColor();
titleTextColor = ColorUtils.setAlphaComponent(titleTextColor, 255);
}
textView.setTextColor(titleTextColor);
}
});
}
}