2017-01-07 16 views
0

画像の色をスペクトルのように左から右に変更し、色の変化が変わっているのを見たいのですが、これは私のコードです。 buttomをクリックして元のイメージから最終的なイメージに変更する方法がわからないので、変更が反映されるように変更を確認することができます。画像の色が左から右に滑らかに変化する

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(); 
             } 
          } 

       })); 
      } 
     } 


    }); 
+0

[色グラデーション](https://en.wikipedia.org/wiki/Color_gradient)のような意味ですか? – CommonsWare

+0

はい、たとえば白で始まり、緑で連続して終了します。 –

+0

独自のビットマップをアセンブルするのではなく、 'GradientDrawable'を使うことを検討してください。 – CommonsWare

答えて

1

あなたのドロウアブルフォルダを右クリックし、New \ Drawableリソースファイルを選択することができます。ファイルの名前を選択し、[OK]をクリックします。 このコードを追加することができます後:

<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)); 
1

私は専門家ではないんだけど、私はあなたが欲しい色を選択するには、このを試すことができると思います。それらを適用する

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

そして、これを:

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

はそれを願っていますヘルプ

+0

ありがとう "ビットマップ型のメソッドsetPixel(int、int、int)は引数(int、int、GradientDrawable)には適用できません" –

+0

コードで行う必要がありますか?なぜなら、xmlはとても簡単だからです。あなたが新しい描画可能なものを作成し、それをImageViewに適用すると、興味があり、それがあなたが望むものなら、私はあなたにどのように表示できるのですか? – Johnson

+0

@BitaMirshafiee – Johnson

関連する問題