美文网首页
Android用动画实现闪屏效果

Android用动画实现闪屏效果

作者: IcodeIlife | 来源:发表于2016-10-14 18:08 被阅读0次

    这几天在接SDK的时候,发现好多渠道都需接入闪屏,可是很多家的闪屏不是那么好,自己就写了一套,使用Android动画来实现闪屏,废话不多说直接上代码。
    先在项目中创建一个类,我这里取名就叫 WelcomeActivity
    1、实现代码:
    public class WelcomeActivity extends Activity {

    private Handler mHandler = new Handler();
    private ImageView splashImageView;
    private Bitmap imageViewBitmap = null;
    private int orientation;
    private LruCache<String, Bitmap> mMemoryCache;
    
    private int screenWidth;
    private int screenHeight;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
    
        int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
        int cacheSize = maxMemory / 8;
        mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
            @Override
            protected int sizeOf(String key, Bitmap bitmap) {
                return bitmap.getByteCount() / 1024;
            }
        };
        splashImageView = (ImageView) findViewById(R.id.splashId);
        orientation = getResources().getConfiguration().orientation;
        if (orientation == Configuration.ORIENTATION_PORTRAIT) {
            imageViewBitmap = getBitmap(R.drawable.letv_gamesdk_logo_port);
        } else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
            imageViewBitmap = getBitmap(R.drawable.letv_gamesdk_logo_land);
        }
        loadSplashIn();
    }
    
    public void addBitmapToMemoryCache(String key, Bitmap bitmap) {
        if (getBitmapFromMemCache(key) == null) {
            mMemoryCache.put(key, bitmap);
        }
    }
    
    public Bitmap getBitmapFromMemCache(String key) {
        return mMemoryCache.get(key);
    }
    
    public void loadSplashIn() {
        AlphaAnimation alphaAnimation_in = new AlphaAnimation(0.0f, 1.0f);
        alphaAnimation_in.setDuration(2000);
        alphaAnimation_in.setAnimationListener(new AnimationListener() {
    
            @Override
            public void onAnimationStart(Animation animation) {
                // TODO Auto-generated method stub
                if (splashImageView != null) {
                    splashImageView.setImageBitmap(imageViewBitmap);
                }
            }
    
            @Override
            public void onAnimationRepeat(Animation animation) {
                // TODO Auto-generated method stub
    
            }
    
            @Override
            public void onAnimationEnd(Animation animation) {
                // TODO Auto-generated method stub
    
                mHandler.postDelayed(new Runnable() {
    
                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        loadSplashOut();
                    }
                }, 1500);
            }
        });
    
        splashImageView.startAnimation(alphaAnimation_in);
    
    }
    
    public Bitmap getBitmap(int id) {
    
        Bitmap bitmap = getBitmapFromMemCache("splash");
        if (bitmap != null) {
            return bitmap;
        }
        WindowManager wm = this.getWindowManager();
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        screenWidth = dm.widthPixels;
        screenHeight = dm.heightPixels;
        bitmap = decodeSampledBitmapFromResource(getResources(), id, screenWidth, screenHeight);
    
        addBitmapToMemoryCache("splash", bitmap);
    
        return bitmap;
    }
    
    public void loadSplashOut() {
        AlphaAnimation alphaAnimation_out = new AlphaAnimation(1.0f, 0.0f);
        alphaAnimation_out.setDuration(1000);
        alphaAnimation_out.setAnimationListener(new AnimationListener() {
    
            @Override
            public void onAnimationStart(Animation animation) {
                // TODO Auto-generated method stub
    
            }
    
            @Override
            public void onAnimationRepeat(Animation animation) {
                // TODO Auto-generated method stub
    
            }
    
            @Override
            public void onAnimationEnd(Animation animation) {
                // TODO Auto-generated method stub
                splashImageView.setImageBitmap(null);
                Intent intent = new Intent();
                intent.setClass(getApplicationContext(), MainActivity.class);
                startActivity(intent);
                WelcomeActivity.this.finish();
                imageViewBitmap.recycle();
                imageViewBitmap = null;
            }
        });
        splashImageView.startAnimation(alphaAnimation_out);
    }
    
    public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;
        if (height > reqHeight || width > reqWidth) {
            if (width > height) {
                inSampleSize = Math.round((float) height / (float) reqHeight);
            } else {
                inSampleSize = Math.round((float) width / (float) reqWidth);
            }
        }
        return inSampleSize;
    }
    
    public Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) {
    
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(res, resId, options);
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeResource(res, resId, options);
    }
    

    }

    2、配置文件

    <activity android:name="com.xxx.xxx.xxx.WelcomeActivity" //闪屏Activity android:label="@string/app_name" android:theme="@android:style/Theme.NoTitleBar.Fullscreen" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="com.xxx.xxx.xxx.MainActivity" //自己的Activity android:label="@string/title_activity_game" android:theme="@android:style/Theme.NoTitleBar.Fullscreen" > </activity>

    3、配置xml
    activity_welcome.xml
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" tools:context="com.qianhuan.yxgsd.leshi.WelcomeActivity" >
    <ImageView android:id="@+id/splashId" android:layout_width="fill_parent" android:layout_height="fill_parent" android:contentDescription="@null" />
    </RelativeLayout>
    `
    这样加上自己的MainActivity,就可以测试了,这样一个不错的闪屏界面就出来了,效果杠杠的,亲测!!!

    相关文章

      网友评论

          本文标题:Android用动画实现闪屏效果

          本文链接:https://www.haomeiwen.com/subject/nifnyttx.html