上下滚动自定义广告条ViewGroup

工作日志记录,记录本文的初衷是用来抛砖引玉,希望对自己以后有用,对各位看官也有用。。。

惯例上图:

上下滚动自定义广告条ViewGroup_第1张图片

上代码:

package com.xuganwen.testhichart;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

/**
 * 文件描述:
 * 作者:徐干稳
 * 创建时间:2020/1/9
 * 更改时间:2020/1/9
 * 版本号:1.0
 */
public class AdvertisementView extends ViewGroup {

    private int childCount;


    float value;
    public AdvertisementView(Context context) {
        super(context);
    }

    public AdvertisementView(Context context, AttributeSet attrs) {
        super(context, attrs);
        ValueAnimator valueAnimator = new ValueAnimator();
        valueAnimator.setFloatValues(0,0,1,1,2,2);
        valueAnimator.setDuration(7000);
        valueAnimator.setRepeatCount(ValueAnimator.INFINITE);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                value = (float) animation.getAnimatedValue();
                scrollTo(0,(int)(getMeasuredHeight()*value));
            }
        });
        valueAnimator.start();
    }

    public AdvertisementView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            measureChild(getChildAt(i), widthMeasureSpec, heightMeasureSpec);
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {

        for (int i = 0; i < childCount; i++) {
            View child = getChildAt(i);
            if (null != child) {
                child.layout(l, getMeasuredHeight() * i, r, getMeasuredHeight() * (i + 1));
            }
        }
    }


}

MainActivity.java中使用如下:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        /*webview = findViewById(R.id.webview);
        btn=findViewById(R.id.btn);

        btn.setOnClickListener(this);
        initWebView();*/

        roundview = findViewById(R.id.roundview);
        roundview.setPriority("hehe",80f, RoundRectView.RadiusOrientation.RIGHT_TO_LEFT);

        advertise= findViewById(R.id.advertise);
        for (int i=0;i<3;i++){
            TextView textView=new TextView(this);
            textView.setText("我擦擦擦擦擦擦擦擦"+i);
            advertise.addView(textView);
        }
    }

虽然这里只使用了textview作为填充控件,但其实可以用大部分的控件作为子控件 。这个自定义viewgroup实际上最关键的地方就是onlayout,就是将子控件摆放好,然后执行动画,使用scrollto()就行了。

 

 

 

你可能感兴趣的:(移动开发)