2016-06-12 9 views
9

私はグラデーションで色付けしたい白い画像を持っています。特定のグラデーションで色付けされた画像の束を生成する代わりに、私はコードでこれをやりたいと思います(xmlではありません)。 画像ビューにグラデーションカラーフィルタを設定しました

は、画像の色を変更するには、私は

imageView.setColorFilter(Color.GREEN); 

を使用し、これが正常に動作します。しかし、単色の代わりにどのようにグラデーションカラーを適用できますか? LinearGradientは役に立ちません。なぜなら、setColorFilterはShaderオブジェクトに適用できないからです。

EDITは:

enter image description here

これは私が欲しいものです::これは私が持っているイメージです

enter image description here

そして、これは私が取得していますです:

enter image description here

012勾配、てLinearGradient幅= 0とPorterDuffXfermodeの色:
+0

の背景として、これを追加するには、最初の場所に画像を描画するためにXMLでImageViewのを使用していますか? – SQLiteNoob

+0

@SQLiteNoobいいえ、私はそれらを動的にコードで作成しています。 – Malfunction

答えて

12

はあなたのImageViewBitmapを取得し、私は変更Shader

public void clickButton(View v){ 
    Bitmap myBitmap = ((BitmapDrawable)myImageView.getDrawable()).getBitmap(); 

    Bitmap newBitmap = addGradient(myBitmap); 
    myImageView.setImageDrawable(new BitmapDrawable(getResources(), newBitmap)); 
} 


public Bitmap addGradient(Bitmap originalBitmap) { 
    int width = originalBitmap.getWidth(); 
    int height = originalBitmap.getHeight(); 
    Bitmap updatedBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 
    Canvas canvas = new Canvas(updatedBitmap); 

    canvas.drawBitmap(originalBitmap, 0, 0, null); 

    Paint paint = new Paint(); 
    LinearGradient shader = new LinearGradient(0, 0, 0, height, 0xFFF0D252, 0xFFF07305, Shader.TileMode.CLAMP); 
    paint.setShader(shader); 
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); 
    canvas.drawRect(0, 0, width, height, paint); 

    return updatedBitmap; 
} 

UPDATE 3 と同じBitmapを再描画する必要があります。ここで PorterDuffXfermodeを理解する良い絵: enter image description here

+0

これはどのようにイメージビューで使用できますか? – Malfunction

+0

@Malfunction、申し訳ありません。私の回答を更新します。 – LaurentY

+0

@Malfunction私は自分の答えを更新してテストします。 – LaurentY

0

あなたはセレクタ

main_header.xml使用することができます

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout   xmlns:android="http://schemas.android.com/apk/res/. android" 
    android:layout_width="fill_parent" 
    android:layout_height="50dip" 
    android:orientation="horizontal" 
    android:background="@drawable/main_header_selector"> 
</LinearLayout> 

がmain_header_selector.xml:同じ背景

<?xml version="1.0" encoding="utf-8"?> 
<selector  xmlns:android="http://schemas.android.com/apk/res/.  android"> 
<item> 
    <shape> 
     <gradient 
     android:angle="90" 
     android:startColor="#FFFF0000" 
     android:endColor="#FF00FF00" 
     android:type="linear" /> 
    </shape> 
</item> 
</selector> 

をすることができますImageViewに適用されます。 Dynamically defining and using selectors

+0

これは背景を適用するためです。私はイメージ自体を変えるものが必要です。 – Malfunction

0

XMLファイルを作成し、描画可能なフォルダに配置します:このリンクを参照して、動的にセレクタを定義して使用するには

gradient.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="rectangle"> 
<gradient 
    android:startColor="#CCb1e7fa" 
    android:centerColor="#B3ffffff" 
    android:endColor="#CCb1e7fa" 
    android:angle="180" /> 
<corners android:radius="5dp" /> 

</shape> 

次はあなたのイメージ図

<ImageView 
      android:id="@+id/umageview1" 
      android:layout_width="100dp" 
      android:layout_height="100dp" 
      android:background="@drawable/gradient" 
      android:layout_centerHorizontal="true" 
      /> 
関連する問題