알다시피 일반 Android API를 사용하여 타원 방사형 그래디언트를 그릴 수는 없습니다. 메모리 무거운 타원형 그라디언트
그래서 나는이 솔루션 구현 :이
내가 달성하고자하는 것입니다 정사각형 비트 맵에 일반 방사형 그라디언트를 그릴 다음이 비트 맵 뷰 자체로 뻗어 얻을 것이다 (아이디어 발견 여기에 : https://stackoverflow.com/a/3543899/649910)이 솔루션은 BitmapDrawable 사용으로 인해 많은 메모리가 필요합니다 (아래 구현 세부 정보 참조).
큰 비트 맵 사용을 피하는 방법에 대한 아이디어는 언제나 환영합니다!
이 내 코드 :
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);
}
}
그리고이 드로어 블 XML입니다은 - 그들이 전체 화면 특히 경우,
<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