android组件—ProgressBar、seekbar ImageView、TabHost

在某些操作的进度中的可视指示器,为用户呈现操作的进度,还它有一个次要的进度条,用来显示中间进度,如在流媒体播放的缓冲区的进度。一个进度条也可不确定其进度。在不确定模式下,进度条显示循环动画。这种模式常用于应用程序使用任务的长度是未知的。getMax():返回这个进度条的范围的上限

getProgress():返回进度

getSecondaryProgress():返回次要进度

incrementProgressBy(int diff):指定增加的进度

isIndeterminate():指示进度条是否在不确定模式下

setIndeterminate(boolean indeterminate):设置不确定模式下

setVisibility(intv):设置该进度条是否可视

onSizeChanged(int w, int h, int oldw, int oldh):当进度值改变时引发此事件

“环形进度条”和“水平进度条”。如下图所示:


packagecn.csdn.class3g;

 

importandroid.app.Activity;

importandroid.os.Bundle;

importandroid.os.Handler;

importandroid.widget.ProgressBar;

 

public classProgressBarDemo extends Activity{

       ProgressBar progressbar=null;

       int i=0;

       int proMax;

       Handler handler=new Handler();

    public void onCreate(BundlesavedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.progressbar);

        findViews();

    }

       private void findViews() {

              progressbar=(ProgressBar)this.findViewById(R.id.progressbar2);

              proMax=progressbar.getMax();

          new Thread(new Runnable(){

                     public void run() {

                            while(i++<proMax){

                                   handler.post(newRunnable(){

                                          publicvoid run() {

                                                 progressbar.setProgress(i);

                                          }

                                         

                                   });

                            }

                     }

               

           }).start();

       }

      

}

Xml文件

<?xml version="1.0"encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical" >

 

    <TextView

       android:layout_width="fill_parent"

       android:layout_height="wrap_content"

       android:text="进度条演示" />

    <ProgressBar

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:max="1000"

       android:progress="100"

       android:id="@+id/progressbar1"

       />

    <ProgressBar

       style="@android:style/Widget.ProgressBar.Horizontal"

       android:layout_marginTop="30dp"

       android:layout_width="fill_parent"

       android:layout_height="wrap_content"

       android:max="1000"

        android:progress="100"

        android:secondaryProgress="300"

        android:id="@+id/progressbar2"

       />

</LinearLayout>

SeekBar可以作为音乐播放器的进度指示和调整工具,音量调整工具等,SeekBar是ProgressBar的一个子类,在其基础上增加了一个可以滑动的滑片(就是可以拖动的图标)可以触摸滑片并向左向右移动,SeekBar可以附加一个SeekBar.OnSeekBarChangeListener以获得用户的操作通知


<?xml version="1.0"encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical" >

    <SeekBar

       android:layout_width="fill_parent"

       android:layout_height="wrap_content"

       android:max="1000"

        android:id="@+id/seekbar"

        />

</LinearLayout>

JAVA代码

packagecn.csdn.class3g;

 

importandroid.app.Activity;

importandroid.os.Bundle;

importandroid.util.Log;

importandroid.widget.SeekBar;

import android.widget.SeekBar.OnSeekBarChangeListener;

public classSeekBarDemo extends Activity implements OnSeekBarChangeListener{

       SeekBar seekbar=null;

       protected void onCreate(BundlesavedInstanceState) {

              super.onCreate(savedInstanceState);

              setContentView(R.layout.seekbar);

              findViews();

       }

       private void findViews() {

              seekbar=(SeekBar)this.findViewById(R.id.seekbar);

              seekbar.setOnSeekBarChangeListener(this);

       }

       public void onProgressChanged(SeekBarseekBar, int progress,

                     boolean fromUser) {

             

       }

       public void onStartTrackingTouch(SeekBarseekBar) {

              Log.i("TAG","start"+String.valueOf(seekBar.getProgress()));

       }

       public void onStopTrackingTouch(SeekBarseekBar) {

              Log.i("TAG","stop"+String.valueOf(seekBar.getProgress()));

       }

      

}

ImageView

mageView类可以加载各种来源的图片(如资源或图片库),需要计算图像的尺寸,比便它可以在其他布局中使用,并提供例如缩放和着色(渲染)各种显示选项。

<?xml version="1.0"encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical" >

 

    <ImageView

       android:id="@+id/img1"

       android:layout_width="fill_parent"

       android:layout_height="300dp"

       android:src="@drawable/aaa"

       android:background="#CCCCCC"

       android:scaleType="center"

       />

        <ImageView

       android:id="@+id/img2"

       android:layout_width="100dp"

       android:layout_height="100dp"

       android:src="@drawable/ic_launcher"

       android:background="#CCCCCC"

       android:scaleType="center"

       />

 

</LinearLayout>

XML中的属性

 

android:adjustViewBounds

设置该属性为真可以在 ImageView 调整边界时保持图片的纵横比例。(译者注:需要与maxWidth、MaxHeight一起使用,否则单独使用没有效果。)

android:maxHeight

为视图提供最大高度的可选参数。(译者注:单独使用无效,需要与setAdjustViewBounds一起使用。如果想设置图片固定大小,又想保持图片宽高比,需要如下设置:

1) 设置setAdjustViewBounds为true;

2) 设置maxWidth、MaxHeight;

3) 设置设置layout_width和layout_height为wrap_content。)

android:src设置可绘制对象作为 ImageView 显示的内容

android:scaleType值的意义:

CENTER /center  按图片的原来size居中显示,当图片长/宽超过View的长/宽,则截取图片的居中部分显示

CENTER_CROP / centerCrop  按比例扩大图片的size居中显示,使得图片长(宽)等于或大于View的长(宽)

CENTER_INSIDE / centerInside  将图片的内容完整居中显示,通过按比例缩小或原来的size使得图片长/宽等于或小于View的长/宽

FIT_CENTER / fitCenter  把图片按比例扩大/缩小到View的宽度,居中显示

FIT_END / fitEnd   把图片按比例扩大/缩小到View的宽度,显示在View的下部分位置

FIT_START / fitStart  把图片按比例扩大/缩小到View的宽度,显示在View的上部分位置

FIT_XY / fitXY  把图片不按比例扩大/缩小到View的大小显示

MATRIX / matrix 用矩阵来绘制

 

public class ImageDemo extends Activity implementsOnTouchListener{

    ImageViewimg1,img2;

    protected voidonCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

       setContentView(R.layout.image_layout);

       findViews();

    }

    private voidfindViews() {

       img1=(ImageView)this.findViewById(R.id.img1);

       img2=(ImageView)this.findViewById(R.id.img2);

      

       img1.setOnTouchListener(this);

    }

    @Override

    public booleanonTouch(View v, MotionEvent event) {

       float scale=413/320;

       //图片的缩放比例

       intx=(int)(event.getX()*scale);

       inty=(int)(event.getY()*scale);

       intwidth=(int)(100*scale);

       intheight=(int)(100*scale);

      

       BitmapDrawablebitmapDrawable=(BitmapDrawable) img1.getDrawable();

       img2.setImageBitmap(Bitmap.createBitmap(bitmapDrawable.getBitmap(),x,y,width,height));

       return false;

    }

 

}

TabHost:提供选项卡的窗口视图容器,此对象包含两个子对象,一组是可以选择指定的Tab页的标签,另一组是FrameLayout用来显示该tab页的内容,即使使用的是单个元素,也最好把她放在容器对象的ViewGroup里。示例如下图:


publicclass TabHostDemo extends TabActivity {

 

   TabHost tabHost = null;

 

   protected void onCreate(BundlesavedInstanceState) {

      super.onCreate(savedInstanceState);

 

      tabHost = this.getTabHost();

 

      LayoutInflater inflater =LayoutInflater.from(this);

 

      inflater.inflate(R.layout.tabhost_layout,tabHost.getTabContentView(),

            true);

 

      tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("切换标签")

            .setContent(R.id.tab1));

 

      tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("SeekBardemo")

            .setContent(new Intent(this,SeekBarDemo.class)));

 

      tabHost.addTab(tabHost.newTabSpec("tab3")

            .setIndicator("ImageViewDemo")

            .setContent(new Intent(this,ImageDemo.class)));

 

      findViews();

   }

 

   private void findViews() {

      Button btn = (Button)this.findViewById(R.id.button);

      btn.setOnClickListener(newView.OnClickListener() {

        

         public void onClick(View v) {

           

//          tabHost.setCurrentTab(1);

            tabHost.setCurrentTabByTag("tab2");

         }

      });

     

   }

 

}

 

 

<?xml version="1.0"encoding="utf-8"?>

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"   >

 

    <LinearLayout

       android:id="@+id/tab1"       

       android:layout_width="match_parent"

       android:layout_height="match_parent"

        android:orientation="vertical">

       

        <Button

           android:id="@+id/button"

           android:layout_width="match_parent"

           android:layout_height="wrap_content"

          android:text="切换至tab2"

           />

        <ImageView

           android:layout_width="wrap_content"

           android:layout_height="wrap_content"

           android:src="@drawable/aaa"

           android:scaleType="fitCenter"

           android:layout_marginTop="20dp"

           />

    </LinearLayout>

</TabHost>

让 自己的类继承TabActivity,然后通过调用getTabHost()方法得到tabhost对象,然后把自己写好的数据展示的布局文件加载到 tabhost中,就可以实现了。最后是通过调用addTab()方法添加标签的相关属性(如:标签名称,标签图片,标签内容布局)。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值