Android软件启动动画及动画结束后跳转的实现方法

内容摘要
本文实例讲述了Android软件启动动画及动画结束后跳转的实现方法。分享给大家供大家参考,具体如下:
自己写了个小程序,软件启动时,先显示几张图片,每3秒显示一张,图片显示完跳转到
文章正文

本文实例讲述了Android软件启动动画及动画结束后跳转的实现方法。分享给大家供大家参考,具体如下:

自己写了个小程序,软件启动时,先显示几张图片,每3秒显示一张,图片显示完跳转到首页

1. 图片轮播使用Gallery,用法很简单

GalleryAdapter adapter = new GalleryAdapter(this, mIds);
mGallery.setAdapter(adapter);

GalleryAdapter是自定义适配器

public class GalleryAdapter extends BaseAdapter {
  private Context mContext;
  private int mImageHeight;
  private int[] mIds;
  public GalleryAdapter(Context context, int[] ids) {
   this.mContext = context;
   this.mIds = ids;
   init();
  }
  private void init() {
   mImageHeight = px2dip(mContext, getScreenHeight(MainActivity.this));
  }
  @Override
  public int getCount() {
   return mIds.length;
  }
  @Override
  public Object getItem(int position) {
   return position;
  }
  @Override
  public long getItemId(int position) {
   return position;
  }
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
   final ImageView imageView = new ImageView(mContext);
   if (position < mIds.length) {
    int imageId = mIds[position];
    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);//设置图片居中
    imageView.setLayoutParams(new Gallery.LayoutParams(
      Gallery.LayoutParams.FILL_PARENT, mImageHeight));
    Bitmap bitmap = readBitMap(mContext, imageId);//用节省内存的方式加载图片,防止OOM
    imageView.setImageBitmap(bitmap);
    overridePendingTransition(R.anim.push_in, R.anim.push_out);//图片切换动画
   }
   return imageView;
  }
}

2. 设置图片切换时间使用Timer定时器

Timer timer = new Timer();
timer.schedule(task, 3000, 3000);// 每3秒切换一张图片
private TimerTask task = new TimerTask() {
  @Override
  public void run() {
   Message message = new Message();
   message.what = 0;
   index = mGallery.getSelectedItemPosition();
   handler.sendMessage(message);
   index++;
   if (index == mIds.length - 1) {
    this.cancel();
    MainActivity.this.finish();
    Intent intent = new Intent(MainActivity.this, Test.class);
    startActivity(intent);
   }
  }
};
//不能直接在task中更新UI,所以用handler向主线程发送消息
private Handler handler = new Handler() {
  @Override
  public void handleMessage(Message msg) {
   super.handleMessage(msg);
   switch (msg.what) {
   case 0:
    mGallery.setSelection(index);
    break;
   default:
    break;
   }
  }
};

3. 读取本地资源图片

public static Bitmap readBitMap(Context context, int resId) {
  BitmapFactory.Options opt = new BitmapFactory.Options();
  opt.inPreferredConfig = Bitmap.Config.RGB_565;
  opt.inPurgeable = true;
  opt.inInputShareable = true;
  // 获取资源图片
  InputStream is = context.getResources().openRawResource(resId);
  return BitmapFactory.decodeStream(is, null, opt);
}

完整实例代码代码点击此处本站下载。

希望本文所述对大家Android程序设计有所帮助。


代码注释

作者:喵哥笔记

IDC笔记

学的不仅是技术,更是梦想!