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)));

        }

        }

 }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.a2, PID: 18827 android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.a2/com.example.a2.jisuanqi}; have you declared this activity in your AndroidManifest.xml? at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:2049) at android.app.Instrumentation.execStartActivity(Instrumentation.java:1709) at android.app.Activity.startActivityForResult(Activity.java:5192) at androidx.activity.ComponentActivity.startActivityForResult(ComponentActivity.java:597) at android.app.Activity.startActivityForResult(Activity.java:5150) at androidx.activity.ComponentActivity.startActivityForResult(ComponentActivity.java:583) at android.app.Activity.startActivity(Activity.java:5521) at android.app.Activity.startActivity(Activity.java:5489) at com.example.a2.MainActivity$1.onClick(MainActivity.java:37) at android.view.View.performClick(View.java:7125) at android.view.View.performClickInternal(View.java:7102) at android.view.View.access$3500(View.java:801) at android.view.View$PerformClick.run(View.java:27336) at android.os.Handler.handleCallback(Handler.java:883) at android.os.Handler.dispatchMessage(Handler.java:100) at android.os.Looper.loop(Looper.java:214) at android.app.ActivityThread.main(ActivityThread.java:7356) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
06-07

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值