我想要实现的是制作渐变效果,渐变效果从视图的底部开始,让我们说在视图高度的80%之后。 所以我希望大多数视图(80%)都包含在startColor中。
我试过了:
<gradient
android:type="linear"
android:angle="90"
android:startColor="#70C272"
android:endColor="#F19941"
android:centerX="80%"
android:centerY="100%"
/>
但它似乎不起作用。
编辑:
编辑:
使用ShapeDrawable:
ShapeDrawable.ShaderFactory sf=new ShapeDrawable.ShaderFactory() {
@Override
public Shader resize(int width, int height) {
final float DY = 60;
final float heightFraction = height * 0.8f;
return new LinearGradient(0,heightFraction-DY,0,heightFraction+DY,
// new int[] {Color.parseColor("#E44342"), Color.parseColor("#F19941")},
new int[] {Color.BLUE, Color.GREEN},
new float[]{0.5f, 1},
Shader.TileMode.REPEAT);
}
};
ShapeDrawable sd = new ShapeDrawable(new RectShape());
sd.setShaderFactory(sf);
答案 0 :(得分:1)
好吧如果你想为你的View的底部20%应用渐变,这是我的方法,
FrameLayout
作为XML根目录LinearLayout
用于渐变背景为gradient.XML 我的xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relative"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="horizontal">
// whatever your views
<LinearLayout
android:id="@+id/my_view_gradient"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/gradient_draw"
android:orientation="vertical"></LinearLayout>
</FrameLayout>
可绘制资源 gradient_draw.xml (您可以使用自己的颜色)
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
android:type="linear"
android:angle="90"
android:startColor="#70C27200"
android:endColor="#00000000"
android:centerX="80%"
android:centerY="100%"
/>
<corners android:radius="0dp" />
</shape>
现在我把它设置为20%
public class YourActivity extends Activity {
private int height,width;
private LinearLayout linearLayoutGradient ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
linearLayoutGradient = (LinearLayout) findViewById(R.id.my_view_gradient);
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
height = displaymetrics.heightPixels;
width = displaymetrics.widthPixels;
FrameLayout.LayoutParams frameLparams= new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (height*20)/100);
frameLparams.topMargin = (height*80)/100;
linearLayoutGradient.setLayoutParams(frameLparams);
}
}