<ViewSwitcher android:id="@+id/viewSwitcher1" android:layout_width="wrap_content" android:layout_height="wrap_content" />
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:id="@+id/LinearLayout02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="75dp" > <ViewSwitcher android:id="@+id/viewSwitcher1" android:layout_width="wrap_content" android:layout_height="wrap_content" > <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/pic_a" /> <ImageView android:id="@+id/imageView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/pic_b" /> </ViewSwitcher> </LinearLayout> </RelativeLayout>
package com.myapp; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.MotionEvent; import android.view.View; import android.view.animation.AlphaAnimation; import android.view.animation.Animation; import android.widget.Button; import android.widget.ViewSwitcher; public class MainActivity extends Activity { Button next; Button previous; ViewSwitcher vs; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); vs = (ViewSwitcher) findViewById(R.id.viewSwitcher1); Animation inAnim = new AlphaAnimation(0, 1); inAnim.setDuration(2000); Animation outAnim = new AlphaAnimation(1, 0); outAnim.setDuration(2000); vs.setInAnimation(inAnim); vs.setOutAnimation(outAnim); vs.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { vs.showNext(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }
vs.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { vs.showNext(); } });
<ViewSwitcher xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/viewSwitcher1" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/tableLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="50dp" > <ProgressBar android:id="@+id/progressBar1" style="?android:attr/progressBarStyleLarge" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </TableLayout> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/tableLayout2" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="50dp" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="Welcome to My App" android:textAppearance="?android:attr/textAppearanceMedium" /> <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/pic_a" android:layout_marginTop="10dp" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="CopyRight 2012 www.ThaiCreate.Com" android:textAppearance="?android:attr/textAppearanceSmall" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Reload again" /> </TableLayout> </ViewSwitcher>
package com.myapp; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.annotation.SuppressLint; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.ViewSwitcher; public class MainActivity extends Activity { ViewSwitcher Vs; private static final int REFRESH_SCREEN = 1; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // viewSwitcher1 Vs = (ViewSwitcher) findViewById(R.id.viewSwitcher1); startScan(); // Reload again Button btn1 = (Button) findViewById(R.id.button1); btn1.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Vs.showPrevious(); startScan(); } }); } public void startScan() { new Thread() { public void run() { try{ Thread.sleep(5000); hRefresh.sendEmptyMessage(REFRESH_SCREEN); }catch(Exception e){ } } }.start(); } @SuppressLint("HandlerLeak") Handler hRefresh = new Handler(){ public void handleMessage(Message msg) { switch(msg.what){ case REFRESH_SCREEN: Vs.showNext(); break; default: break; } } }; @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท