ZNDS智能電視網 推薦當貝市場

TV應用下載 / 資源分享區(qū)

軟件下載 | 游戲 | 討論 | 電視計算器

綜合交流 / 評測 / 活動區(qū)

交流區(qū) | 測硬件 | 網站活動 | Z幣中心

新手入門 / 進階 / 社區(qū)互助

新手 | 你問我答 | 免費刷機救磚 | ROM固件

查看: 14401|回復: 0
上一主題 下一主題
[教程]

第四十五講:用戶界面 View(十二)

[復制鏈接]
跳轉到指定樓層
樓主
發(fā)表于 2013-8-28 16:26 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
>
本講內容:ImageSwitcher 圖片切換器 和 TextSwitcher 文本切換器
源代碼下載:
  
3、在main.xml中添加一個ImageSwitcher組件:
   
  1. <?xml version="1.0" encoding="utf-8"?>   
    <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">   
            <imageswitcher android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageSwitcher1">   
            </imageswitcher>   
    </linearlayout>
復制代碼
4、在MainActivity.java中的代碼如下:
   
  1. package basic.android.lesson45;   
       
    import android.app.Activity;   
    import android.os.Bundle;   
    import android.view.View;   
    import android.view.Window;   
    import android.view.WindowManager;   
    import android.view.animation.AnimationUtils;   
    import android.widget.ImageSwitcher;   
    import android.widget.ImageView;   
    import android.widget.ViewSwitcher.ViewFactory;   
       
    public class MainActivity extends Activity {   
       
            //當前顯示的圖片索引   
            private int index;   
       
            //圖片數(shù)組   
            private int[] images = { R.drawable.image1, R.drawable.image2, R.drawable.image3, R.drawable.image4,   
                            R.drawable.image5 };   
       
            /** Called when the activity is first created. */   
            @Override   
            public void onCreate(Bundle savedInstanceState) {   
       
                    super.onCreate(savedInstanceState);   
       
                    //全屏設置   
                    requestWindowFeature(Window.FEATURE_NO_TITLE);   
                    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);   
       
                    setContentView(R.layout.main);   
       
                    //得到ImageSwitcher對象   
                    final ImageSwitcher is = (ImageSwitcher) findViewById(R.id.imageSwitcher1);   
       
                    //實現(xiàn)并設置工廠內部接口的makeView方法,用來顯示視圖。   
                    is.setFactory(new ViewFactory() {   
                            @Override   
                            public View makeView() {   
                                    return new ImageView(MainActivity.this);   
                            }   
                    });   
       
                    //設置圖片來源   
                    is.setImageResource(images[index]);   
       
                    //設置點擊監(jiān)聽器   
                    is.setOnClickListener(new View.OnClickListener() {   
       
                            @Override   
                            public void onClick(View v) {   
                                    //點擊會切換圖片   
                                    index++;   
                                    if (index >= images.length) {   
                                            index = 0;   
                                    }   
                                    is.setImageResource(images[index]);   
                            }   
                    });   
       
                    //設置切入動畫   
                    is.setInAnimation(AnimationUtils.loadAnimation(getApplicationContext(), android.R.anim.slide_in_left));   
                    //設置切出動畫   
                    is.setOutAnimation(AnimationUtils.loadAnimation(getApplicationContext(), android.R.anim.slide_out_right));   
       
            }   
    }
復制代碼
   5、編譯并運行程序,查看結果:抱歉我抓不到切換圖片瞬間的截圖。      
二、TextSwitcher 文本切換器文本的切換動畫也是有一個叫TextSwitcher的類可以做到,它的使用方法和ImageSwitcher類似。至于為什么用法如此相似,還是看下面兩張繼承關系圖吧:   
       
下面直接上例子:1、新建一個項目:Lesson45_TextSwitcher2、在main.xml中添加一個TextSwitcher組件:   
  1. <?xml version="1.0" encoding="utf-8"?>   
    <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">   
            <textswitcher android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/textSwitcher1">   
            </textswitcher>   
    </linearlayout>
復制代碼
  3、在MainActivity.java中的代碼如下:   
  1. package basic.android.lesson45;   
       
    import android.app.Activity;   
    import android.graphics.Color;   
    import android.os.Bundle;   
    import android.view.View;   
    import android.view.animation.AnimationUtils;   
    import android.widget.TextSwitcher;   
    import android.widget.TextView;   
    import android.widget.ViewSwitcher.ViewFactory;   
       
    public class MainActivity extends Activity {   
       
            // 索引   
            private int index;   
            // 文本數(shù)組   
            private String[] poemArray = { "去年今日栽", "臨去見花開", "好住守空院", "夜間人不來" };   
       
            /** Called when the activity is first created. */   
            @Override   
            public void onCreate(Bundle savedInstanceState) {   
                    super.onCreate(savedInstanceState);   
                    setContentView(R.layout.main);   
       
                    //定義文字切換器   
                    final TextSwitcher ts = (TextSwitcher) findViewById(R.id.textSwitcher1);   
       
                    //定義視圖顯示工廠,并設置   
                    ts.setFactory(new ViewFactory() {   
       
                            @Override   
                            public View makeView() {   
                                    TextView tv =new TextView(MainActivity.this);   
                                    tv.setTextSize(32);   
                                    tv.setTextColor(Color.GREEN);   
                                    return tv;   
                            }   
                    });   
       
                    // 設置圖片來源   
                    ts.setText(poemArray[index]);   
       
                    // 設置點擊監(jiān)聽器   
                    ts.setOnClickListener(new View.OnClickListener() {   
       
                            @Override   
                            public void onClick(View v) {   
                                    // 點擊會切換圖片   
                                    index++;   
                                    if (index >= poemArray.length) {   
                                            index = 0;   
                                    }   
                                    ts.setText(poemArray[index]);   
                            }   
                    });   
       
                    // 設置切入動畫   
                    ts.setInAnimation(AnimationUtils.loadAnimation(getApplicationContext(), android.R.anim.slide_in_left));   
                    // 設置切出動畫   
                    ts.setOutAnimation(AnimationUtils.loadAnimation(getApplicationContext(), android.R.anim.slide_out_right));   
       
            }   
    }
復制代碼
  
   4、編譯并運行程序,查看結果:   
      
抱歉還是沒法截取到切換時的場景#_#好吧,本講就到這里,各位下次再見。</div

上一篇:百度地圖開發(fā)之環(huán)境搭建
下一篇:第三十二講:Android中的主題和風格學習指南
您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

Archiver|新帖|標簽|軟件|Sitemap|ZNDS智能電視網 ( 蘇ICP備2023012627號 )

網絡信息服務信用承諾書 | 增值電信業(yè)務經營許可證:蘇B2-20221768 丨 蘇公網安備 32011402011373號

GMT+8, 2024-10-20 20:55 , Processed in 0.056825 second(s), 15 queries , Redis On.

Powered by Discuz!

監(jiān)督舉報:report#znds.com (請將#替換為@)

© 2007-2024 ZNDS.Com

快速回復 返回頂部 返回列表