31

白と灰色の境界線で塗りつぶされたOvalShapeを使用してカスタムShapeDrawableを描画しようとしています。私はこのようなドローアブルを作成しました:

ShapeDrawable drawable = new ShapeDrawable(new OvalShape());
drawable.getPaint().setColor(Color.GRAY);
drawable.getPaint().setStyle(Style.STROKE);
drawable.getPaint().setStrokeWidth(getPixels(5));
drawable.getPaint().setAntiAlias(true);

しかし、その結果は次のとおりでした:コーナーの問題

ここに画像の説明を入力してください

アイデアは、プログラムでこのような形を作成することですが、色は異なります。

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="oval">
<corners android:radius="10dip"/>
<stroke android:color="#FF0000" android:width="5dip"/>
<solid android:color="@android:color/transparent"/>
</shape>

どうすればこれを修正できますか?

4

2 に答える 2

45

新しいドローアブルの作成を回避する方法を見つけました!

次のように、AndroidXMLからの境界線を持つ定義済みの円:

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="oval">
<corners android:radius="10dip"/>
<stroke android:color="#FF0000" android:width="5dip"/>
<solid android:color="@android:color/transparent"/>
</shape>

次に、描画可能な色を変更したい場合は、ColorFilterを適用します。たとえば、drawableの赤い色を青に変更したい場合は、次のようにします。

Drawable drawable = context.getResources().getDrawable(R.drawable.name);
drawable.setColorFilter(Color.BLUE, PorterDuff.Mode.SRC_ATOP);

コードからカスタムセレクターを作成するためにStateListDrawableを使用する場合は、注意してください-StateListDrawableは、ドローアブルフィルターに適用されます...selector.setColorFilter...問題を修正してセレクター全体にフィルターを適用します。

于 2013-03-07T00:16:41.947 に答える
1

これは新しい解決策です:

 RoundedBitmapDrawable RBD = RoundedBitmapDrawableFactory.create(mContext.getResources(),YourBitmap);
            RBD.setCornerRadius(20);
            RBD.setAntiAlias(true);

            wholeRel.setBackground(RBD);
于 2017-12-12T23:24:14.917 に答える