本文主要是介绍过期Gallery取代方案horizontalscrollView之幻灯片效果,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
效果
-------------------------------------------------------------------------------------------------------------------------------
素材
-------------------------------------------------------------------------------------------------------------------------------
activity_main.xml
-------------------------------------------------------------------------------------------------------------------------------
| |
MainActivity.java
-------------------------------------------------------------------------------------------------------------------------------
package com.horizontalscrollView02;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
/**
*
* 项目名称:com.horizontalscrollView02
* 类 名 称:MainActivity
* 类 描 述:幻灯片效果
* 创 建 人:fy
* 创建时间:2014-3-21 下午4:53:31
* Copyright (c) 方勇-版权所有
*/
public class MainActivity extends Activity {
/* 横向滚动菜单 */
private HorizontalScrollView horizontalScrollView;
/* 自定义画廊Gallery */
private MyGallery mGallery;
/* 记录选中的图片位置 */
private int index = 0;
/* 记录幻灯片小圆点个数 */
private static final int IMAGE_COUNT = 3;
/* 记录幻灯片圆点图片 */
private ImageView[] mImageViewIds;// 小圆点ImageView数组
/* 屏幕宽度(像素) */
private int width;
/* 开一个线程执行耗时操作,用于更新主线程的UI控件 */
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 2:
int pos = index % IMAGE_COUNT;
mGallery.setItemSelected(pos);
break;
default:
break;
}
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViews();
setListeners();
setScreens();
initViews();
}
/* 手机屏幕属性 */
private void setScreens() {
// 定义DisplayMetrics 对象
DisplayMetrics metric = new DisplayMetrics();
// 取得窗口属性
getWindowManager().getDefaultDisplay().getMetrics(metric);
width = metric.widthPixels; // 屏幕宽度(像素)
}
private void findViews() {
horizontalScrollView = (HorizontalScrollView) findViewById(R.id.hsv);
mGallery = (MyGallery) findViewById(R.id.gallery);
mImageViewIds = new ImageView[] { (ImageView) findViewById(R.id.dot_1), (ImageView) findViewById(R.id.dot_2),
(ImageView) findViewById(R.id.dot_3) };
}
private void setListeners() {
/* 设置小圆点显示,position会一直增加,如果要循环显示图片,需要对position取余,否则数组越界 */
mGallery.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3) {
// TODO Auto-generated method stub
int pos = position % IMAGE_COUNT;
mImageViewIds[pos].setImageDrawable(getBaseContext().getResources().getDrawable(R.drawable.ic_dot_focused));
if (pos > 0) {
mImageViewIds[pos - 1].setImageDrawable(getBaseContext().getResources().getDrawable(R.drawable.ic_dot_normal));
}
if (pos < (IMAGE_COUNT - 1)) {
// 已经是最后一张图片了,重来(从0位置开始)
horizontalScrollView.scrollTo(0, 0);
mImageViewIds[pos + 1].setImageDrawable(getBaseContext().getResources().getDrawable(R.drawable.ic_dot_normal));
}
if (pos == 0) {
mImageViewIds[IMAGE_COUNT - 1].setImageDrawable(getBaseContext().getResources().getDrawable(R.drawable.ic_dot_normal));
}
// 移动X宽度=当前位置*屏幕宽度
horizontalScrollView.scrollTo(pos * width, 0);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
/* 初始化幻灯片小圆点、画廊、定时器 */
private void initViews() {
// 默认选中
mImageViewIds[0].setImageDrawable(getBaseContext().getResources().getDrawable(R.drawable.ic_dot_focused));
ImageAdapter adapter = new ImageAdapter(this);
// 设置画廊Gallery适配器
mGallery.setAdapter(adapter);
// 定时器,使用Message传递消息
Timer timer = new Timer();
// 每隔2秒执行一次调度,延迟2秒执行
timer.schedule(new TimerTask() {
@Override
public void run() {
Message message = new Message();
message.what = 2;
index = mGallery.getSelectedItemPosition();
index++;
handler.sendMessage(message);
}
}, 2000, 2000);
}
}
ImageAdapter.java
-------------------------------------------------------------------------------------------------------------------------------
package com.horizontalscrollView02;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
/**
*
* 项目名称:com.horizontalscrollView02
* 类 名 称:ImageAdapter
* 类 描 述:图片适配器
* 创 建 人:fy
* 创建时间:2014-3-21 下午5:08:56
* Copyright (c) 方勇-版权所有
*/
public class ImageAdapter extends BaseAdapter {
private Context context;
private int[] mImages = { R.drawable.img001, R.drawable.img002, R.drawable.img003 };
public ImageAdapter(Context context) {
this.context = context;
}
@Override
public int getCount() {
return mImages.length;// 实现循环显示
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if(null==convertView){
imageView = new ImageView(context);
}else{
imageView = (ImageView) convertView;
}
imageView.setImageResource(mImages[position]);
return imageView;
}
}
MyGallery.java
-------------------------------------------------------------------------------------------------------------------------------
package com.horizontalscrollView02;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.Toast;
/**
*
* 项目名称:com.horizontalscrollView02
* 类 名 称:MyGallery
* 类 描 述:自定义Gallery画廊
* 创 建 人:fy
* 创建时间:2014-3-21 下午2:27:03
* Copyright (c) 方勇-版权所有
*/
public class MyGallery extends LinearLayout {
/* 上下文 */
private Context mContext;
/* 适配器 */
private BaseAdapter adapter;
/* 适配器-->条目选中事件 */
private AdapterView.OnItemSelectedListener onItemSelectedListener;
public static int selectedId = 0;
public MyGallery(Context context) {
super(context);
mContext = context;
setOrientation(HORIZONTAL);
}
public MyGallery(Context context, AttributeSet attrs) {
super(context,attrs);
mContext = context;
setOrientation(HORIZONTAL);
}
/* 适配器-->设置数据 */
public void setAdapter(BaseAdapter adapter) {
this.adapter = adapter;
// 设置条目touch事件
for (int i = 0; i < adapter.getCount(); i++) {
// 条目
View view = adapter.getView(i, null, null);
// 条目位置编号
final int position = i;
// 条目ID
final long id = adapter.getItemId(position);
// 条目touch事件
view.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (onItemSelectedListener != null) {
/**
* <p>Callback method to be invoked when an item in this view has been
* selected. This callback is invoked only when the newly selected
* position is different from the previously selected position or if
* there was no selected item.</p>
*
* Impelmenters can call getItemAtPosition(position) if they need to access the
* data associated with the selected item.
*
* @param parent The AdapterView where the selection happened
* @param view The view within the AdapterView that was clicked
* @param position The position of the view in the adapter
* @param id The row id of the item that is selected
*/
onItemSelectedListener.onItemSelected(null, v, position, id);
}
//选中的位置
selectedId = position;
return true;
}
});
this.addView(view, new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
}
}
public void setItemSelected(int position) {
Toast.makeText(mContext, "" + position, Toast.LENGTH_SHORT).show();
//选中的位置
selectedId = position;
//选中的视图
View view = (View) adapter.getView(position, null, null);
onItemSelectedListener.onItemSelected(null,view, position, getItemIdAtPosition(position));
}
public int getSelectedItemPosition(){
return selectedId;
}
public long getItemIdAtPosition(int position) {
return (adapter == null || position < 0) ? null : adapter.getItemId(position);
}
public void setOnItemSelectedListener(AdapterView.OnItemSelectedListener onItemSelectedListener) {
this.onItemSelectedListener = onItemSelectedListener;
}
}
这篇关于过期Gallery取代方案horizontalscrollView之幻灯片效果的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!