其他分享
首页 > 其他分享> > Android APIDemo之条目随机淡入动画效果

Android APIDemo之条目随机淡入动画效果

作者:互联网

效果:

这里演示的是加载系统所有的app图标:

首先Activity的实现:

package com.jwx.frescodemo;


import android.app.Activity;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import java.util.List;

public class LayoutAnimation4 extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        loadApps();
        setContentView(R.layout.layout_animation_4);
        GridView grid = (GridView) findViewById(R.id.grid);
        grid.setAdapter(new AppsAdapter());

    }

    private List<ResolveInfo> mApps;

    private void loadApps() {
        Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        mApps = getPackageManager().queryIntentActivities(mainIntent, 0);
    }

    public class AppsAdapter extends BaseAdapter {
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView i = new ImageView(LayoutAnimation4.this);

            ResolveInfo info = mApps.get(position % mApps.size());

            i.setImageDrawable(info.activityInfo.loadIcon(getPackageManager()));
            i.setScaleType(ImageView.ScaleType.FIT_CENTER);
            final int w = (int) (36 * getResources().getDisplayMetrics().density + 0.5f);
            i.setLayoutParams(new GridView.LayoutParams(w, w));
            return i;
        }


        public final int getCount() {
            return Math.min(32, mApps.size());
        }

        public final Object getItem(int position) {
            return mApps.get(position % mApps.size());
        }

        public final long getItemId(int position) {
            return position;
        }
    }
}

布局:

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/grid"
    android:layoutAnimation="@anim/layout_random_fade"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:numColumns="auto_fit"
    android:columnWidth="60dp"
    android:stretchMode="columnWidth"
    android:gravity="center" />

注意这里有个属性:

android:layoutAnimation="@anim/layout_random_fade"

 需要新建一个布局动画layout_random_fade.xml:

<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
        android:delay="0.5"
        android:animationOrder="random"
        android:animation="@anim/fade" />

观察属性:

android:animationOrder="random" // 随机效果,另外有normal,reverse可选

android:animation="@anim/fade"// 淡入动画

ps 可以查看系统源码animationOrder相关的属性值有哪些:

 <!-- The order in which the animations will be started. -->
        <attr name="animationOrder">
            <!-- Animations are started in the natural order. -->
            <enum name="normal" value="0" />
            <!-- Animations are started in the reverse order. -->
            <enum name="reverse" value="1" />
            <!-- Animations are started randomly. -->
            <enum name="random" value="2" />
        </attr>

 

需要新建一个动画fade.xml:

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
       android:interpolator="@android:anim/accelerate_interpolator"
       android:fromAlpha="0.0" android:toAlpha="1.0"
       android:duration="@android:integer/config_longAnimTime" />

end!

标签:淡入,APIDemo,int,mApps,position,import,Android,public,android
来源: https://blog.csdn.net/qq_17441227/article/details/115247808