2011-12-12 31 views
11

Im cố gắng tạo lại gradient theo trình tự sau đây.Tạo một gradient xuyên tâm lập trình

<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
    <gradient 
     android:startColor="@color/startcolor" 
     android:centerColor="#343434" 
     android:endColor="#00000000" 
     android:type="radial" 
     android:gradientRadius="140" 
     android:centerY="45%" 
    /> 
    <corners android:radius="0dp" /> 
</shape> 

Làm cách nào để đặt tham số theo chương trình? Cảm ơn

 android:centerY="45%" 

Trả lời

24

http://developer.android.com/reference/android/graphics/drawable/GradientDrawable.html

Để thiết rằng tham số cụ thể (Tôi giả định một giá trị centerX như bạn đã không được xác định một):

yourGradientDrawable.setGradientCenter(1.0f, 0.45f); 

Vì vậy, để tạo gradient ở trên (trừ có màu sắc khác nhau) theo chương trình:

GradientDrawable g = new GradientDrawable(Orientation.TL_BR, new int[] { getResources().getColor(R.color.startcolor), Color.rgb(255, 0, 0), Color.BLUE }); 
g.setGradientType(GradientDrawable.RADIAL_GRADIENT); 
g.setGradientRadius(140.0f); 
g.setGradientCenter(0.0f, 0.45f); 

Lưu ý: Định hướng bị bỏ qua f hoặc một gradient xuyên tâm nhưng là cần thiết cho các nhà xây dựng có màu sắc.

+2

Điều này không có tác dụng đối với tôi, không có đường nét ngang nào có màu trắng đồng nhất. – Poutrathor

+1

Không cần phải gọi '' 'setGradientCenter''' nếu bạn muốn sử dụng giá trị trung tâm mặc định –

Các vấn đề liên quan