Implementation method of Android software starting animation and jumping after animation

  • 2020-10-07 18:52:29
  • OfStack

An example of Android software startup animation and the implementation of the end of animation jump method. To share for your reference, the details are as follows:

I wrote a small program, the software started, first show a few pictures, every 3 seconds to show 1, the picture shows jump to the home page

1. Gallery is used for image broadcast, which is very simple


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

GalleryAdapter is a custom adapter


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);// Center the image 
    imageView.setLayoutParams(new Gallery.LayoutParams(
      Gallery.LayoutParams.FILL_PARENT, mImageHeight));
    Bitmap bitmap = readBitMap(mContext, imageId);// Use memory saving way to load the image, prevent OOM
    imageView.setImageBitmap(bitmap);
    overridePendingTransition(R.anim.push_in, R.anim.push_out);// Image toggle animation 
   }
   return imageView;
  }
}

2. Set image switching time using Timer timer


Timer timer = new Timer();
timer.schedule(task, 3000, 3000);//  every 3 Seconds to switch 1 image 
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);
   }
  }
};
// Not directly in task In the update UI , so use handler Send a message to the main thread 
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. Read the local resource image


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;
  //  Get resource images 
  InputStream is = context.getResources().openRawResource(resId);
  return BitmapFactory.decodeStream(is, null, opt);
}

Click here to download the complete example code.

I hope this article has been helpful in Android programming.


Related articles: