RadiodButton切换Fragment,切换图片帧动画显示

2018-11-01  本文已影响0人  That丶Alex

<?xml version="1.0" encoding="utf-8"?><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" android:orientation="vertical" tools:context="com.example.nineteenclass.MainActivity"> <FrameLayout android:layout_width="match_parent" android:layout_height="480dp" android:id="@+id/fragment"></FrameLayout> <RadioGroup android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/rg" android:layout_below="@id/fragment" android:layout_alignParentBottom="true" android:orientation="horizontal"> <RadioButton android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="首页" android:id="@+id/but1" android:button="@null" android:gravity="center_horizontal" android:drawableTop="@drawable/bt_ok1"/> <RadioButton android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:button="@null" android:text="授课" android:gravity="center_horizontal" android:id="@+id/but2" android:drawableTop="@drawable/bt_ok2"/> <RadioButton android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="今日直播" android:button="@null" android:gravity="center_horizontal" android:id="@+id/live" android:drawableTop="@drawable/bt_ok3"/> <RadioButton android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center_horizontal" android:text="缓存" android:button="@null" android:id="@+id/but3" android:background="@drawable/four" android:drawableTop="@drawable/bt_ok4"/> <RadioButton android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center_horizontal" android:text="我的" android:button="@null" android:id="@+id/but4" android:background="@drawable/my" android:drawableTop="@drawable/bt_ok5"/> </RadioGroup></RelativeLayout>

图片绑定的一定要是动画的布局,bt_ok1

Activity代码

public class MainActivityextends AppCompatActivityimplements View.OnClickListener {

private FrameLayoutmFrame;

private RadioButtonbut1;

private RadioButtonbut2;

private RadioButtonbut3;

private RadioButtonbut4;

private RadioButtonlive;

@Override

    protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

initView();

}

private void initView() {

mFrame = (FrameLayout) findViewById(R.id.fragment);

mFrame.setOnClickListener(this);

//        mLinear = (LinearLayout) findViewById(R.id.mLinear);

//        mLinear.setOnClickListener(this);

        but1 = (RadioButton) findViewById(R.id.but1);

but1.setOnClickListener(this);

but2 = (RadioButton) findViewById(R.id.but2);

but2.setOnClickListener(this);

but3 = (RadioButton) findViewById(R.id.but3);

but3.setOnClickListener(this);

but4 = (RadioButton) findViewById(R.id.but4);

but4.setOnClickListener(this);

live = (RadioButton) findViewById(R.id.live);

live.setOnClickListener(this);

}

private void initFragment(Fragment fragment) {

FragmentManager supportFragmentManager = getSupportFragmentManager();

FragmentTransaction transaction = supportFragmentManager.beginTransaction();

transaction.replace(R.id.fragment, fragment);

transaction.commit();

}

@Override

    public void onClick(View v) {

switch (v.getId()) {

case R.id.but1:

AFragment aFragment =new AFragment();

initFragment(aFragment);

break;

case R.id.but2:

BFragment bFragment =new BFragment();

initFragment(bFragment);

break;

case R.id.but3:

CFragment cFragment =new CFragment();

initFragment(cFragment);

break;

case R.id.but4:

DFragment dFragment =new DFragment();

initFragment(dFragment);

break;

case R.id.live:

TaskFragment taskFragment =new TaskFragment();

initFragment(taskFragment);

break;

}

}

}

二:在drawable中创建动画

    先把图片加入到mipmap  


起的动画名      bt_ok1

<?xml version="1.0" encoding="utf-8"?><animated-selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/state_on" android:drawable="@mipmap/tab_btn_01_def" android:state_checked="false" /> <item android:id="@+id/state_off" android:drawable="@mipmap/tab_btn_01_sel_00011" android:state_checked="true" /> <transition android:fromId="@id/state_on" android:toId="@id/state_off"> <animation-list android:oneshot="true"> <item android:drawable="@mipmap/tab_btn_01_sel_00001" android:duration="30" /> <item android:drawable="@mipmap/tab_btn_01_sel_00002" android:duration="100" /> <item android:drawable="@mipmap/tab_btn_01_sel_00003" android:duration="100" /> <item android:drawable="@mipmap/tab_btn_01_sel_00004" android:duration="100" /> <item android:drawable="@mipmap/tab_btn_01_sel_00005" android:duration="100" /> <item android:drawable="@mipmap/tab_btn_01_sel_00006" android:duration="100" /> <item android:drawable="@mipmap/tab_btn_01_sel_00007" android:duration="100" /> <item android:drawable="@mipmap/tab_btn_01_sel_00008" android:duration="100" /> <item android:drawable="@mipmap/tab_btn_01_sel_00009" android:duration="100" /> <item android:drawable="@mipmap/tab_btn_01_sel_00010" android:duration="100" /> <item android:drawable="@mipmap/tab_btn_01_sel_00011" android:duration="100" /> </animation-list> </transition></animated-selector>

报错,直接生成文件

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:drawable="@drawable/shouye" android:state_checked="true"></item> <item android:drawable="@mipmap/tab_btn_01_def"></item></selector>

再建一个XML,才是真正的显示动画

<?xml version="1.0" encoding="utf-8"?><animation-list android:oneshot="true" xmlns:android="http://schemas.android.com/apk/res/android" > <item android:drawable="@mipmap/tab_btn_01_sel_00001" android:duration="30" /> <item android:drawable="@mipmap/tab_btn_01_sel_00002" android:duration="100" /> <item android:drawable="@mipmap/tab_btn_01_sel_00003" android:duration="100" /> <item android:drawable="@mipmap/tab_btn_01_sel_00004" android:duration="100" /> <item android:drawable="@mipmap/tab_btn_01_sel_00005" android:duration="100" /> <item android:drawable="@mipmap/tab_btn_01_sel_00006" android:duration="100" /> <item android:drawable="@mipmap/tab_btn_01_sel_00007" android:duration="100" /> <item android:drawable="@mipmap/tab_btn_01_sel_00008" android:duration="100" /> <item android:drawable="@mipmap/tab_btn_01_sel_00009" android:duration="100" /> <item android:drawable="@mipmap/tab_btn_01_sel_00010" android:duration="100" /> <item android:drawable="@mipmap/tab_btn_01_sel_00011" android:duration="100" /></animation-list>

上一篇 下一篇

猜你喜欢

热点阅读