1.xml
2.Activity
------------------------------------------------------------------------------------------------------------------
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="150dp" >
<Gallery
android:id="@+id/gallery"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animationDuration="100"
android:fadingEdge="none"
android:unselectedAlpha="0" />
<Button
android:id="@+id/button1"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true" />
<Button
android:id="@+id/button2"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="@+id/button1" />
<Button
android:id="@+id/button3"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="@+id/button2" />
</RelativeLayout>
</FrameLayout>
--------------------------------------------------------------------------------------------------
package com.example.viewparge;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
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;
public class MainActivity extends Activity implements OnClickListener {
Gallery gallery;
ImageView iv;
private int[] imageId = { R.drawable.test, R.drawable.ic_launcher,
R.drawable.test };
int index;
Button button1, button2, button3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
gallery = (Gallery) findViewById(R.id.gallery);
gallery.setAdapter(adapter);
gallery.setSelection(0);//imageId.length / 2
timer.schedule(task, 2500, 2500);
}
BaseAdapter adapter = new BaseAdapter() {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
iv = new ImageView(MainActivity.this);
iv.setScaleType(ImageView.ScaleType.FIT_XY);
iv.setLayoutParams(new Gallery.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
TypedArray typedArray = obtainStyledAttributes(R.styleable.Gallery);
iv.setBackgroundResource(typedArray.getResourceId(
R.styleable.Gallery_android_galleryItemBackground, 0));
iv.setPadding(0, 0, 0, 0);
} else {
iv = (ImageView) convertView;
}
iv.setImageResource(imageId[position]);
return iv;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return imageId.length;
}
};
Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
if (msg.arg1 == 1) {
gallery.onKeyDown(KeyEvent.KEYCODE_DPAD_RIGHT, null);
}
//如果 gallery当前照片的下标 与 数组下标相同时、证明移动到了尾端
//重新将gallery 设置为开始位置
if((Integer) gallery.getSelectedItem()==imageId.length-1){
System.err.println((Integer) gallery.getSelectedItem());
System.err.println(imageId.length-1);
// gallery.getSelectedItemId();
// gallery.getSelectedItemPosition();
gallery.setSelection(0);
}
};
};
Timer timer = new Timer();
TimerTask task = new TimerTask() {
// 2015/9/27(星期天)
@Override
public void run() {
Message msg = Message.obtain();
msg.arg1 = 1;
handler.sendMessage(msg);
// gallery.onKeyDown(KeyEvent.KEYCODE_DPAD_RIGHT,null);
}
};
@Override
public void onClick(View v) {
}
}
--------------------------------------------------------------------------------------------------------------
新建attr.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="Gallery">
<attr name="android:galleryItemBackground"/>
</declare-styleable>
</resources>
--------------------------------------------------------------------------------------------------------------
还需将按钮与照片一一对应、
并未实现---
思路---
定义index--与数组下标对应---