简体   繁体   English

如何从RGB颜色创建位图

[英]How to create a bitmap from a RGB color

I am making an app that uses 3 SeekBars to allow the user to create a RGB color. 我正在制作一个使用3个SeekBars的应用程序,允许用户创建RGB颜色。 I want to then set this color as a background to the user using the WallpaperManager. 我想使用WallpaperManager将此颜色设置为用户的背景。

If I have 3 values, one for red, one for green, and one for blue, is there a way to create a square bitmap that is filled only with that one color? 如果我有3个值,一个用于红色,一个用于绿色,一个用于蓝色,有没有办法创建仅用这一种颜色填充的方形位图?

You could do this to create a squared bitmap with a selected color. 您可以这样做以创建具有所选颜色的平方位图。

// Here you create the bound of your shape
Rect rect = new Rect(0, 0, 1, 1);

// You then create a Bitmap and get a canvas to draw into it
Bitmap image = Bitmap.createBitmap(rect.width(), rect.height(), Config.ARGB_8888);
Canvas canvas = new Canvas(image);

//You can get an int value representing an argb color by doing so. Put 1 as alpha by default
int color = Color.argb(alpha, red, green, blue);

//Paint holds information about how to draw shapes
Paint paint = new Paint();
paint.setColor(color);

// Then draw your shape
canvas.drawRect(rect, paint);

Here is a sample project that I created which uses custom view. 这是我创建的一个使用自定义视图的示例项目。 This custom view has setColor() method, where you pass RGB values separately, when seek bar is changed, the color is updated. 此自定义视图具有setColor()方法,您可以在其中单独传递RGB值,当搜索栏更改时,颜色会更新。 And when button is clicked it generates bitmap. 当单击按钮时,它会生成位图。 Download full project here from github. 从github下载完整的项目 One thing is missing, seekbar value at max is 100, you should map it to 0-255, in order to avail all possible color combinations. 缺少一件事,最大值的搜索栏值为100,您应该将其映射到0-255,以便利用所有可能的颜色组合。

Now, whenever you change seekbar, it change color at runtime. 现在,每当您更改搜索栏时,它都会在运行时更改颜色。 Since this was fun task to do it was worth investing my time. 由于这是一项有趣的任务,所以值得投入时间。 You can also create jpeg file to allow user to use that file, refer here . 您还可以创建jpeg文件以允许用户使用该文件, 请参阅此处 Below is the source code: 以下是源代码:

MainActivity: 主要活动:

public class MainActivity extends AppCompatActivity {

    SeekBar skRed;
     SeekBar skGreen;
    SeekBar skBlue;


    RgbView myView ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        skRed=(SeekBar) findViewById(R.id.redBar);
         skGreen=(SeekBar) findViewById(R.id.greenBar);
        skBlue=(SeekBar) findViewById(R.id.blueBar);
        myView = (RgbView) findViewById(R.id.customView);

        myView = (RgbView) findViewById(R.id.customView);

        // This is sample color combination replace with your seekbar values

        Button colorChange=(Button) findViewById(R.id.button);

        colorChange.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                 // Bitmap creation code
                myView.setDrawingCacheEnabled(true);

                myView.buildDrawingCache();


                // bm is your required bitmap
                Bitmap bm = myView.getDrawingCache();

                Toast.makeText(MainActivity.this,"Bitmap Ready",Toast.LENGTH_LONG).show();

            }
        });


        skRed.setOnSeekBarChangeListener(changeListener);
        skGreen.setOnSeekBarChangeListener(changeListener);
        skBlue.setOnSeekBarChangeListener(changeListener);
    }

    SeekBar.OnSeekBarChangeListener changeListener=new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {


    // calling setColor() of custom view , passing current seekbar values from 3 seekbars 

    myView.setColor(skRed.getProgress(),skGreen.getProgress(),skBlue.getProgress());


            }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    };


}

CustomView: RgbView.java CustomView:RgbView.java

public class RgbView extends View {

    Paint p=new Paint();
    public RgbView(Context context) {
        super(context);
        init(null, 0);
    }

    public RgbView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs, 0);
    }

    public RgbView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(attrs, defStyle);
    }

    private void init(AttributeSet attrs, int defStyle) {
        p.setColor(Color.RED);
    }

    public void setColor(int Red, int Green, int Blue)
    {

        p.setARGB(255, Red, Green, Blue);
        invalidate();

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.drawRect(0,0, getHeight(),getWidth(),p);
    }



}

xml for MainActivity: MainActivity的xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.talha.projects.bitmaptest.MainActivity">

  <com.talha.projects.bitmaptest.RgbView
      android:layout_width="match_parent"
      android:layout_height="200dp"
      android:id="@+id/customView"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Load into Bitmap"
        android:id="@+id/button"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />

    <SeekBar
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:id="@+id/redBar"

        android:layout_below="@+id/customView"
        android:layout_alignRight="@+id/customView"
        android:layout_alignEnd="@+id/customView" />

    <SeekBar
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:id="@+id/greenBar"
        android:layout_below="@+id/redBar"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <SeekBar
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:id="@+id/blueBar"
        android:layout_below="@+id/greenBar"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>

Here is the output: 这是输出: 在此输入图像描述

Link to project . 链接到项目

Created using Android Studio, minimum API level 15. Remember to change the value mapping on seekbar from 0-255, currently is 0-99. 使用Android Studio创建,最低API级别15.请记住将搜索栏上的值映射更改为0-255,目前为0-99。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM