Android用動(dòng)畫實(shí)現(xiàn)閃屏效果

這幾天在接SDK的時(shí)候产雹,發(fā)現(xiàn)好多渠道都需接入閃屏,可是很多家的閃屏不是那么好翁锡,自己就寫了一套蔓挖,使用Android動(dòng)畫來(lái)實(shí)現(xiàn)閃屏,廢話不多說(shuō)直接上代碼馆衔。
先在項(xiàng)目中創(chuàng)建一個(gè)類瘟判,我這里取名就叫 WelcomeActivity
1、實(shí)現(xiàn)代碼:
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,就可以測(cè)試了减细,這樣一個(gè)不錯(cuò)的閃屏界面就出來(lái)了匆瓜,效果杠杠的,親測(cè)!M灾ā茧妒!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市左冬,隨后出現(xiàn)的幾起案子桐筏,更是在濱河造成了極大的恐慌,老刑警劉巖拇砰,帶你破解...
    沈念sama閱讀 222,946評(píng)論 6 518
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件梅忌,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡毕匀,警方通過(guò)查閱死者的電腦和手機(jī)铸鹰,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,336評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)皂岔,“玉大人蹋笼,你說(shuō)我怎么就攤上這事≡甓猓” “怎么了剖毯?”我有些...
    開封第一講書人閱讀 169,716評(píng)論 0 364
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)教馆。 經(jīng)常有香客問(wèn)我逊谋,道長(zhǎng),這世上最難降的妖魔是什么土铺? 我笑而不...
    開封第一講書人閱讀 60,222評(píng)論 1 300
  • 正文 為了忘掉前任胶滋,我火速辦了婚禮,結(jié)果婚禮上悲敷,老公的妹妹穿的比我還像新娘究恤。我一直安慰自己,他們只是感情好后德,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,223評(píng)論 6 398
  • 文/花漫 我一把揭開白布部宿。 她就那樣靜靜地躺著,像睡著了一般瓢湃。 火紅的嫁衣襯著肌膚如雪理张。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,807評(píng)論 1 314
  • 那天绵患,我揣著相機(jī)與錄音雾叭,去河邊找鬼。 笑死落蝙,一個(gè)胖子當(dāng)著我的面吹牛拷况,可吹牛的內(nèi)容都是我干的作煌。 我是一名探鬼主播,決...
    沈念sama閱讀 41,235評(píng)論 3 424
  • 文/蒼蘭香墨 我猛地睜開眼赚瘦,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼粟誓!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起起意,我...
    開封第一講書人閱讀 40,189評(píng)論 0 277
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤鹰服,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后揽咕,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體悲酷,經(jīng)...
    沈念sama閱讀 46,712評(píng)論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,775評(píng)論 3 343
  • 正文 我和宋清朗相戀三年亲善,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了设易。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,926評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡蛹头,死狀恐怖顿肺,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情渣蜗,我是刑警寧澤屠尊,帶...
    沈念sama閱讀 36,580評(píng)論 5 351
  • 正文 年R本政府宣布,位于F島的核電站耕拷,受9級(jí)特大地震影響讼昆,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜骚烧,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,259評(píng)論 3 336
  • 文/蒙蒙 一浸赫、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧赃绊,春花似錦既峡、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,750評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)炕矮。三九已至么夫,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間肤视,已是汗流浹背档痪。 一陣腳步聲響...
    開封第一講書人閱讀 33,867評(píng)論 1 274
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留邢滑,地道東北人腐螟。 一個(gè)月前我還...
    沈念sama閱讀 49,368評(píng)論 3 379
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親乐纸。 傳聞我的和親對(duì)象是個(gè)殘疾皇子衬廷,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,930評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容