一.效果图:
二.快速实现:
1.添加依赖:
implementation "com.github.skydoves:multicolorpicker:1.0.8"
implementation "com.github.skydoves:elasticviews:2.0.0"
2.主函数代码:调用系统相册,来获取图片上的颜色值
package com.example.m1571.myapplication.view.activity.color;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import com.example.m1571.myapplication.R;
import com.skydoves.elasticviews.ElasticButton;
import com.skydoves.multicolorpicker.ColorEnvelope;
import com.skydoves.multicolorpicker.MultiColorPickerView;
import com.skydoves.multicolorpicker.listeners.ColorListener;
import java.io.FileNotFoundException;
import java.io.InputStream;
public class MultiColorPickerActivity extends AppCompatActivity {
private int RESULT_LOAD_IMG = 1000;
private MultiColorPickerView mMultiColorPickerView;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mc);
mMultiColorPickerView = findViewById(R.id.multiColorPickerView);
final TextView tv_bg = findViewById(R.id.tv_bg);
final TextView tv_add = findViewById(R.id.tv_add);
final ElasticButton gallery = findViewById(R.id.gallery);
mMultiColorPickerView.addSelector(ContextCompat.getDrawable(this, R.drawable.wheel), new ColorListener() {
@Override
public void onColorSelected(ColorEnvelope envelope) {
int color = envelope.getColor();
int[] rgb = envelope.getRgb();
String htmlCode = envelope.getHtmlCode();
String htmlCodes = envelope.getHtmlCode();
tv_bg.setText("#"+htmlCodes + "\t,R:" +rgb[0]+",G:"+rgb[1]+",B:"+rgb[2]);
// tv_bg.setBackgroundColor(envelope.getColor());
tv_add.setBackgroundColor(envelope.getColor());
// TODO
}
});
gallery.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent,RESULT_LOAD_IMG);
}
});
tv_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent,RESULT_LOAD_IMG);
}
});
}
@SuppressLint("NewApi")
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if (resultCode == Activity.RESULT_OK) {
try {
if(data!=null){
Uri imageUri = data.getData();
InputStream inputStream = getContentResolver().openInputStream(imageUri);
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
Drawable bitmapDrawable = new BitmapDrawable(bitmap);
mMultiColorPickerView.setPaletteDrawable(bitmapDrawable);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
}
3.布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/mainLayout"
android:orientation="vertical">
<com.skydoves.multicolorpicker.MultiColorPickerView
android:id="@+id/multiColorPickerView"
android:layout_width="300dp"
android:layout_gravity="center"
android:layout_height="300dp"
app:palette="@drawable/g"/>
<TextView
android:id="@+id/tv_bg"
android:text="KING"
android:layout_marginTop="15dp"
android:layout_gravity="center"
android:gravity="center"
android:layout_width="150dp"
android:layout_height="50dp" />
<TextView
android:id="@+id/tv_add"
android:text="添加图片"
android:layout_marginTop="15dp"
android:layout_gravity="center"
android:gravity="center"
android:layout_width="150dp"
android:layout_height="50dp" />
<com.skydoves.elasticviews.ElasticButton
android:id="@+id/gallery"
android:textColor="@android:color/white"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="15dp"
app:button_labelText="Gallery"
app:button_labelStyle="bold"
app:button_labelSize="17"
app:button_backgroundColor="@color/btnColor"
app:button_duration="250"
app:button_scale="0.8"/>
</LinearLayout>
相关源码:
https://github.com/jbruchanov/AndroidColorPicker
https://github.com/MummyDing/Leisure
https://github.com/MummyDing/ColorPickerDialog
https://github.com/MummyDing/Awesome-Campus
https://github.com/JorgeCastilloPrz/AndroidColorX
https://github.com/QuadFlask/colorpicker 一个不错的主题颜色设置