android基础学习之六——常用效果1

来源:互联网 发布:战网 mac 更新agent 编辑:程序博客网 时间:2024/06/06 18:25

继续学习《android应用开发揭秘》这本书,这里把基础控件的学习进行了分类,下面两篇博客主要是一些常用的效果

一、    拖动效果(Gallery)

拖动效果是现在智能手机的一个不可缺少的功能,Android平台中实现拖动效果,主要是使用了Gallery控件,Gallery英文是画廊的意思,顾名思义存放这显示的图片,存放图片资源容器是继承自BaseAdapter类的派生类。当然我们也可以通过setOnItemClickListener监听其事件。

【注意】Gallery 组件一般用于显示图像列表,因此也可称相册组件 Gallery 与GridView 的区别是Gallery只能水平显示一行,而且支持水平滑动效果。也就是说,单击、选中或拖动Gallery 中的图像,Gallery中的图像列表会根据不同的情况向左或右移动,直到显示最后一个图像为止.

 

实例分析:基于上述介绍,把要显示的图片资源索引存在一个int类型数组中,并且要显示哪个图片,通过setImageResource方法来设置ImageView要显示的图片。

关键源码:

main.xml布局文件:

<?xml version="1.0" encoding="utf-8"?><Gallery xmlns:android="http://schemas.android.com/apk/res/android"   android:id="@+id/Gallery01"  android:layout_width="fill_parent"  android:layout_height="wrap_content"/>

注意,以下ImageAdapter类继承自BaseAdapter,并重写其方法

public class ImageAdapter extends BaseAdapter {private ContextmContext;// 定义Context// 定义整型数组 即图片源private Integer[]mImageIds = { R.drawable.img1, R.drawable.img2, R.drawable.img3, R.drawable.img4, R.drawable.img5, R.drawable.img6, R.drawable.img7,R.drawable.img8,};public ImageAdapter(Context c) {this.mContext = c;}//获取图片个数public int getCount() {return mImageIds.length;}//获取图片在图库的位置public Object getItem(int position) {return position;}// 获取图片IDpublic long getItemId(int position) {return position;}public View getView(int position, View convertView, ViewGroup parent{ImageView imageview = new ImageView(mContext);//给ImageView设置资源imageview.setImageResource(mImageIds[position]);//设置布局图片120x120显示imageview.setLayoutParams(new Gallery.LayoutParams(120,120));//设置显示比例类型imageview.setScaleType(ImageView.ScaleType.FIT_CENTER);return imageview;}}

而实际Activity01中只要把adapter付给Gallery对象,并设置监听事件即可“

//获得Gallery对象Gallery g = (Gallery) this.findViewById(R.id.Gallery01);//添加ImageAdapter给Gallery对象g.setAdapter(new ImageAdapter(this));//设置Gallery对象的监听事件g.setOnItemClickListener(new OnItemClickListener(){public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {Toast.makeText(Examples_04_17Activity.this,"你选择了"+(arg2+1)+" 号图片", Toast.LENGTH_SHORT).show();}});

实例效果:

二、     切换图片(ImageSwitcher)

该功能类似于很多图片浏览器,上一张下一张图片进行切换,ImageSwitcher在实现该效果时,需要设置一个ViewSwitcher.ViewFactory,注意ViewSwitcher.ViewFactory是个接口,目的:在视图转换器(ViewSwitcher)中创建视图。并通过makeView()方法来显示图片。

公共方法:public abstract View makeView ()

创建一个用于添加到视图转换器(ViewSwitcher)中的新视图

 

关键源码:

private ImageSwitcher m_Switcher; //创建ImageSwitcher对象private static int index = 0; //索引private static final int BUTTON_DOWN_ID = 0x123456; //“下一页”按钮IDprivate static final int BUTTON_UP_ID= 0x123457; //“上一页”按钮IDprivate static final int SWITCHER_ID= 0x123458; //ImageSwitcher对象的IDpublic void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        LinearLayout main_view = new LinearLayout(this);        m_Switcher = new ImageSwitcher(this); //创建ImageSwitcher对象        main_view.addView(m_Switcher); //在线性布局中添加ImageSwitcher视图        m_Switcher.setId(SWITCHER_ID); //设置ImageSwitcher对象的ID        m_Switcher.setFactory(this); //设置ImageSwitcher对象的数据源        m_Switcher.setImageResource(imagelist[index]);                setContentView(main_view); //设置显示上面创建的线性布局        Button next = new Button(this); //创建“下一张”按钮        next.setId(BUTTON_DOWN_ID);        next.setText("下一张");        next.setOnClickListener(this);        LinearLayout.LayoutParams param = new  LinearLayout.LayoutParams(100, 100);        main_view.addView(next, param);    Button pre = new Button(this); //创建“上一张”按钮pre.setId(BUTTON_UP_ID);pre.setText("上一张");pre.setOnClickListener(this);main_view.addView(pre, param);    }//事件监听、处理public void onClick(View v) {switch(v.getId()){case BUTTON_DOWN_ID: //下一页index++;if (index >= imagelist.length){index = 0;}//ImageSwitcher对象资源索引m_Switcher.setImageResource(imagelist[index]);break;    case BUTTON_UP_ID: //上一页index--;if (index < 0){index = imagelist.length - 1;}//ImageSwitcher对象资源索引m_Switcher.setImageResource(imagelist[index]);break;default:break;}}@Overridepublic View makeView() {return new ImageView(this); //将所有图片通过ImageView来显示}

【扩展点】ViewSwitcher

一、结构

public class ViewSwitcher extends ViewAnimator

        

Java.lang.Object

android.view.View

         android.view.ViewGroup

                   android.widget.FrameLayout

                            android.widget.ViewAnimator

                                     android.widget.ViewSwitcher

已知直接子类:ImageSwitcher, TextSwitcher

  二、概述

     在两个视图间转换时显示动画,有一个可以创建这些视图的工厂类。你可以用工厂来创建这些视图,也可以自己创建。一个ViewSwitcher只允许包含两个子视图,且一次仅能显示一个。

  (译者注:与ViewFlipper类相似,但该类不常用,常用其两个子类ImageSwitcher:转换图片时增加动画效果; TextSwitcher: 转换文字时增加动画效果; 其实例见apidemos中ImageSwitcher实例和TextSwitcher实例)

  三、内部类

    interface          ViewSwitcher.ViewFactory     

    在一个ViewSwitcher里创建视图

…………….

五、公共方法

public void setFactory (ViewSwitcher.ViewFactory factory)

  设置用来生成将在视图转换器中切换的两个视图的工厂。也可以调用两次 addView(android.view.View, int, android.view.ViewGroup.LayoutParams)来替代使用工厂的方法。

参数: factory   用来生成转换器内容的视图工厂

引用自:http://dev.10086.cn/cmdn/wiki/index.php?doc-view-4764.html

实例效果:

三、网格视图(GridVIew)

         网格视图的排列方式与矩阵类似,它所需要显示元素同样适用BaseAdapter来实现,适用方法,类似于Gallery控件

         关键代码:

//取得GridView对象GridView gridview = (GridView) this.findViewById(R.id.gridview);//添加元素给gridviewgridview.setAdapter(new ImageAdapter(this));gridview.setBackgroundResource(R.drawable.bg0);//事件监听gridview.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> parent, View v, int position, long id){Toast.makeText(Activity01.this, "你选择了" + (position + 1) + " 号图片", Toast.LENGTH_SHORT).show();}});

四、卷轴视图(ScrollView)

卷轴视图主要用于,一页数据显示不开后,需要滚动来显示的视图。

实例分析:ScrollView定义个线性布局,线性布局中一个TextView以及一个Button,每点击一次按钮就增加一个线性布局。

关键源码:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/ScrollView01"    android:layout_width="fill_parent"    android:layout_height="wrap_content"android:scrollbars="none" >...... </ScrollView>

 

在按钮点击事件里:

......  //改变默认焦点切换  buttonView.setOnKeyListener(mNewButtonKeyListener); //投递一个消息进行滚动   mHandler.post(mScrollToBottom);  private Runnable mScrollToBottom = new Runnable() {           @Override          public void run(){               int off = mLayout.getMeasuredHeight() - mScrollView.getHeight();               if (off > 0) {                   mScrollView.scrollTo(0, off);//设置当前视图滚动到的位置            }                                  }   <p>    };   </p><p></p><p></p><p></p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 14px; line-height: 26px;">2011-10-30周日,继续《Android应用开发揭秘》的学习,接上一篇常用效果的学习;</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 14px; line-height: 26px;"><strong>一、    进度条(ProgressBar)</strong></p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 14px; line-height: 26px;">进度条作为后台程序处理过程中,反馈给使用者的一个很好的凭证,来显示当前程序处理的怎么样,进度如何等情况。Android中一共有两种样式进度条:长形进度条与圆形进度条。而且有的程序也可以在标题栏显示进度条。</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 14px; line-height: 26px;"> </p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 14px; line-height: 26px;">在我们Eclipse开发android程序中,在编辑main.xml文件时,也提供了图形化界面的编辑,如下图:</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 14px; line-height: 26px;"><img alt="" src="http://hi.csdn.net/attachment/201110/31/0_13200459319RMt.gif" width="376" height="265" style="border: none; max-width: 100%;" /></p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 14px; line-height: 26px;">实例分析:通过一个开始按钮的点击,显示圆形与长形进度条的进度。</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 14px; line-height: 26px;"> </p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 14px; line-height: 26px;">关键源码:</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 14px; line-height: 26px;">main.xml布局文件:</p><pre class="html" name="code" style="white-space: pre-wrap; word-wrap: break-word; font-size: 14px; line-height: 26px; background-color: rgb(255, 255, 255);"><ProgressBar    android:id="@+id/ProgressBar01"style="?android:attr/progressBarStyleHorizontal"    android:layout_width="200dp"    android:layout_height="wrap_content"    android:visibility="gone"  />  <ProgressBar   android:id="@+id/ProgressBar02"        android:layout_width="wrap_content"         android:layout_height="wrap_content"        style="?android:attr/progressBarStyleLarge"        android:max="100"        android:progress="50"        android:secondaryProgress="70"android:visibility="gone"  />

【注意】该实例关键的是对ProgressBar的控制,之前例子中已经将过若是通过Handler实例的sendMessage()方法进而触发handleMessage(Message mesg)方法:

//当按钮按下时开始执行,    mButton01.setOnClickListener(new Button.OnClickListener(){      public void onClick(View v){        m_ProgressBar.setVisibility(View.VISIBLE);//设置ProgressBar为可见状态      m_ProgressBar2.setVisibility(View.VISIBLE);      m_ProgressBar.setMax(100);//设置ProgressBar的最大值      m_ProgressBar.setProgress(0); //设置ProgressBar当前值      m_ProgressBar2.setProgress(0);      //通过线程来改变ProgressBar的值      new Thread(new Runnable() {public void run(){for (int i = 0; i < 10; i++){try{intCounter = (i + 1) * 20;Thread.sleep(1000);if (i == 4){Message m = new Message();m.what = Activity01.GUI_STOP_NOTIFIER;Activity01.this.myMessageHandler.sendMessage(m);break;}else{Message m = new Message();m.what = Activity01.GUI_THREADING_NOTIFIER;Activity01.this.myMessageHandler.sendMessage(m);}}catch (Exception e){e.printStackTrace();}}}}).start();}});}  Handler myMessageHandler = new Handler(){  public void handleMessage(Message msg){  switch (msg.what){    case Activity01.GUI_STOP_NOTIFIER://ProgressBar已经是对大值  m_ProgressBar.setVisibility(View.GONE);  m_ProgressBar2.setVisibility(View.GONE);  Thread.currentThread().interrupt();  break;  case Activity01.GUI_THREADING_NOTIFIER:  if (!Thread.currentThread().isInterrupted()){  m_ProgressBar.setProgress(intCounter);// 改变ProgressBar的当前值m_ProgressBar2.setProgress(intCounter);setProgress(intCounter*100);// 设置标题栏中前景的一个进度条进度值setSecondaryProgress(intCounter*100);//设置标题栏中后面的一个进度条进度值   }  break;  }  super.handleMessage(msg); }  }; 

实例效果:

   

二、    拖动条(SeekBar)

拖动条主要用于程序中,对一些属性的调节,如:音效大小。在Android中实现还是比较容易,SeekBar控件,而且只需要监听该控件的三个事件:

数值改变(onProgressChanged);

开始拖动(onStartTrackingTouch);

停止拖动(onStopTrackingTouch);

 

其控件配置也比较简单:

<SeekBar android:id="@+id/seek"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:max="100"        android:progress="50"android:secondaryProgress="75" />

效果图:

三、状态栏提示(Notification,NotificationManager)

当手机有未接电话或者短信息时,手机顶部状态栏就会显示一个小图标,用来显示用户有没有处理的快讯。NotificationManager用来管理状态栏的信息,而Notification用来处理这些快讯信息。

NotificationManager对象的获取通过gerSystenService方法,Notification对象可以设置其内容,图标,标题等属性。然后通过notify方法来执行一个Notification快讯。

实例分析:当用户点击一个按钮,就发出一个Notification快讯,这是手机顶部状态栏显示相应提示信息。展开状态栏,点击快讯信息,跳转到处理界面。

关键源码:

//初始化NotificationManager对象        m_NotificationManager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);                //获取四个按钮对象        mButton1 = (Button) this.findViewById(R.id.Button01);        mButton2 = (Button) this.findViewById(R.id.Button02);        mButton3 = (Button) this.findViewById(R.id.Button03);        mButton4 = (Button) this.findViewById(R.id.Button04);                //点击通知时转移内容        m_Intent = new Intent(Activity01.this, Activity02.class);        //主要是设置点击通知时显示内容的类        m_PendingIntent = PendingIntent.getActivity(Activity01.this, 0, m_Intent, 0);        //构造Notification对象        m_Notification = new Notification();        mButton1.setOnClickListener(new Button.OnClickListener() {public void onClick(View v) {//设置通知在状态栏显示的图标m_Notification.icon = R.drawable.img1;//当我们点击通知时显示的内容m_Notification.tickerText="Button1通知内容..........";//通知时发出默认的声音m_Notification.defaults = Notification.DEFAULT_SOUND;//设置通知显示的参数m_Notification.setLatestEventInfo(Activity01.this, "Button1", "Button1通知", m_PendingIntent);//可以理解为执行这个通知m_NotificationManager.notify(0,m_Notification);}});<span style="font-family: Arial;"></span>

其中,notify()方法:

public void notify (int id, Notification notification)

Post a notification to be shown in the status bar. If a notification with the same id has already been posted by your application and has not yet been canceled, it will be replaced by the updated information.

Parameters

id

An identifier for this notification unique within your application.

notification

A Notification object describing what to show the user. Must not be null.

 

 实例效果图:

  

四、对话框中的进度条(ProgressDialog)

对话框中的进度条,可以设置图标,内容等属性。

 

实例分析:通过点击两个按钮,显示对话框中得两种进度条。

关键源码:

//设置mButton01的事件的监听mButton01.setOnClickListener(new Button.OnClickListener() {public void onClick(View v) {m_pDialog = new ProgressDialog(Examples_04_24Activity.this);//创建ProgressDialog对象//设置进度条风格,风格为圆形,旋转的m_pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);m_pDialog.setTitle("提示");//设置ProgressDialog标题m_pDialog.setMessage("这是一个圆形进度条对话框");//设置ProgressDialog提示信息m_pDialog.setIcon(R.drawable.img1);//设置ProgressDialog标题图标m_pDialog.setIndeterminate(false);//设置ProgressDialog的进度条是否不明确m_pDialog.setCancelable(true);//设置PrgoressDialog是否可以按退回键取消m_pDialog.setButton("确定", new DialogInterface.OnClickListener() {//设置ProgressDialog的一个Buttonpublic void onClick(DialogInterface dialog, int which){dialog.cancel();}});//让ProgressDialog显示m_pDialog.show();}});        //设置mButton02的事件监听mButton02.setOnClickListener(new Button.OnClickListener() {public void onClick(View v) {m_count = 0;m_pDialog = new ProgressDialog(Examples_04_24Activity.this);//创建ProgressDialog对象m_pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//设置进度条风格,风格为长形......m_pDialog.show();// 让ProgressDialog显示new Thread() {public void run(){try{while (m_count <= 100){ // 由线程来控制进度。m_pDialog.setProgress(m_count++);Thread.sleep(100); }m_pDialog.cancel();}catch (InterruptedException e){m_pDialog.cancel();}}}.start();}});

实例效果:

  

今天就实例效果学习结束了,关于android的学习,下面把布局学习结束后,基础的学习就结束了。加油!

 


                                             
0 0