代码之家  ›  专栏  ›  技术社区  ›  Bita Mirshafiee

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

  •  0
  • Bita Mirshafiee  · 技术社区  · 9 年前

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

    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();
                                            }
                                 }
    
                    }));
                }
            }
    
    
        });
    
    2 回复  |  直到 9 年前
        1
  •  1
  •   Johnson    9 年前

    您可以右键单击可绘制文件夹,选择新建\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" />
    

    您可以根据自己的颜色创建新颜色。xml并使用它们。然后在java代码中:

    imageView.setImageDrawable(getResources().getDrawable(R.drawable.YOURDRAWABLENAME));
    
        2
  •  1
  •   Johnson    9 年前

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

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

    并将其应用于:

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

    希望有帮助