2016-11-25 7 views
0

알다시피 일반 Android API를 사용하여 타원 방사형 그래디언트를 그릴 수는 없습니다. 메모리 무거운 타원형 그라디언트

Expected effect

그래서 나는이 솔루션 구현 :

내가 달성하고자하는 것입니다 정사각형 비트 맵에 일반 방사형 그라디언트를 그릴 다음이 비트 맵 뷰 자체로 뻗어 얻을 것이다 (아이디어 발견 여기에 : 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 

답변

0

my_sexy_gradient.xml이 match_parent

public class OvalGradientView extends ImageView { 
    public OvalGradientView(Context context, AttributeSet attrs) { 
     super(context, attrs); 

     LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     inflater.inflate(R.layout.my_sexy_gradient, this, true); 
    } 
} 

경우 뷰의 비율을 늘릴 것 :

<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
    <gradient 
     android:startColor="@color/my_background_start_color" 
     android:endColor="@color/my_background_end_color" 
     android:gradientRadius="@fraction/my_background_gradient_radius_percent" 
     android:type="radial" /> 
</shape> 

이보기의 너비와 높이는 모두 match_parent입니다.

1

내가 그와 같은 비트 맵을 만들 수있는 내 자신의 능력을 신뢰하지 것이다 default_background . 왜 XML에서 컨테이너 뷰의 배경으로 XML 셰이프를 적용하지 않습니까? my_background이

public class MyBackgroundView extends ImageView { 

    public MyBackgroundView(Context context) { 
     super(context); 
    } 

    public MyBackgroundView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public MyBackgroundView(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
    } 

    { 
     setBackgroundResource(R.drawable.my_background); 
     setScaleType(ScaleType.FIT_XY); 
    } 

    @Override 
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) { 
     super.onLayout(changed, left, top, right, bottom); 

     if (!changed) { 
      return; 
     } 

     int width = right - left; 
     int height = bottom - top; 
     float aspectRatio = (float) width/height; 
     if (aspectRatio > 1f) { 
      setScaleX(aspectRatio); 
     } else { 
      setScaleY(1/aspectRatio); 
     } 
    } 
} 

입니다

여기서 그것은 너비와 높이 나는이 구현 결국

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="@drawable/default_background"/>