安卓個人學習——ImageView,SeekBar,TableHost,ProgressBar的使用

來源:互聯網
上載者:User

先讓大家看一個

、、

怎麼樣,效果炫嗎?

讓我們來分析一下,這種效果怎麼樣才能實現呢?

首先我們注意一下,上方几個橫著切換的功能是TabHost

TabHost

提供選項卡(Tab頁)的視窗視圖容器。此對象包含兩個子物件:一組是使用者可以選擇指定Tab頁的標籤;另一組是FrameLayout用來顯示該Tab頁的內容。通常控制使用這個容器物件,而不是設定在子項目本身的值。(譯者註:即使使用的是單個元素,也最好把它放到容器物件ViewGroup裡)

內部類

interfaceTabHost.OnTabChangeListener    

介面定義了當選項卡更改時被調用的回呼函數

 

interface TabHost.TabContentFactory  

當某一選項卡被選中時產生選項卡的內容

 

class TabHost.TabSpec     

單獨的選項卡,每個選項卡都有一個選項卡指示符,內容和tag標籤,以便於記錄.

 

公用方法

public void addTab(TabHost.TabSpec tabSpec)

新增一個選項卡

參數

tabSpec   指定怎樣建立指示符和內容.

<?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:orientation="vertical"android:layout_width="fill_parent" android:layout_height="fill_parent"><Button android:id="@+id/button" android:layout_width="fill_parent"android:layout_height="wrap_content" android:text="切換到第3個標籤" /><ImageView  android:layout_width="fill_parent"android:layout_height="wrap_content" android:src="@drawable/background" android:layout_marginTop="30dp"/></LinearLayout></TabHost>

import android.app.TabActivity;import android.content.Intent;import android.os.Bundle;import android.view.LayoutInflater;import android.widget.TabHost;public class TableHostTest extends TabActivity {private TabHost tabHost = null;@Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);tabHost = this.getTabHost();LayoutInflater inflater = LayoutInflater.from(this);/*注意true的作用*/inflater.inflate(R.layout.tablehost, tabHost.getTabContentView(),true);tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("介面1").setContent(R.id.tab1));tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("介面2").setContent(new Intent(this,SeekBarDemo.class)));tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("介面3").setContent(new Intent(this,UITest4Activity.class)));}}

進度條顯示:

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

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 blocke.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 blocke.printStackTrace();}}}*/}

下面用ImageView實現圖片預覽功能

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 stubsuper.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;}}

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

調節音量等的控制條:

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

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 stubsuper.onCreate(savedInstanceState);this.setContentView(R.layout.seekbar_layout);findViews();}private void findViews() {seekbar = (SeekBar) this.findViewById(R.id.seekbar);seekbar.setOnSeekBarChangeListener(this);}@Overridepublic void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {Log.d("TAG", "changed: "+  String.valueOf(seekBar.getProgress()));}@Overridepublic void onStartTrackingTouch(SeekBar seekBar) {Log.d("TAG", "start: "+  String.valueOf(seekBar.getProgress()));}@Overridepublic void onStopTrackingTouch(SeekBar seekBar) {Log.d("TAG", "stop: "+  String.valueOf(seekBar.getProgress()));}}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.