ImageSwitcher 練習(android)

來源:互聯網
上載者:User

ImageSwitch 需要一個 ViewFactory 介面方法,返回一個 ImageView 對像,同時指定 setFactory 方法

其中試了試用 ImageView 實現 ImageSwitcher ,並沒有發現有什麼不同,還有是其它什麼不同呢?

 

代碼如下:

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="@string/hello" />
     <ImageView 
         android:id="@+id/iv"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         />
     <Button 
         android:id="@+id/btn"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="切換圖片"
         />
     
     <ImageSwitcher 
         android:id="@+id/imageswitch"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"  
         ></ImageSwitcher>
     <Button 
         android:id="@+id/btn2"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="切換圖片2"
         />
</LinearLayout>

 

java代碼

package zziss.android.imageswitchtest;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;

public class ImageSwitchTestActivity extends Activity implements ViewFactory {
    /** Called when the activity is first created. */
    private ImageView iv;
    private ImageSwitcher imw;
    private Button    btn;
    private Button    btn2;
    private int       i;
    private int       index;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        iv = (ImageView)this.findViewById(R.id.iv);
        btn = (Button)this.findViewById(R.id.btn);
        btn2 = (Button)this.findViewById(R.id.btn2);
        
        imw  = (ImageSwitcher)this.findViewById(R.id.imageswitch);
        imw.setFactory(this);
        i = 0;
        index =0;
        btn.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                /*if (i==0)
                {
                    iv.setImageResource(R.drawable.png1410);
                    i=1;
                }
                else
                {
                    iv.setImageResource(R.drawable.png1411);
                    i=0;
                }*/
                if (index>=3)
                    index = 0;
                iv.setImageResource(imagelist[index]);
                index++;
            }
        });
        
        btn2.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (index>=3)
                    index = 0;
                imw.setImageResource(imagelist[index]);
                index++;
            }
        });
        
    }
    
    private static int[] imagelist =
        {
            R.drawable.png1410,
            R.drawable.png1411,
            R.drawable.png1412
        };
    @Override
    public View makeView() {
        // TODO Auto-generated method stub
        return new ImageView(this);
    }
}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.