android广告闪屏怎么做,Android用动画实现闪屏效果

这几天在接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 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(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、配置文件

android:name="com.xxx.xxx.xxx.WelcomeActivity" //闪屏Activity

android:label="@string/app_name"

android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >

android:name="com.xxx.xxx.xxx.MainActivity" //自己的Activity

android:label="@string/title_activity_game"

android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >

3、配置xml

activity_welcome.xml

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

tools:context="com.qianhuan.yxgsd.leshi.WelcomeActivity" >

android:id="@+id/splashId"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:contentDescription="@null" />

`

这样加上自己的MainActivity,就可以测试了,这样一个不错的闪屏界面就出来了,效果杠杠的,亲测!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值