我需要创建一个像图片中那样的多色背景。我知道所有的颜色和角度。
我尝试用SweepGradient做到这一点,但没有任何改变,我只看到白色背景。
我的布局
<RelativeLayout
android:id="@+id/user_info_background"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
片段中的代码:
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mRelativeLayout.setBackground(GraphicalUtils.addSweepGradientBackground(getResources().getDisplayMetrics().widthPixels,
getResources().getDisplayMetrics().heightPixels,getContext()));}
添加SweepGradient的方法:
public static Drawable addSweepGradientBackground(int width, int height, Context context){
Bitmap bitmap = Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas();
SweepGradient sweepGradient = new SweepGradient(width/2,height/2,Color.RED, Color.BLUE);
Paint paint = new Paint();
paint.setShader(sweepGradient);
paint.setStyle(Paint.Style.FILL);
paint.setAntiAlias(true);
canvas.drawBitmap(bitmap,0,0,paint);
return new BitmapDrawable(context.getResources(), bitmap);
}
答案 0 :(得分:1)
试试这个:
public static Drawable addSweepGradientBackground(int width, int height, Context context){
Bitmap bitmap = Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
SweepGradient sweepGradient = new SweepGradient(width/2,height/2,Color.RED, Color.BLUE);
Paint paint = new Paint();
paint.setShader(sweepGradient);
paint.setStyle(Paint.Style.FILL);
paint.setAntiAlias(true);
canvas.drawBitmap(bitmap,0,0,null);
canvas.drawCircle(width/2,height/2, width/2, paint)
return new BitmapDrawable(context.getResources(), bitmap);
}
画布应该用位图绑定,你需要在位图上绘制具有sweepGradient的paint的圆圈。