安卓第九天——UI组件

来源:互联网 发布:工业三维动画软件 编辑:程序博客网 时间:2024/05/16 17:16
 

安卓第九天

                                                                                                                                                  UI组件3

ProgressBar

<?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="500"

        android:secondaryProgress="300"

       android:id="@+id/progressbar2"

        />

</LinearLayout>

 

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 ProgressBarDemo extends Activity{

   

    ProgressBar progressbar = null;

    static int i = 0;

    int progressbarMax = 0;

 

    Handler handler = new Handler();

   

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.progressbar_layout);

       

        findViews();

    }

 

    private void findViews() {

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

       progressbar.setMax(1000);

       progressbarMax = progressbar.getMax();

      

       new Thread(new Runnable(){

          

           public void run(){

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

 

    }

   

    public int doWork(){

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

       return ++i;

    }

   

/*  //不能用!!!!

    public void run(){

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

      

       while(i++ < progressbarMax){

           progressbar.setProgress(i);

           try {

              Thread.sleep(50);

           } catch (InterruptedException e) {

              // TODO Auto-generated catch block

              e.printStackTrace();

           }

       }

    }

    */

}

 

SeekBar

<?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>

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 Activityimplements 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() {

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

       seekbar.setOnSeekBarChangeListener(this);

    }

 

    @Override

    public void onProgressChanged(SeekBar seekBar,int progress,

           boolean fromUser) {

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

      

    }

 

    @Override

    public void onStartTrackingTouch(SeekBar seekBar) {

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

      

    }

 

    @Override

    public void onStopTrackingTouch(SeekBar seekBar) {

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

      

    }

 

}

 

ImageView

<?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:background="#cccccc"

        android:src="@drawable/pig" />

 

    <ImageView

        android:id="@+id/img2"

        android:layout_width="100dp"

        android:layout_height="100dp"

        android:background="#cccccc"

        android:scaleType="fitStart"

        android:layout_marginTop="20dp"

        />

 

</LinearLayout>

 

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 Activityimplements 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() {

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

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

      

       imageView1.setOnTouchListener(this);

 

    }

 

    public boolean onTouch(View v, MotionEvent event) {

       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

<?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/pig"

            android:scaleType="fitCenter"

            android:layout_marginTop="20dp"

            />

       

       

    </LinearLayout>

 

</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) {

       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("ImageView Demo")

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

 

       findViews();

    }

 

    private void findViews() {

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

       btn.setOnClickListener(new View.OnClickListener() {

          

           public void onClick(View v) {

             

//            tabHost.setCurrentTab(1);

              tabHost.setCurrentTabByTag("tab2");

           }

       });

      

    }

 

}