栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

android实现自动滚动的Gallary控件效果

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

android实现自动滚动的Gallary控件效果

本文实例讲述了android实现自动滚动的Gallary控件。分享给大家供大家参考。具体如下:

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;
public class HomeGallery extends Gallery
{
  
  private static final int timerAnimation = 1;
  private static final int time = 2000;
  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 HomeGallery(Context paramContext)
  {
    super(paramContext);
    timer.schedule(task, time, time);
  }
  public HomeGallery(Context paramContext, AttributeSet paramAttributeSet)
  {
    super(paramContext, paramAttributeSet);
    timer.schedule(task, time, time);
  }
  public HomeGallery(Context paramContext, AttributeSet paramAttributeSet,
      int paramInt)
  {
    super(paramContext, paramAttributeSet, paramInt);
    timer.schedule(task, time, time);
  }
  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();
  }
}

希望本文所述对大家的Android程序设计有所帮助。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/164160.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号