TextSwitcher为TextView实现平滑过渡

来源:互联网 发布:淘宝电信旗舰店 编辑:程序博客网 时间:2024/05/25 21:50

Xml文件:

<RelativeLayout 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"    tools:context="cn.five.materialapp.FourFragment">    <TextSwitcher        android:id="@+id/switcher"        android:layout_width="match_parent"        android:layout_height="48dp"        >    </TextSwitcher>    <Button        android:id="@+id/but_change"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="change"        android:layout_below="@+id/switcher"        android:layout_centerHorizontal="true"        android:layout_marginTop="8dp"        /></RelativeLayout>
代码:

public class FourFragment extends Fragment {    private View view;    private TextSwitcher switcher;    private TextView tv;    private Button change;    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container,                             Bundle savedInstanceState) {        view = inflater.inflate(R.layout.fragment_four, container, false);        initView();        return view;    }    private void initView() {        Animation in = AnimationUtils.loadAnimation(getContext(),android.R.anim.fade_in);        Animation out = AnimationUtils.loadAnimation(getContext(),android.R.anim.fade_out);        change = (Button) view.findViewById(R.id.but_change);        switcher = (TextSwitcher) view.findViewById(R.id.switcher);        switcher.setFactory(new ViewSwitcher.ViewFactory() {            @Override            public View makeView() {                tv = new TextView(getContext());                tv.setGravity(Gravity.CENTER);                tv.setText("这是一个TextViewSwitcher");                return tv;            }        });        switcher.setInAnimation(in);        switcher.setOutAnimation(out);        change.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {              switcher.setText("文字改变");            }        });    }}

0 0
原创粉丝点击