UI组件3

 ProgressBar,SeekBar,Touch取图,TabHost

 

ProgressBar进度条的效果实现

package cn.class3g.activity;

 

import android.app.Activity;

import android.os.Bundle;

import android.os.Handler;

import android.util.Log;

import android.widget.ProgressBar;

 

public class progressDemo extends Activity implements Runnable{

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

         ProgressBar progressbar = null;

         int i=0;

         int progressbarMax = 0;

         Handler handler = new Handler();

        

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.progressbar_layout);

        findViews();

    }

 

         private void findViews() {

                   // TODO Auto-generated method stub

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

                   progressbar.setMax(1000);

                   progressbarMax = progressbar.getMax();

                  

                   new Thread(new Runnable(){

                            @Override

                            public void run() {

                                     // TODO Auto-generated method stub

                                     while(i<progressbarMax){

                                              

                                               i=doWork();

                                               handler.post(new Runnable(){

                                                        public void run(){

                                                                 progressbar.setProgress(i);

                                                        }

                                               });

                                               try {

                                                        Thread.sleep(50);

                                               } catch (InterruptedException e) {

                                                        // TODO Auto-generated catch block

                                                        e.printStackTrace();

                                               }

                                     }

                            }

                   }).start();

                   //new Thread(this).start();

                  

         }

         public int doWork(){

                   Log.d("TAG",String.valueOf(i));

                   return ++i;//注意:i++则错误

         }

 

         @Override

         public void run() {

                   // TODO Auto-generated method stub

                  

         }

/*//不合适,一般不用,但能调适通

         public void run() {

                   // TODO Auto-generated method stub

                   Log.d("TAG","thread starting.....");

                  

                   while(i++<progressbar.getMax()){

                            progressbar.setProgress(i);

                            try {

                                     Thread.sleep(50);

                            } catch (InterruptedException e) {

                                     // TODO Auto-generated catch block

                                     e.printStackTrace();

                            }

                   }

                  

                  

         }*/

}

SeekBar滑杆的效果实现

package cn.class3g.activity;

 

import android.app.Activity;

import android.os.Bundle;

import android.util.Log;

import android.widget.SeekBar;

import android.widget.SeekBar.OnSeekBarChangeListener;

 

public class SeekBarDemo extends Activity implements OnSeekBarChangeListener{

 

         SeekBar seekbar = null;

         protected void onCreate(Bundle savedInstanceState) {

                   // TODO Auto-generated method stub

                   super.onCreate(savedInstanceState);

                   this.setContentView(R.layout.seekbar_layout);

                   findViews();

                  

         }

         private void findViews() {

                   // TODO Auto-generated method stub

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

                   seekbar.setOnSeekBarChangeListener(this);

         }

         @Override

         public void onProgressChanged(SeekBar seekBar, int progress,

                            boolean fromUser) {

                   // TODO Auto-generated method stub

                   Log.d("TAG","changed:" + String.valueOf(seekBar.getProgress()));

                  

         }

         @Override

         public void onStartTrackingTouch(SeekBar seekBar) {

                   // TODO Auto-generated method stub

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

                  

         }

         @Override

         public void onStopTrackingTouch(SeekBar seekBar) {

                   // TODO Auto-generated method stub

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

                  

         }

 

}

用Touch取图的效果实现

package cn.class3g.activity;

 

import android.app.Activity;

import android.graphics.Bitmap;

import android.graphics.drawable.BitmapDrawable;

import android.os.Bundle;

import android.view.MotionEvent;

import android.view.View;

import android.view.View.OnTouchListener;

import android.widget.ImageView;

 

public class ImageViewDemo extends Activity implements OnTouchListener{

 

         ImageView imageView1,imageView2;

         protected void onCreate(Bundle savedInstanceState) {

                   // TODO Auto-generated method stub

                   super.onCreate(savedInstanceState);

                   this.setContentView(R.layout.imageview_layout);

                   findViews();

                  

         }

         private void findViews() {

                   // TODO Auto-generated method stub

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

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

        

         imageView1.setOnTouchListener(this);

         }

 

@Override

public boolean onTouch(View v, MotionEvent event) {

         // TODO Auto-generated method stub

         float scale = 412/320;

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

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

         //尝试考虑

         int width=(int) (100*scale);

         int height = (int)(100*scale);

        

         BitmapDrawable bitmapDrawable  = (BitmapDrawable) imageView1.getDrawable();

         imageView2.setImageBitmap(Bitmap.createBitmap(bitmapDrawable.getBitmap(),

                   x,y,width,height       ));

        

        

         return false;

}

 

 

}

TabHost标签切换的实现

package cn.class3g.activity;

 

 

import android.app.TabActivity;

import android.content.Intent;

import android.os.Bundle;

import android.view.LayoutInflater;

import android.view.View;

import android.widget.Button;

import android.widget.TabHost;

 

public class TabHostDemo extends TabActivity {

 

         TabHost tabHost= null;

        

         protected void onCreate(Bundle savedInstanceState) {

                   // TODO Auto-generated method stub

                   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("seekBar demo").

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

                                              

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

                            .setIndicator("ImageViewDemo").

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

                                              

         findView();

         }

 

         private void findView() {

                   // TODO Auto-generated method stub

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

                   btn.setOnClickListener(new View.OnClickListener() {

                           

                            @Override

                            public void onClick(View v) {

                                     // TODO Auto-generated method stub

                                     tabHost.setCurrentTab(1);

                                     //tabHost.setCurrentTabByTag("tab2");

                                    

                            }

                   });

         }

 

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值