不断向上滚动的公告栏

不知道该如何控制TextView的 TextSize 和TextView大小
用inflate 填充xml布局?
求大神指点下,为什么我获取我的控件的高设置给TextView ,TextView却比期望的大出好多

不断向上滚动的公告栏_第1张图片
ddd.gif

使用


  MyViewFlipper myViewFlipper = (MyViewFlipper) findViewById(R.id.mvf);
        myViewFlipper.setStrings(titleList);


        myViewFlipper.setOnItemClickListener(new MyViewFlipper.ItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Toast.makeText(ViewFlipperActivity.this, "position" + position, Toast.LENGTH_SHORT).show();
            }
        });

/**
 * desc:
 * author: pdog
 * email: [email protected]
 * time: 2017/3/21  14 :33
 */
public class MyViewFlipper extends ViewFlipper implements View.OnClickListener {

    private String DEFAULT_MESSAGE = "Default Message";
    private int mCurrPos;       //当前显示的
    private ArrayList mStrings;
    private Timer mTimer = new Timer();
    private TextView mTextView;

    TimerTask task = new TimerTask() {
        @Override
        public void run() {
            post(new Runnable() {
                @Override
                public void run() {
                    moveNext();
                }
            });
        }
    };

    TranslateAnimation in = new TranslateAnimation(
            Animation.RELATIVE_TO_SELF, 0.0f,
            Animation.RELATIVE_TO_SELF, 0.0f,
            Animation.RELATIVE_TO_SELF, 1.0f,
            Animation.RELATIVE_TO_SELF, 0.0f);

    TranslateAnimation out = new TranslateAnimation(
            Animation.RELATIVE_TO_SELF, 0.0f,
            Animation.RELATIVE_TO_SELF, 0.0f,
            Animation.RELATIVE_TO_SELF, 0.0f,
            Animation.RELATIVE_TO_SELF, -1.0f);

    public MyViewFlipper(Context context) {
        this(context, null);
    }

    public MyViewFlipper(Context context, AttributeSet attrs) {
        super(context, attrs);
        setOnClickListener(this);
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        in.setDuration(500);
        out.setDuration(500);
        mTimer.schedule(task, 0, 4000);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        mTimer.cancel();
    }

    /**
     * 需要显示的数组
     *
     * @param strings
     */
    public void setStrings(@NonNull ArrayList strings) {
        if (strings == null) {
            throw new NullPointerException("传入的集合为null");
        }
        this.mStrings = strings;
    }


    private void moveNext() {
        setView(mCurrPos, mCurrPos + 1);
        setInAnimation(in);
        setOutAnimation(out);
        showNext();
    }

  private void setView(int curr, int next) {
        if (mStrings == null || mStrings.size() == 0) {
            if (mTextView == null) {
                mTextView = new TextView(getContext());
                addView(mTextView);
            }
            mTextView.setText(DEFAULT_MESSAGE);
            return;
        }
        View view = LayoutInflater.from(getContext()).inflate(R.layout.viewflipper_item, this, false);
        ImageView image = (ImageView) view.findViewById(R.id.img_viewflipper_item);
        TextView tv = (TextView) view.findViewById(R.id.tv_viewflipper_item);

        if ((curr < next) && (next > (mStrings.size() - 1))) {
            next = 0;
        } else if ((curr > next) && (next < 0)) {
            next = mStrings.size() - 1;
        }
        tv.setText(mStrings.get(next));
        if (getChildCount() > 1) {
            removeViewAt(0);
        }
        addView(view, getChildCount());
        mCurrPos = next;
    }

    ItemClickListener mItemClickListener;

    public void setOnItemClickListener(ItemClickListener listener) {
        this.mItemClickListener = listener;
    }

    interface ItemClickListener {
        void onItemClick(int position);
    }

    @Override
    public void onClick(View v) {
        if (mItemClickListener != null) {
            mItemClickListener.onItemClick(mCurrPos);
        }
    }
}



\

    

    

你可能感兴趣的:(不断向上滚动的公告栏)