自定义MyGally:
<pre name="code" class="java"><span style="font-size:18px;">package com.cn;
import java.util.Timer;
import java.util.TimerTask;
import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.widget.Gallery;
@SuppressWarnings("deprecation")
public class MyGallery extends Gallery {
private static final int timerAnimation = 1;
private final Handler mHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case timerAnimation:
int position = getSelectedItemPosition();
Log.i("msg", "position:" + position);
if (position >= (getCount() - 1)) {
onKeyDown(KeyEvent.KEYCODE_DPAD_LEFT, null);
} else {
onKeyDown(KeyEvent.KEYCODE_DPAD_RIGHT, null);
}
break;
default:
break;
}
};
};
private final Timer timer = new Timer();
private final TimerTask task = new TimerTask() {
public void run() {
mHandler.sendEmptyMessage(timerAnimation);
}
};
public MyGallery(Context paramContext) {
super(paramContext);
timer.schedule(task, 3000, 3000);
}
public MyGallery(Context paramContext, AttributeSet paramAttributeSet) {
super(paramContext, paramAttributeSet);
timer.schedule(task, 3000, 3000);
}
public MyGallery(Context paramContext, AttributeSet paramAttributeSet,
int paramInt) {
super(paramContext, paramAttributeSet, paramInt);
timer.schedule(task, 3000, 3000);
}
private boolean isScrollingLeft(MotionEvent paramMotionEvent1,
MotionEvent paramMotionEvent2) {
float f2 = paramMotionEvent2.getX();
float f1 = paramMotionEvent1.getX();
if (f2 > f1)
return true;
return false;
}
public boolean onFling(MotionEvent paramMotionEvent1,
MotionEvent paramMotionEvent2, float paramFloat1, float paramFloat2) {
int keyCode;
if (isScrollingLeft(paramMotionEvent1, paramMotionEvent2)) {
keyCode = KeyEvent.KEYCODE_DPAD_LEFT;
} else {
keyCode = KeyEvent.KEYCODE_DPAD_RIGHT;
}
onKeyDown(keyCode, null);
return true;
}
public void destroy() {
timer.cancel();
}
}</span>
<strong>
</strong>
布局代码:
<span style="font-size:18px;"><LinearLayout 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"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<com.cn.MyGallery
android:id="@+id/gallery"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9" />
<LinearLayout
android:id="@+id/ll_bottomNavPoint"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_gravity="top|center"
android:layout_weight="0.5"
android:gravity="center"
android:background="@drawable/ic_select_bg"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/ll_focus_indicator_container"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" />
</LinearLayout>
</LinearLayout>
</LinearLayout></span>
<span style="font-size: 18px; background-color: rgb(255, 255, 255);">
</span>
<span style="font-size: 18px; background-color: rgb(255, 255, 255);"><strong>自定义适配器ImgAdapter:</strong></span>
<span style="font-size: 18px; background-color: rgb(255, 255, 255);"><strong>
</strong></span>
<span style="font-size:18px;"></span><pre name="code" class="java">package com.cn;
import java.util.List;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.Gallery.LayoutParams;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
public class ImgAdapter extends BaseAdapter {
private Context _context;
private List<Integer> imgList;
public ImgAdapter(Context context,List<Integer> imgList ) {
_context = context;
this.imgList=imgList;
}
public int getCount() {
return Integer.MAX_VALUE;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
viewHolder = new ViewHolder();
ImageView imageView = new ImageView(_context);
imageView.setAdjustViewBounds(true);
imageView.setScaleType(ScaleType.FIT_XY);
imageView.setLayoutParams(new Gallery.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
convertView = imageView;
viewHolder.imageView = (ImageView) convertView;
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.imageView.setImageResource(imgList.get(position % imgList.size()));
return convertView;
}
private static class ViewHolder {
ImageView imageView;
}
}
具体实现代码:
package com.cn;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
private MyGallery gallery = null;
private ArrayList<Integer> imgList;
private ArrayList<ImageView> portImg;
/**
* 存储上一个选择项的Index
*/
private int preSelImgIndex = 0;
private LinearLayout ll_focus_indicator_container = null;
// private String[] imageUrls = {"http://www.baidu.com",
// "http://www.eoeandroid.com",
// "http://blog.sina.com.cn"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ll_focus_indicator_container = (LinearLayout) findViewById(R.id.ll_focus_indicator_container);
imgList = new ArrayList<Integer>();
imgList.add(R.drawable.img1);
imgList.add(R.drawable.img2);
imgList.add(R.drawable.img3);
InitFocusIndicatorContainer();
gallery = (MyGallery) findViewById(R.id.gallery);
gallery.setAdapter(new ImgAdapter(MainActivity.this, imgList));
gallery.setFocusable(true);
gallery.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int selIndex, long arg3) {
selIndex = selIndex % imgList.size();
// 修改上一次选中项的背景
portImg.get(preSelImgIndex).setImageResource(R.drawable.ic_focus);
// 修改当前选中项的背景
portImg.get(selIndex).setImageResource(R.drawable.ic_focus_select);
preSelImgIndex = selIndex;
}
public void onNothingSelected(AdapterView<?> arg0) {}
});
// gallery.setOnItemClickListener(new OnItemClickListener() {
//
// @Override
// public void onItemClick(AdapterView<?> parent, View view,
// int position, long id) {
// position = position % imgList.size();
// Intent intent = new Intent(Intent.ACTION_VIEW);
// intent.setData(Uri.parse(imageUrls[position]));
// startActivity(intent);
// }
// });
}
private void InitFocusIndicatorContainer() {
portImg = new ArrayList<ImageView>();
for (int i = 0; i < imgList.size(); i++) {
ImageView localImageView = new ImageView(MainActivity.this);
localImageView.setId(i);
ImageView.ScaleType localScaleType = ImageView.ScaleType.FIT_XY;
localImageView.setScaleType(localScaleType);
LinearLayout.LayoutParams localLayoutParams = new LinearLayout.LayoutParams(
24, 24);
localImageView.setLayoutParams(localLayoutParams);
localImageView.setPadding(5, 5, 5, 5);
localImageView.setImageResource(R.drawable.ic_focus);
portImg.add(localImageView);
this.ll_focus_indicator_container.addView(localImageView);
}
}
}<strong>
</strong>