MainActivity
package com.ceshidemo;
import java.util.Timer;
import java.util.TimerTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.text.format.Time;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.Gallery.LayoutParams;
import android.widget.ImageView;
@SuppressWarnings("deprecation")
public class MainActivity extends Activity implements OnClickListener {
Button bt1, bt2, bt3, bt4;
Gallery gallery;
int[] picture = { R.drawable.gallery1, R.drawable.gallery2,
R.drawable.gallery3, R.drawable.gallery4, R.drawable.gallery5,
R.drawable.gallery6, };
private int index = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gallery = (Gallery) findViewById(R.id.gallery1);
bt1 = (Button) findViewById(R.id.button1);
bt2 = (Button) findViewById(R.id.button2);
bt3 = (Button) findViewById(R.id.button3);
bt4 = (Button) findViewById(R.id.button4);
bt1.setOnClickListener(this);
bt2.setOnClickListener(this);
bt3.setOnClickListener(this);
bt4.setOnClickListener(this);
gallery.setAdapter(new MyAdapter());
Timer timer = new Timer();
timer.schedule(task, 2000, 2000);
}
private TimerTask task = new TimerTask() {
@Override
public void run() {
Message message = new Message();
message.what = 2;
index = gallery.getSelectedItemPosition();
index++;
handler.sendMessage(message);
}
};
Handler handler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case 2:
gallery.setSelection(index);
break;
default:
break;
}
};
};
@Override
public void onClick(View v) {
Intent intent = null;
switch (v.getId()) {
case R.id.button1:
intent=new Intent("Bt1Activity");
break;
case R.id.button2:
intent=new Intent(MainActivity.this,Bt2Activity.class);
break;
case R.id.button3:
intent=new Intent(MainActivity.this,Bt3Activity.class);
break;
case R.id.button4:
intent=new Intent(MainActivity.this,Bt4Activity.class);
break;
}
startActivity(intent);
}
class MyAdapter extends BaseAdapter {
@Override
public int getCount() {
return picture.length;
}
@Override
public Object getItem(int arg0) {
return arg0;
}
@Override
public long getItemId(int arg0) {
return arg0;
}
@Override
public View getView(int position, View arg1, ViewGroup arg2) {
ImageView imageView = new ImageView(MainActivity.this);
imageView.setImageResource(picture[position % picture.length]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);// 设置最佳比例图片显示
imageView.setPadding(10, 20, 10, 20);
imageView.setBackgroundColor(Color.BLACK);
imageView.setLayoutParams(new Gallery.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
return imageView;
}
}
}
activity_main
<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"
tools:context=".MainActivity" >
<Gallery
android:id="@+id/gallery1"
android:layout_width="300dp"
android:layout_height="250dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="58dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/gallery1"
android:layout_marginTop="60dp" >
<Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button1" />
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button2" />
<Button
android:id="@+id/button3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button3" />
<Button
android:id="@+id/button4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button4" />
</LinearLayout>
</RelativeLayout>