Comme vous le savez peut-être, il n'est pas possible de dessiner un dégradé radial ovale à l'aide de l'API Android habituelle.Gradient ovale lourd de mémoire
C'est ce que je veux atteindre:
Je mis en œuvre cette solution: tirer un dégradé radial régulier sur une image bitmap carré puis ce bitmap sera pas étirée par la vue elle-même (idée trouvé ici: https://stackoverflow.com/a/3543899/649910)
Cela fonctionne très bien, mais cette solution prend beaucoup de mémoire, en raison de l'utilisation de BitmapDrawable (voir les détails de mise en œuvre ci-dessous).
Toutes les idées sur la façon d'éviter l'utilisation d'un tel bitmap sont les bienvenues!
Ce mon code:
public class OvalGradientView extends ImageView {
private Drawable defaultBackgroundDrawable;
public OvalGradientView(Context context) {
super(context);
init();
}
public OvalGradientView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public OvalGradientView(Context context, AttributeSet attrs, int defStyleAttr, Paint paint) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
setScaleType(ScaleType.FIT_XY);
defaultBackgroundDrawable = getResources().getDrawable(R.drawable.default_background);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
int width = getWidth();
int height = getHeight();
Rect currentBounds = defaultBackgroundDrawable.getBounds();
// check if we already have bitmap for these bounds
if (currentBounds.right == width && currentBounds.bottom == height) {
return;
}
// draw the drawable on square bitmap, it will be then stretched if needed to rectangular shape
// as the view gets more rectangular
defaultBackgroundDrawable.setBounds(0, 0, width, width);
Bitmap defaultBackgroundBitmap = Bitmap.createBitmap(width, width, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(defaultBackgroundBitmap);
defaultBackgroundDrawable.draw(canvas);
setImageBitmap(defaultBackgroundBitmap);
}
}
Et c'est le XML drawable - default_background
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#ffffff"
android:endColor="#ff6600"
android:gradientRadius="50%p"
android:type="radial" />
</shape