listview展示:帧动画各种动画实现

Activity

public class MainActivity extends AppCompatActivity {

    private ListView mylist;
    private ImageView myimg;
    private ImageView myimg2;
    private List<String> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //找控件
        mylist = (ListView) findViewById(R.id.mylist);
        myimg = (ImageView) findViewById(R.id.myimg);
        myimg2 = (ImageView) findViewById(R.id.myimg2);
        list = new ArrayList<>();
        list.add("帧布局动画");
        list.add("屏幕中间到屏幕下方");
        list.add("半透明到完全显示");
        list.add("本身大小3倍动画");
        list.add("左侧移除动画");
        mylist.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, list));
        mylist.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            private AnimationDrawable drawable;

            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {
                switch (position){
                    case 0:
                        myimg2.setVisibility(View.GONE);
                        myimg.setVisibility(View.VISIBLE);
                        myimg.setImageResource(R.drawable.myanim);
                        drawable = (AnimationDrawable)myimg.getDrawable();
                        drawable.start();
                        break;
                    case 1:
                        myimg.setVisibility(View.GONE);
                        myimg2.setVisibility(View.VISIBLE);
                        TranslateAnimation tr=new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,2f);
                        tr.setDuration(3000);
                        myimg2.startAnimation(tr);
                        break;
                    case 2:
                        myimg.setVisibility(View.GONE);
                        myimg2.setVisibility(View.VISIBLE);
                        AlphaAnimation al=new AlphaAnimation(0.5f,1f);
                        al.setDuration(3000);
                        myimg2.startAnimation(al);
                        break;
                    case 3:
                        myimg.setVisibility(View.GONE);
                        myimg2.setVisibility(View.VISIBLE);
                        ScaleAnimation ro=new ScaleAnimation(Animation.RELATIVE_TO_SELF,3,Animation.RELATIVE_TO_SELF,3);
                        ro.setDuration(3000);
                        myimg2.startAnimation(ro);
                        break;
                    case 4:
                        myimg.setVisibility(View.GONE);
                        myimg2.setVisibility(View.VISIBLE);
                        Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.myset);
                        myimg2.startAnimation(animation);
                        break;
                }
            }
        });
    }
}

 XML


xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent"
    tools:context="com.bwie.fragment.MainActivity">

    <ListView
        android:id="@+id/mylist"
        android:layout_width="match_parent"
        android:layout_height="300dp"
         />
    <ImageView
        android:id="@+id/myimg"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:visibility="gone"
        android:layout_gravity="center_horizontal"
        />
    <ImageView
        android:id="@+id/myimg2"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="center_horizontal"
        android:src="@drawable/a"
        />

LinearLayout>

 anim文件

xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="3000"
    android:fillAfter="true"
    >
    <rotate
        android:fromDegrees="360"
        android:toDegrees="0"
        android:pivotY="50%"
        android:pivotX="50%"
        android:duration="1000"
        >rotate>
    <translate
        android:fromXDelta="0"
        android:toXDelta="-1000"
        >translate>

set>

Drawable

xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false"
    >

    <item android:drawable="@drawable/a" android:duration="50">item>
    <item android:drawable="@drawable/b" android:duration="50">item>
    <item android:drawable="@drawable/c" android:duration="50">item>
    <item android:drawable="@drawable/d" android:duration="50">item>
    <item android:drawable="@drawable/e" android:duration="50">item>
    <item android:drawable="@drawable/f" android:duration="50">item>
    <item android:drawable="@drawable/g" android:duration="50">item>
    <item android:drawable="@drawable/h" android:duration="50">item>

animation-list>

你可能感兴趣的:(android-studio)