package com.demo.palettetest;
import android.graphics.Color;
public class ColorUtils {
public static int parseBackgroundColor2(int color) {
int counter = 0;
counter += Color.red(color) >= 128 ? 1 : 0;
counter += Color.green(color) >= 128 ? 1 : 0;
counter += Color.blue(color) >= 128 ? 1 : 0;
return counter >= 2 ? Color.BLACK : Color.WHITE;
}
// 通过分析背景色来决定当前文字的匹配颜色,使文字颜色自适应背景颜色
public static int parseBackgroundColor(int color) {
int red = Color.red(color);
int green = Color.green(color);
int blue = Color.blue(color);
if (red >= 128 && green >= 128 // 三选二
|| red >= 128 && blue >= 128
|| green >= 128 && blue >= 128) {
return Color.rgb(0, 0, 0);
}
return Color.rgb(255, 255, 255);
}
// #FF55FF => color
// int color = Color.parseColor("#b64242");
// color -> #FF55FF
public static String toRGBHexString(final int color) {
return toRGBHexString(Color.red(color), Color.green(color), Color.blue(color));
}
// (r,g,b) -> #FF55FF
public static String toRGBHexString(int red, int green, int blue) {
return toARGBHexString(-1, red, green, blue);
}
// default prefix: "#"
// (a,r,g,b) -> #FF55FF55
public static String toARGBHexString(int alpha, int red, int green, int blue) {
return toARGBHexString("#", alpha, red, green, blue);
}
public static String toARGBHexString(String prefix, int alpha, int red, int green, int blue) {
StringBuilder sb = new StringBuilder();
sb.append(prefix);
if (alpha != -1) {
String mAlphaStr = Integer.toHexString(alpha);
sb.append(mAlphaStr.length() == 1 ? "0" + mAlphaStr : mAlphaStr);
}
String mRedStr = Integer.toHexString(red);
sb.append(mRedStr.length() == 1 ? "0" + mRedStr : mRedStr);
String mGreenStr = Integer.toHexString(green);
sb.append(mGreenStr.length() == 1 ? "0" + mGreenStr : mGreenStr);
String mBlueStr = Integer.toHexString(blue);
sb.append(mBlueStr.length() == 1 ? "0" + mBlueStr : mBlueStr);
return sb.toString().toUpperCase();
}
}
package com.demo.palettetest
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Color
import android.net.Uri
import android.os.Build
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.annotation.RequiresApi
import android.support.v4.content.ContextCompat
import android.support.v7.graphics.Palette
import android.widget.Button
import android.widget.TextView
import com.facebook.binaryresource.FileBinaryResource
import com.facebook.cache.common.SimpleCacheKey
import com.facebook.drawee.backends.pipeline.Fresco
import com.facebook.drawee.view.SimpleDraweeView
class MainActivity : AppCompatActivity() {
private var interImg: SimpleDraweeView? = null
private var cacheImg: SimpleDraweeView? = null
private var bgImg: SimpleDraweeView? = null
private var tv1: TextView?= null
private var tv3: TextView?= null
private var tv2: TextView?= null
private var tv4: TextView?= null
private var tv5: TextView?= null
private var tv6: TextView?= null
private var tv7: TextView?= null
private var tv8: TextView?= null
private var btn: Button? = null
private val imgUrl = "http://d.lanrentuku.com/down/png/1712/22xiaodongwu/22xiaodongwu_12.png"
private var count = 1
@RequiresApi(Build.VERSION_CODES.JELLY_BEAN)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Fresco.initialize(this)//初始化Fresco
setContentView(R.layout.activity_main)
interImg = findViewById(R.id.interImg)//网络请求的图片的id
cacheImg = findViewById(R.id.cacheImg)//从本地加载的图片的id
bgImg = findViewById(R.id.bgImg)
tv1 = findViewById(R.id.tv1)
tv2 = findViewById(R.id.tv2)
tv3 = findViewById(R.id.tv3)
tv4 = findViewById(R.id.tv4)
tv5 = findViewById(R.id.tv5)
tv6 = findViewById(R.id.tv6)
tv7 = findViewById(R.id.tv7)
tv8 = findViewById(R.id.tv8)
btn = findViewById(R.id.btnChange)
btn?.setOnClickListener {
if (count <= 5) {
setColor(count)
count ++
} else {
count = 1
setColor(count)
}
}
val parse = Uri.parse(imgUrl)
interImg?.setImageURI(parse)
/** * 获取缓存之后的图片
* */
/* if (parse != null) {
val resource = Fresco.getImagePipelineFactory().mainFileCache.getResource(SimpleCacheKey(parse.toString())) as FileBinaryResource
val file = resource.file
*//** * 将文件转为uri形式 *//*
val uri = Uri.fromFile(file)
cacheImg!!.setImageURI(uri)
}*/
setColor(count)
}
@RequiresApi(Build.VERSION_CODES.JELLY_BEAN)
fun setColor(count: Int){
val bgDraw = when(count){
1 -> R.drawable.a1
2 -> R.drawable.a2
3 -> R.drawable.a3
4 -> R.drawable.a4
else -> R.drawable.a5
}
val bitmap = BitmapFactory.decodeResource(resources, bgDraw)
bgImg?.setActualImageResource(bgDraw)
Palette.from(bitmap?:return).generate {
tv1?.setTextColor(it.getDominantColor(Color.WHITE))
tv2?.setTextColor(it.getLightVibrantColor(Color.WHITE))
tv3?.setTextColor(it.getMutedColor(Color.WHITE))
tv4?.setTextColor(it.getDarkMutedColor(Color.WHITE))
tv5?.setTextColor(it.getDarkVibrantColor(Color.WHITE))
tv6?.setTextColor(it.getLightMutedColor(Color.WHITE))
tv7?.setTextColor(it.getVibrantColor(Color.WHITE))
tv8?.setTextColor(it.dominantSwatch?.titleTextColor?:return@generate)
}
}
private fun getBitmap(uri: String?): Bitmap?{
if (uri == null) return null
var bitmap: Bitmap?= null
try {
val resource = Fresco.getImagePipelineFactory().mainFileCache.getResource(SimpleCacheKey(uri)) as FileBinaryResource
val file = resource.file
bitmap = BitmapFactory.decodeFile(file.path)
} catch (e: Exception){
}
return bitmap
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"> <!--网络请求的图片-->
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/bgImg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true"
app:actualImageResource="@drawable/a4"/>
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/interImg"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerVertical="true"
android:src="@mipmap/ic_launcher_round"
app:failureImage="@mipmap/ic_launcher_round"
app:placeholderImage="@mipmap/ic_launcher_round"
app:roundAsCircle="true" /> <!--网络缓存到本地之后获取的图片-->
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/cacheImg"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
android:src="@mipmap/ic_launcher_round"
app:failureImage="@mipmap/ic_launcher_round"
app:layout_constraintTop_toBottomOf="@id/interImg"
app:placeholderImage="@mipmap/ic_launcher_round" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#FFFFFF"
app:layout_constraintTop_toBottomOf="@+id/cacheImg">
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:text="getDominantColor"/>
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:text="getLightVibrantColor" />
<TextView
android:id="@+id/tv3"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:text="getMutedColor" />
<TextView
android:id="@+id/tv4"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:text="getDarkMutedColor" />
<TextView
android:id="@+id/tv5"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:text="getDarkVibrantColor" />
<TextView
android:id="@+id/tv6"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:text="getLightMutedColor" />
<TextView
android:id="@+id/tv7"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:text="getVibrantColor" />
</LinearLayout>
<TextView
android:id="@+id/tv8"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:text="titleColor"
android:layout_marginTop="20dp"
android:textSize="18sp"
android:ellipsize="middle"
android:ems="12"
android:gravity="center"
android:singleLine="true"
android:shadowColor="#80000000"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="16"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<Button
android:id="@+id/btnChange"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="change"/>
</android.support.constraint.ConstraintLayout>