1.gallery.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:cacheColorHint="#00000000"
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<FrameLayout
android:id="@+id/gallerylayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<!-- 广告图片 -->
<Gallery android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="160dp"
android:spacing="1dp"
android:fadingEdge="none"
/>
<LinearLayout
android:orientation="vertical"
android:layout_gravity="bottom"
android:background="@drawable/boke_titlebg"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextSwitcher
android:id="@+id/TextSwitcher01"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TextSwitcher>
<LinearLayout
android:id="@+id/hintlayout"
android:layout_gravity="right"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</LinearLayout>
</LinearLayout>
</FrameLayout>
</LinearLayout>
2.代码中添加
package com.example.imageswicher;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher;
public class GalleryActivity extends Activity implements ViewSwitcher.ViewFactory{
private LinearLayout mHintLayout;
private TextSwitcher switcher;
private Gallery g;
private int gallerypisition = 0;
private int count;
private Timer autoGallery;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.adergallery);
mHintLayout = (LinearLayout) findViewById(R.id.hintlayout);
switcher = (TextSwitcher) findViewById(R.id.TextSwitcher01);
switcher.setFactory(this);
g = (Gallery)findViewById(R.id.gallery);
InitialMain();
}
private void InitialMain() {
g.setSelection(gallerypisition);
g.setAdapter(new ImageAdapter(this));
g.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adapterView, View view,
int position, long id) {

}

});
g.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (autoGallery != null) {
autoGallery.cancel();
autoGallery = null;
}
} else if (event.getAction() == MotionEvent.ACTION_UP) {
if (autoGallery == null) {
audoGallery();
}
}
return false;
}
});
g.setOnItemSelectedListener(new OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
gallerypisition = position;
for (int i = 0; i < count; i++) {
((TextView) mHintLayout.getChildAt(i))
.setBackgroundResource(R.drawable.nofocus);

}
((TextView) mHintLayout.getChildAt(position))
.setBackgroundResource(R.drawable.focus);
// switcher.setText(Title[position]);
}

@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub

}

});
count = mImageIds.length;
fillHintLayout();

}

private void fillHintLayout() {
for (int i = 0; i < count; i++) {
TextView textView = new TextView(this);
String textvalue = String.valueOf(i + 1);
textView.setTextColor(Color.WHITE);
textView.setText(textvalue);
textView.setGravity(Gravity.CENTER);
textView.setBackgroundResource(R.drawable.nofocus);
mHintLayout.addView(textView);
}
}
private void audoGallery() {
autoGallery = new Timer();
autoGallery.schedule(new TimerTask() {
@Override
public void run() {
if (gallerypisition < count - 1) {
gallerypisition = gallerypisition + 1;
} else {
gallerypisition = 0;
}
autoGalleryHandler.sendEmptyMessage(1);
}
}, 5000, 5000);
}
Handler autoGalleryHandler = new Handler() {
public void handleMessage(Message message) {
super.handleMessage(message);
switch (message.what)
{
case 1:
g.setSelection(gallerypisition);
break;
}
}
};
@Override
public View makeView() {
// TODO Auto-generated method stub
TextView tv = new TextView(this);
tv.setTextColor(Color.WHITE);
tv.setTextSize(18);
return tv;
}

public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;

public ImageAdapter(Context c) {
mContext = c;
}

public int getCount() {
return mImageIds.length;
}

public Object getItem(int position) {
return position;
}

public long getItemId(int position) {
return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(mContext);

i.setImageResource(mImageIds[position]);
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setLayoutParams(new Gallery.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

// The preferred Gallery item background
i.setBackgroundResource(mGalleryItemBackground);

return i;
}

private Context mContext;

}
private Integer[] mImageIds = {
R.drawable.android_jd_buy_loading,
R.drawable.android_jd_buy_loading,
R.drawable.android_jd_buy_loading,
R.drawable.android_jd_buy_loading,
R.drawable.android_jd_buy_loading
};
}

更多相关文章

  1. android 开发常用代码备查[更新20150520]
  2. android常用代码积累
  3. android典型代码系列(八)------传递一个String进行MD5编码
  4. 如何在 Android 上优雅地实现截屏?(附代码)
  5. Android一些常用知识和代码(不断更新)
  6. 使用Git下载Google Android源代码

随机推荐

  1. android Button实现水波纹效果
  2. android视频处理相关资料
  3. Android(安卓)面试 指南:互联网大厂需要怎
  4. Android TextView 最全的XML属性
  5. Android发送SOAP数据给服务器调用webserv
  6. Android 开发入门-详解4种基本布局
  7. 《android开发应用实战详解》光盘源代码
  8. Ogre 1.9 RC1 发布了,有了官方的Android支
  9. android 一直在最前面的浮动窗口效果
  10. Android Camera使用小结