背景动画(圆形扩展)

使用项目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();
    }


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值