现在的位置: 首页 > 综合 > 正文

Gallery一次滚动显示一张图片

2013年09月12日 ⁄ 综合 ⁄ 共 4249字 ⁄ 字号 评论关闭

如上图的广告控件需求

  1. 每次显示一张图片

  2. 每次只能滑动一张

  3. 可以自动播放

  • 自定义广告控件继承自Gallery

/**

* 图片gallery 每次只能滑动一个

* @author cmm

* @date 2012-02-02

* @version 1.0

*/

public class ImageGallery extends Gallery {

public ImageGallery(Context context, AttributeSet paramAttributeSet) {

super(context, paramAttributeSet);

}

@Override

public boolean onFling(MotionEvent paramMotionEvent1,

MotionEvent paramMotionEvent2, float paramFloat1, float paramFloat2) {

return false;

}

@Override

public boolean onScroll(MotionEvent paramMotionEvent1,MotionEvent paramMotionEvent2, float paramFloat1, float paramFloat2) {

float f = 1.4F * paramFloat1;

return super.onScroll(paramMotionEvent1, paramMotionEvent2, f,paramFloat2);

}http://www.eoeandroid.com/thread-102526-1-1.html

@Override

protected boolean getChildStaticTransformation(View child, Transformation t) {

return true;

}

}

  • Adapter item 布局 宽度FillParent

<?xml version="1.0" encoding="UTF-8"?>

<!-- 广告项item -->

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

  android:layout_width="fill_parent"

  android:layout_height="wrap_content" >

  <ImageView

      android:id="@+id/qianghaoqi_ad_item_image"

     android:layout_width="fill_parent"

      android:layout_height="300.0dip"

      android:paddingBottom="20.0dip"

      android:paddingLeft="20.0dip"

      android:paddingRight="20.0dip"

      android:paddingTop="28.0dip"

      android:scaleType="fitXY" />

</RelativeLayout>

  • 自动播放

/**

* 自动滚动播放 滚动播放主要是采用Timer和Handler联合实现。 Timer中负责定时更新消息的生成以及传送,

* 由Handler接收到处理消息后重新设置当前显示的图片。

*/

final Handler mAutoGalleryHandler = new Handler() {

public void handleMessage(Message message) {

super.handleMessage(message);

switch (message.what) {

case HandlerTypeUtils.HANDLER_TYPE_LOAD_DATA_MAIN_GALLERY_POS:

mImageGallery.setSelection(message.getData().getInt(

ConstantUtil.GALLERY_POS));

break;

}

}

};

Timer mAutoTimer;


/**

* Timer 负责定时更新消息的生成以及传送, aDelay秒后,每隔aPeriod秒执行信息发送

*

* @param aDelay

* @param aPeriod

*/

protected void setTimer(long aDelay, long aPeriod,final int aPos) {

if (mAutoTimer!=null) {

mAutoTimer.cancel();

}

mAutoTimer = new Timer();

mAutoTimer.schedule(new TimerTask() {

int gallerypisition = aPos;


@Override

public void run() {

if (gallerypisition < mCount - 1) {

gallerypisition = gallerypisition + 1;

} else {

gallerypisition = 0;

}


Message msg = new Message();

Bundle date = new Bundle();// 存放数据

date.putInt(ConstantUtil.GALLERY_POS, gallerypisition);

msg.setData(date);

msg.what = HandlerTypeUtils.HANDLER_TYPE_LOAD_DATA_MAIN_GALLERY_POS;// 消息标识

mAutoGalleryHandler.sendMessage(msg);

}

}, aDelay, aPeriod);// 5秒后,每隔5秒执行信息发送

}

  • 布局文件 Gallery药包含在FrameLayout中,防止抖动

<!-- 首页广告 -->

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"

  xmlns:myspace="http://schemas.android.com/apk/res/com.blade.qianghaoqi"

  android:id="@+id/qianghaoqi_ad_layout"

  android:layout_width="fill_parent"

  android:layout_height="wrap_content"

  android:orientation="vertical" >

  <com.blade.qianghaoqi.ui.uiCommon.baseView.ImageGallery

      android:id="@+id/qianghaoqi_ad_gallery"

      android:layout_width="fill_parent"

      android:layout_height="wrap_content"

      android:animationDuration="100"

      android:fadingEdge="none"

      android:spacing="0.0dip" />

<!--添加标志点-->

<LinearLayout

      android:id="@+id/qianghaoqi_ad_position"

      android:layout_width="wrap_content"

      android:layout_height="wrap_content"

      android:layout_gravity="bottom|center"

      android:orientation="horizontal"

      android:paddingBottom="5.0dip" >

  </LinearLayout>

</FrameLayout>

  • 加载标志点

/**

* 加载标志点

*/

protected void loadPositionImage() {

ImageView aImageView = null;

mLinearLayout.removeAllViews();

for (int i = 0; i < mCount; i++) {

aImageView = new ImageView(this);

if (i == 0) {

aImageView

.setImageResource(R.drawable.game_blade_qianghaoqi_alert_dialog_help_selected);

} else {

aImageView

.setImageResource(R.drawable.game_blade_qianghaoqi_alert_dialog_help_unselected);

}

mLinearLayout.addView(aImageView);

}

}

/**

* 更改标志点状态

*

* @param aPos

*/

public void changePositionImage(int aPos) {

ImageView aImageView = (ImageView) mLinearLayout.getChildAt(aPos);

if (aImageView != null) {

aImageView

.setImageResource(R.drawable.game_blade_qianghaoqi_alert_dialog_help_selected);

}

for (int i = 0; i < mCount; i++) {

if (i != aPos) {

aImageView = (ImageView) mLinearLayout.getChildAt(i);

aImageView

.setImageResource(R.drawable.game_blade_qianghaoqi_alert_dialog_help_unselected);

}

}

}

@Override

public void onItemSelected(AdapterView<?> parent, View view, int position,

long id) {

changePositionImage(position);

setActionAdData(position);

}

 

 

 

转自:http://mmandroid.diandian.com/post/2012-02-27/16773046

 

牛人博客空间

 

抱歉!评论已关闭.