android 平滑地从左到右改变图像的颜色

android changing color of image from left to right smoothly

我想像光谱一样从左到右改变图像的颜色,并在颜色变化时看到颜色的变化,这是我的代码,问题是它从原始图像变为最终图像单击按钮后,但我不知道如何更改它,所以我可以在更改时看到更改。

final ImageView imgView = (ImageView) findViewById(R.id.imageView);
    Button btn = (Button) findViewById(R.id.button1);

    Bitmap bitmap = ((BitmapDrawable) imgView.getDrawable()).getBitmap();
    final Bitmap mutableBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {


            for( x = 0; x < mutableBitmap.getWidth() ; x++){
                runOnUiThread (new Thread(new Runnable() { 
                     public void run() {
                         try {
                                        for(int y = 0; y < mutableBitmap.getHeight() ; y++){
                                            int c = mutableBitmap.getPixel(x, y);
                                            mutableBitmap.setPixel(x, y, Color.rgb(Color.red(c), (200 - Color.green(c)), Color.blue(c)));
                                            Log.d("IMAGE", ""+x);
                                        }   
                                        imgView.setImageBitmap(mutableBitmap);
                                            Thread.sleep(100);
                                        } catch (InterruptedException e) {
                                            // TODO Auto-generated catch block
                                            e.printStackTrace();
                                        }
                             }

                }));
            }
        }


    });

我不是专家,但我想你可以试试这个来选择你想要的颜色:

int[] colors = {0xFFFFFFFF, 0xFFFF0000, 0xFF00FF00}

这是为了应用它们:

mutableBitmap.setPixel(x, y, new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT, colors);

希望对您有所帮助

您可以右键单击您的可绘制文件夹,select New\Drawable 资源文件。为文件选择一个名称,然后单击“确定”。 之后您可以添加此代码:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
    android:angle="135"
    android:centerColor="@color/colorAccent"
    android:endColor="@color/colorPrimaryDark"
    android:startColor="@color/colorPrimary"
    android:type="linear" />

您可以color.xml创造新的颜色并使用它们。然后在java代码中:

imageView.setImageDrawable(getResources().getDrawable(R.drawable.YOURDRAWABLENAME));