gallery显示图片轮换(手动和自动)
16lz
2021-01-23
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
};
}
<?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
};
}
更多相关文章
- android 开发常用代码备查[更新20150520]
- android常用代码积累
- android典型代码系列(八)------传递一个String进行MD5编码
- 如何在 Android 上优雅地实现截屏?(附代码)
- Android一些常用知识和代码(不断更新)
- 使用Git下载Google Android源代码