使用项目Constellation
1.xml布局
<RelativeLayout
android:id="@+id/background_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/shapel_bg1"
android:clipChildren="false"
android:clipToPadding="false"
android:visibility="invisible" />
<ImageView
android:id="@+id/backgroundcolr"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/shapel_bg1"
android:clipChildren="false"
android:clipToPadding="false" />
2.自定义
public class CircleView extends View {
private boolean isLeft = true;
private Point point;
private int circleRadius;
private int startColor;
private int endColor;
Paint paint = new Paint();
public CircleView(Context context, int startColor, int endColor, int circleRadius) {
super(context);
WindowManager wm = (WindowManager) context.getSystemService(Context.
WINDOW_SERVICE
);
int windowWidth = wm.getDefaultDisplay().getWidth();
point = new Point();
this.circleRadius = circleRadius;
this.startColor = startColor;
this.endColor = endColor;
//绘制中心确定,可根据需要修改
point.cx = windowWidth / 2;
point.cy = 0;
}
public CircleView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CircleView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//设置渐变
RadialGradient lg=new RadialGradient(point.cx, point.cy,circleRadius,startColor,endColor, Shader.TileMode.
REPEAT
);
paint.setShader(lg);
canvas.drawCircle(point.cx, point.cy, circleRadius, paint);
}
class Point {
float cx;
float cy;
int color;
}
}
3.代码设置
WindowManager wm = (WindowManager) context.getSystemService(Context.
WINDOW_SERVICE
);
int windowWidth = wm.getDefaultDisplay().getWidth();
int windowHeight = wm.getDefaultDisplay().getHeight();
//
背景颜色切换动画
public void backgroundAnimation(final int position, boolean location) {
//Context, 中心颜色,边缘颜色, 初始半径
CircleView circleView_1 = new CircleView(context, ints[0], ints[1], 10);
background_view.removeAllViews();
background_view.setVisibility(View.
VISIBLE
);
findViewById(R.id.
backgroundcolr
).setVisibility(View.
INVISIBLE
);
background_view.addView(circleView_1);
int v = (int) (Math.sqrt(Math.pow(windowHeight, 2) + Math.pow(windowWidth, 2)) / 10);
//缩放动画
ObjectAnimator circleView1_scaleX = ObjectAnimator.ofFloat(circleView_1, "scaleX", 0f, v);
ObjectAnimator circleView1_scaleY = ObjectAnimator.ofFloat(circleView_1, "scaleY", 0f, v);
//缩放中心
circleView_1.setPivotX(windowWidth / 2);
circleView_1.setPivotY(0);
DecelerateInterpolator interpolator = new DecelerateInterpolator(1.2f);
AnimatorSet set = new AnimatorSet();
set.setDuration(400);
set.setInterpolator(interpolator);
set.playTogether(circleView1_scaleX, circleView1_scaleY);
set.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
Logger.e("position__bg", position + "");
setbackground(position);
animationDoing = false;
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
set.start();
}