Android 主题背景颜色设置(一)

Android 主题背景颜色设置

一.效果图:

Android 主题背景颜色设置(一)_第1张图片

二.快速实现:

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.布局:



    

    
    
    

相关源码:

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  一个不错的主题颜色设置

你可能感兴趣的:(自定义)