Android Open Source - ScienceQuiz Image Adapter






From Project

Back to project page ScienceQuiz.

License

The source code is released under:

GNU General Public License

If you think the Android project ScienceQuiz listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.finger.sciencequiz;
//from w ww  . j a  va  2 s  .  c  om
import java.util.HashMap;
import java.util.Locale;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar.LayoutParams;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.content.Context;
import com.finger.sciencequiz.R;
import com.squareup.picasso.Picasso;

public class ImageAdapter extends PagerAdapter
{
    public static String     LOG_TAG   = "ImageAdapter";

    protected Context        context;
    protected LayoutInflater inflater;
    protected int[]          GalImages = new int[] {
                                 R.drawable.question_drawer_1,
                                 R.drawable.question_drawer_2,
                                 R.drawable.question_drawer_3 };

    public ImageAdapter (LayoutInflater inflater, Context context)
    {
  this.context = context;
  this.inflater = inflater;
    }

    @Override
    public int getCount ()
    {
  return GalImages.length;
    }

    @Override
    public boolean isViewFromObject (View view, Object object)
    {
  return view == ((RelativeLayout) object);
    }

    @Override
    public Object instantiateItem (ViewGroup container, int position)
    {
  RelativeLayout rootView =
                  (RelativeLayout) inflater.inflate (
                      R.layout.fragment_main_start_gallery_item,
                      container,
                      false);

  ImageView imageView = (ImageView) rootView.findViewById (
                                            R.id.drawerImageView);
  imageView.setScaleType (ScaleType.CENTER);

  Picasso.with (context)
         .load (GalImages[position])
         .placeholder (R.drawable.ic_action_refresh_dark)
         .fit ()
         .centerCrop ()
         .into (imageView);

  TextView authorView = (TextView) rootView.findViewById (
                                           R.id.authorTextView);

  // Load config
  HashMap<String, String> cfg = loadConfig (position);
  int[] gravity = getGravityFromConfig (cfg);

  // apply config
  RotationTextView textView = new RotationTextView (context, gravity);
  textView.setGravity (gravity[0]);
  textView.setPadding (getPaddingFromConfig (cfg));
  textView.setRot (getRotationFromConfig (cfg));

  textView.setTextAppearance (context, R.style.imageDrawerText);

  switch (position)
  {
  case 0:
      textView.setText (R.string.question_drawer_1_text);
      authorView.setText (
          context.getResources ().getString (R.string.image)
                          + ": "
                          + context.getResources ().getString (
                              R.string.question_drawer_1_author));
      break;
  case 1:
      textView.setText (R.string.question_drawer_2_text);
      authorView.setText (
          context.getResources ().getString (R.string.image)
                          + ": "
                          + context.getResources ().getString (
                              R.string.question_drawer_2_author));
      break;
  case 2:
      textView.setText (R.string.question_drawer_3_text);
      authorView.setText (
          context.getResources ().getString (R.string.image)
                          + ": "
                          + context.getResources ().getString (
                              R.string.question_drawer_3_author));
      break;
  default:
      textView.setText (
        context.getResources ()
               .getString (R.string.error)
               .toUpperCase (Locale.getDefault ())
                        + ": "
                        + context.getResources ().getString (
                            R.string.question_not_found));
      break;
  }

  rootView.addView (textView, getLayoutFromConfig (cfg));

  ((ViewPager) container).addView (rootView, 0);
  return rootView;
    }
    
    @Override
    public void destroyItem (ViewGroup container, int position, Object object)
    {
  ((ViewPager) container).removeView ((RelativeLayout) object);
    }

    public HashMap<String, String> loadConfig (int pos)
    {
  return ConfigReader.Read ("question_drawer/" + ++pos + ".cfg", context);
    }

    public float getRotationFromConfig (HashMap<String, String> cfg)
    {
  float rot = 0f;
  try
  {
      rot = Float.parseFloat (cfg.get ("rotation"));
  }
  catch (Exception e)
  {
      Log.w (LOG_TAG, "Rotation not valid float - using default.");
  }
  return rot;
    }

    public LayoutParams getLayoutFromConfig (HashMap<String, String> cfg)
    {
  LayoutParams l;
  try
  {
      int width = getLayout (cfg.get ("layout_width"));
      int height = getLayout (cfg.get ("layout_height"));
      l = new LayoutParams (width, height);
  }
  catch (Exception e)
  {
      Log.w (
    LOG_TAG, "Failed to get layout from config - using default.");
      l = new LayoutParams (
    LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
  }
  return l;
    }

    public int[] getGravityFromConfig (HashMap<String, String> cfg)
    {
  int[] g;
  try
  {
      String sgr = cfg.get ("gravity");
      String[] agr = sgr.split ("\\|");
      g = new int[agr.length + 1];
      for (int i = 1; i < g.length; i++)
      {
    g[i] = getGravity (agr[i - 1].trim ());
    g[0] = g[0] | g[i];
      }
  }
  catch (Exception e)
  {
      Log.w (
    LOG_TAG, "Failed to get gravity from config - using default.");
      e.printStackTrace ();
      g = new int[] { Gravity.LEFT | Gravity.BOTTOM,
        Gravity.LEFT, Gravity.BOTTOM };
  }
  return g;
    }

    public String[] getPaddingFromConfig (HashMap<String, String> cfg)
    {
  String[] pad = new String[0];
  try
  {
      pad = cfg.get ("padding").split (",");

      if (pad.length > 4)
    throw new Exception ();
  }
  catch (Exception e)
  {
      Log.w (LOG_TAG, "Padding not valid - using default.");
  }
  return pad;
    }

    protected int getGravity (String s) throws IllegalAccessException,
  IllegalArgumentException, NoSuchFieldException
    {
  return Gravity.class.getField (s.toUpperCase (Locale.getDefault ()))
                      .getInt (null);
    }

    protected int getLayout (String s) throws IllegalAccessException,
  IllegalArgumentException, NoSuchFieldException
    {
  return LayoutParams.class.getField (
      s.toUpperCase (Locale.getDefault ())).getInt (null);
    }
}




Java Source Code List

com.finger.sciencequiz.AboutActivity.java
com.finger.sciencequiz.ConfigReader.java
com.finger.sciencequiz.FriendsHomeFragment.java
com.finger.sciencequiz.GalleryViewPager.java
com.finger.sciencequiz.GameActivity.java
com.finger.sciencequiz.GameTypeSelectActivity.java
com.finger.sciencequiz.HistoryActivity.java
com.finger.sciencequiz.HomePagerAdapter.java
com.finger.sciencequiz.ImageAdapter.java
com.finger.sciencequiz.LobbyActivity.java
com.finger.sciencequiz.MainActivity.java
com.finger.sciencequiz.NavigationDrawerFragment.java
com.finger.sciencequiz.NewGameActivity.java
com.finger.sciencequiz.ProfileHomeFragment.java
com.finger.sciencequiz.RotationTextView.java
com.finger.sciencequiz.RunningGamesHomeFragment.java
com.finger.sciencequiz.SettingsActivity.java
com.finger.sciencequiz.StartHomeFragment.java