Vreau să creez gradient de fundal în cazul în care gradientul este în jumătatea de sus și de acolo's o culoare solidă în jumătatea de jos, ca în imaginea de mai jos:
Pot't pentru că centerColor
se întinde pentru a acoperi partea de jos și de sus.
Cum pot face un fundal ca prima imagine? Cum pot face mici centerColor
care's nu se răspândească?
Acest cod este în XML de fundal butonul de mai sus.
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<gradient
android:startColor="#6586F0"
android:centerColor="#D6D6D6"
android:endColor="#4B6CD6"
android:angle="90"/>
<corners
android:radius="0dp"/>
</shape>
Incearca cu asta :
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<gradient
android:angle="90"
android:centerColor="#555994"
android:endColor="#b5b6d2"
android:startColor="#555994"
android:type="linear" />
<corners
android:radius="0dp"/>
</shape>
Exemple vizuale de ajutor cu acest tip de întrebare.
În scopul de a crea un gradient, puteți crea un fișier xml în res/drawable. Am sunat pe al meu my_gradient_drawable.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:type="linear"
android:angle="0"
android:startColor="#f6ee19"
android:endColor="#115ede" />
</shape>
Setați-l în fundal de un punct de vedere. De exemplu:
<View
android:layout_width="200dp"
android:layout_height="100dp"
android:background="@drawable/my_gradient_drawable"/>
Setați unghi
pentru un liniar
de tip. Acesta trebuie să fie un multiplu de 45 de grade.
<gradient
android:type="linear"
android:angle="0"
android:startColor="#f6ee19"
android:endColor="#115ede" />
Setați gradientRadius
pentru un "radial" tip. Folosind %p
înseamnă că este un procent din cea mai mică dimensiune de părinte.
<gradient
android:type="radial"
android:gradientRadius="10%p"
android:startColor="#f6ee19"
android:endColor="#115ede" />
Eu nu't știu de ce cineva ar folosi o matura, dar sunt inclusiv pentru completitudine. Nu puteam't dau seama cum de a schimba unghiul, deci nu sunt singurul inclusiv o imagine.
<gradient
android:type="sweep"
android:startColor="#f6ee19"
android:endColor="#115ede" />
Puteți schimba, de asemenea, centrul de matura sau radial tipuri. Valorile sunt fracțiuni din lățimea și înălțimea. Puteți folosi, de asemenea, %p
notație.
android:centerX="0.2"
android:centerY="0.7"
Puteți crea acest 'jumătate-gradient' uita-te folosind xml Strat-List pentru a combina partea de sus și de jos 'trupe' într-un fișier. Fiecare bandă este un xml de forma.
Vezi asta răspunsul anterior privind ATÂT pentru un tutorial detaliat: Multi-gradient de forme.
Următorul link poate te ajuta http://angrytools.com/gradient/ .Acest lucru va crea personalizate de fundal gradient de la android la fel ca în photoshop.
În primul rând aveți nevoie pentru a crea un gradient.xml după cum urmează
<shape>
<gradient android:angle="270" android:endColor="#181818" android:startColor="#616161" />
<stroke android:width="1dp" android:color="#343434" />
</shape>
Apoi, aveți nevoie să menționez mai sus gradient în fundal de layout.După cum urmează
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/gradient"
>
</LinearLayout>
Sau puteți folosi în codul ce s-ar putea gândi în PSD:
private void FillCustomGradient(View v) {
final View view = v;
Drawable[] layers = new Drawable[1];
ShapeDrawable.ShaderFactory sf = new ShapeDrawable.ShaderFactory() {
@Override
public Shader resize(int width, int height) {
LinearGradient lg = new LinearGradient(
0,
0,
0,
view.getHeight(),
new int[] {
getResources().getColor(R.color.color1), // please input your color from resource for color-4
getResources().getColor(R.color.color2),
getResources().getColor(R.color.color3),
getResources().getColor(R.color.color4)},
new float[] { 0, 0.49f, 0.50f, 1 },
Shader.TileMode.CLAMP);
return lg;
}
};
PaintDrawable p = new PaintDrawable();
p.setShape(new RectShape());
p.setShaderFactory(sf);
p.setCornerRadii(new float[] { 5, 5, 5, 5, 0, 0, 0, 0 });
layers[0] = (Drawable) p;
LayerDrawable composite = new LayerDrawable(layers);
view.setBackgroundDrawable(composite);
}
//Color.parseColor() method allow us to convert
// a hexadecimal color string to an integer value (int color)
int[] colors = {Color.parseColor("#008000"),Color.parseColor("#ADFF2F")};
//create a new gradient color
GradientDrawable gd = new GradientDrawable(
GradientDrawable.Orientation.TOP_BOTTOM, colors);
gd.setCornerRadius(0f);
//apply the button background to newly created drawable gradient
btn.setBackground(gd);
Se referă de aici https://android--code.blogspot.in/2015/01/android-button-gradient-color.html
utilizați acest cod în drawable
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#3f5063"
/>
<corners android:bottomRightRadius="0dp" android:bottomLeftRadius="30dp" android:topLeftRadius="30dp" android:topRightRadius="0dp" />
<padding android:left="2dp" android:top="2dp" android:right="2dp" android:bottom="2dp" />
<gradient
android:startColor="#2ea4e7"
android:centerColor="#015664"
android:endColor="#636969"
android:angle="45"
>
</gradient>
<stroke android:color="#000000"
android:width="1dp">
</stroke>
</formă>
De ce nu a crea o imagine sau un 9 Patch-uri de imagine și de a folosi asta?
Link-ul de mai jos are un frumos ghid despre cum să faci asta:
http://android.amberfog.com/?p=247
Daca insisti sa folosesti o Forma, încercați site-ul de mai jos (Selectați Android la stânga jos): http://angrytools.com/gradient/
Am'am creat un similare gradient (nu exact) la cea pe care o ai de la acest link: http://angrytools.com/gradient/?0_6586f0,54_4B6CD6,2_D6D6D6&0_100,100_100&l_269