zoukankan      html  css  js  c++  java
  • LearnGallery2Activity.java

    package xilifeng.com.weibo;

    import android.app.Activity;

    import android.content.Context;

    import android.graphics.Color;

    import android.os.Bundle;

    import android.view.View;

    import android.view.ViewGroup;

    import android.widget.BaseAdapter;

    import android.widget.Gallery;

    import android.widget.ImageView;

    import android.widget.TextView;

    publicclass LearnGallery2Activity extends Activity {

        private TextView tv;

        /** Called when the activity is first created. */

        @Override

        publicvoid onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);

            setContentView(R.layout.main);

           

            /*Find the ID of TextView and Gallery .*/

            tv = ( TextView ) findViewById(R.id.m_tv);

            tv.setText(getString(R.string.text));

            tv.setTextColor(Color.BLUE);

           

            /* add an Adapte to Gallery */

            ((Gallery)findViewById(R.id.m_gallery))

            .setAdapter( new ImageAapter(this) );

        }

        /*Context

         * Interface to global information about an application environment.

         *  This is an abstract class whose implementation is provided by the Android system.

         *  It allows access to application-specific resources and classes,

         *  as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc.

         *  一个对应用环境全局信息的接口.这个抽象类的实现由android系统提供.

         *  它提供了对特定应用资源,,以及对如启动Activity(),broadcast(广播),接收intents(意图)等应用级别的操作,等的访问.

         * */

         publicclass ImageAapter extends BaseAdapter

            {

            /* the class member (context) is the sub class Context .  */

            private Context context ;

           

            /* Create an integer array to store images which are found by using #android.R.drawble#

             * If we want to use the image in the floder "res/drawalbe ", we can write #R.drawable.[image name]#

             * when I tested the second method, I noticed that the images should not too  large to handle  for the AVD,

             * or the install process could be very long , or even can  not run any more .

             * */

            privateint [] myImageIds =

                 {

                    android.R.drawable.ic_menu_add,

                    android.R.drawable.ic_dialog_map,

                    android.R.drawable.sym_call_incoming,

                    android.R.drawable.sym_call_missed

                    //R.drawable.image2,

                    //R.drawable.pic1,

                    //R.drawable.pic2,

                    //R.drawable.pic3

                   

                 };

           

            /**

             * construct method ,

             * there is only one parameter in this constructor , the context object to be stored .

             *  */

            public ImageAapter(Context c) {

                 this.context = c ;

            }

           

            /**

             * Return the total images numbers defined .

             *

             * @return this.myImageIds.length the total images number defined.

             */

            publicint getCount()

            {

                 returnthis.myImageIds.length ;

            }

           

            /**

                 * Get the data item associated with the specified position in the data set.

                 *

                 * @param position Position of the item whose data we want within the adapter's

                 * data set.

                 * @return The data at the specified position.

                 */     

            public Object getItem( int position )

            {

                 return position ;

            }

           

            /**

                 * Get the row id associated with the specified position in the list.

                 *

                 * @param position The position of the item within the adapter's data set whose row id we want.

                 * @return The id of the item at the specified position.

                 */

            publiclong getItemId ( int position )

            {

                 return position ;

            }

           

            /**

                 * Get a View that displays the data at the specified position in the data set. You can either

                 * create a View manually or inflate it from an XML layout file. When the View is inflated, the

                 * parent View (GridView, ListView...) will apply default layout parameters unless you use

                 * {@link android.view.LayoutInflater#inflate(int, android.view.ViewGroup, boolean)}

                 * to specify a root view and to prevent attachment to the root.

                 *

                 * @param position The position of the item within the adapter's data set of the item whose view

                 *        we want.

                 * @param convertView The old view to reuse, if possible. Note: You should check that this view

                 *        is non-null and of an appropriate type before using. If it is not possible to convert

                 *        this view to display the correct data, this method can create a new view.

                 *        Heterogeneous lists can specify their number of view types, so that this View is

                 *        always of the right type (see {@link #getViewTypeCount()} and

                 *        {@link #getItemViewType(int)}).

                 * @param parent The parent that this view will eventually be attached to

                 * @return A View corresponding to the data at the specified position.

                 */

            public View getView (int position , View ConvertView , ViewGroup parent )

            {

                 /*Create an ImageView object . */

                 ImageView i = new ImageView ( this.context) ;

                

                 /* Sets a drawable as the content of this ImageView. */

                 i.setImageResource(this.myImageIds[position]);

                 /*Controls how the image should be resized or moved to match the size of this ImageView.*/

                 i.setScaleType(ImageView.ScaleType.FIT_XY);

                 /*Set the layout parameters associated with this view.

                  *  These supply parameters to the parent of this view specifying how it should be arranged.

                  *  There are many subclasses of ViewGroup.LayoutParams,

                  *  and these correspond to the different subclasses of ViewGroup

                  *  that are responsible for arranging their children.*/

                 i.setLayoutParams(new Gallery.LayoutParams(200,200));

                 return i ;

            }

           

            /**

             * According to the distance between image and the middle of the screen

             * to calculate the size of the Views.  

              * @param focused

              * @param offset the distance between image and the middle of the screen

              * @return seze the size of the Views . 

             */

            publicfloat getScale ( boolean focused , int offset )

            {

                 return Math.max(0, 1.0f/(float)Math.pow(2, Math.abs(offset)));

            }

            }

     }

  • 相关阅读:
    不应滥用named let
    PAT甲级——A1106 Lowest Price in Supply Chain
    PAT甲级——A1105 Spiral Matrix【25】
    PAT甲级——A1104 Sum of Number Segments
    PAT甲级——A1103 Integer Factorization
    PAT甲级——A1102 Invert a Binary Tree
    PAT甲级——A1101 Quick Sort
    PAT甲级——A1100 Mars Numbers
    PAT甲级——A1099 Build A Binary Search Tree
    PAT甲级——A1098 Insertion or Heap Sort
  • 原文地址:https://www.cnblogs.com/xilifeng/p/2630822.html
Copyright © 2011-2022 走看看