怎么在Android中自定义ViewFlipper实现一个滚动效果
这期内容当中小编将会给大家带来有关怎么在Android中自定义ViewFlipper实现一个滚动效果,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
10年的昭化网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。成都全网营销推广的优势是能够根据用户设备显示端的尺寸不同,自动调整昭化建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联建站从事“昭化网站设计”,“昭化网站推广”以来,每个客户项目都认真落实执行。
Android是什么
Android是一种基于Linux内核的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由美国Google公司和开放手机联盟领导及开发。
代码:
public class ViewFlipper extends LinearLayout { private final int MAX_SHOW_ITEM_SIZE = 5; private IAdapter mIAdapter; private int mCount; //最后一个item动画 private Animation mLastOneAnimation; //其它item动画 private Animation mCommonAnimation; //数据下标 private int mCurrentIndex; /** * 这里动画时间是1600毫秒,所以间隔得大于动画时间 */ private static final int DEFAULT_INTERVAL = 2000; private int mFlipInterval = DEFAULT_INTERVAL; private boolean mAutoStart = false; private boolean mRunning = false; private boolean mStarted = false; private boolean mVisible = false; private boolean mUserPresent = true; public ViewFlipper(Context context) { super(context); init(context); } public ViewFlipper(Context context, AttributeSet attrs) { super(context, attrs); init(context); } public ViewFlipper(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context); } private final BroadcastReceiver mReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { final String action = intent.getAction(); if (Intent.ACTION_SCREEN_OFF.equals(action)) { mUserPresent = false; updateRunning(); } else if (Intent.ACTION_USER_PRESENT.equals(action)) { mUserPresent = true; updateRunning(false); } } }; @Override protected void onAttachedToWindow() { super.onAttachedToWindow(); // Listen for broadcasts related to user-presence final IntentFilter filter = new IntentFilter(); filter.addAction(Intent.ACTION_SCREEN_OFF); filter.addAction(Intent.ACTION_USER_PRESENT); // OK, this is gross but needed. This class is supported by the // remote views machanism and as a part of that the remote views // can be inflated by a context for another user without the app // having interact users permission - just for loading resources. // For exmaple, when adding widgets from a user profile to the // home screen. Therefore, we register the receiver as the current // user not the one the context is for. getContext().registerReceiver(mReceiver, filter); if (mAutoStart) { // Automatically start when requested startFlipping(); } } @Override protected void onDetachedFromWindow() { super.onDetachedFromWindow(); mVisible = false; getContext().unregisterReceiver(mReceiver); updateRunning(); } @Override protected void onWindowVisibilityChanged(int visibility) { super.onWindowVisibilityChanged(visibility); mVisible = visibility == VISIBLE; updateRunning(mVisible); // updateRunning(false); } private void init(Context context) { this.setOrientation(LinearLayout.VERTICAL); } public void setIAdapter(IAdapter iAdapter) { this.mIAdapter = iAdapter; initShowItems(); } public void startFlipping() { mStarted = true; updateRunning(); } public void stopFlipping() { mStarted = false; updateRunning(); } private void updateRunning() { updateRunning(true); } /** * Returns true if the child views are flipping. */ public boolean isFlipping() { return mStarted; } /** * Set if this view automatically calls {@link #startFlipping()} when it * becomes attached to a window. */ public void setAutoStart(boolean autoStart) { mAutoStart = autoStart; } /** * Returns true if this view automatically calls {@link #startFlipping()} * when it becomes attached to a window. */ public boolean isAutoStart() { return mAutoStart; } @Override public void onInitializeAccessibilityEvent(AccessibilityEvent event) { super.onInitializeAccessibilityEvent(event); event.setClassName(ViewFlipper.class.getName()); } @Override public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) { super.onInitializeAccessibilityNodeInfo(info); info.setClassName(ViewFlipper.class.getName()); } /** * 初始化childViews */ private void initShowItems() { if (mIAdapter != null) { mCount = mIAdapter.getCount(); for (int i = 0; i < mCount; i++) { if (i == MAX_SHOW_ITEM_SIZE) { break; } View convertView = getChildAt(i); View item = mIAdapter.getItemView(convertView, i); addView(item, i); } } } /** * Internal method to start or stop dispatching flip {@link android.os.Message} based * on {@link #mRunning} and {@link #mVisible} state. * * @param flipNow Determines whether or not to execute the animation now, in * addition to queuing future flips. If omitted, defaults to * true. */ private void updateRunning(boolean flipNow) { boolean running = mVisible && mStarted && mUserPresent; System.out.println(" updateRunning running:" + running + " mVisible " + mVisible + " userPresent " + mUserPresent); if (running != mRunning) { if (running && (mCount > MAX_SHOW_ITEM_SIZE)) { showItems(mCurrentIndex++, flipNow); Message msg = mHandler.obtainMessage(FLIP_MSG); mHandler.sendMessageDelayed(msg, mFlipInterval); } else { mHandler.removeMessages(FLIP_MSG); } mRunning = running; } } private void showItems(final int position, boolean animate) { if (animate && (mLastOneAnimation == null || mCommonAnimation == null)) { mLastOneAnimation = AnimationUtils.loadAnimation(getContext(), R.anim.lastone_anim); mCommonAnimation = AnimationUtils.loadAnimation(getContext(), R.anim.common_anim); } int childCount = getChildCount(); for (int i = 0; i < childCount; i++) { View child = getChildAt(i); child.clearAnimation(); int index = position + i; child = mIAdapter.getItemView(child, (index >= mIAdapter.getCount()) ? (index - mIAdapter.getCount()) : index); if (animate) { if (i == childCount - 1) { child.setAnimation(mLastOneAnimation); } else { child.setAnimation(mCommonAnimation); } } child.setVisibility(View.VISIBLE); } if (animate) { mCommonAnimation.startNow(); mLastOneAnimation.startNow(); } //保证传入的position小于getCount if (mCurrentIndex >= mIAdapter.getCount()) { mCurrentIndex = 0; } } private final int FLIP_MSG = 1; private final Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { if (msg.what == FLIP_MSG) { if (mRunning) { showItems(mCurrentIndex++, true); msg = obtainMessage(FLIP_MSG); sendMessageDelayed(msg, mFlipInterval); } } } }; public interface IAdapter { /** * @param convertView * @param position * @return */ public View getItemView(View convertView, int position); /** * @return 数据count */ public int getCount(); } }
再来看看调用部分:
public class MainActivity extends ActionBarActivity implements ViewFlipper.IAdapter { ViewFlipper viewFlipper; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); viewFlipper = (ViewFlipper) findViewById(R.id.view_flipper); viewFlipper.setIAdapter(this); } @Override protected void onResume() { super.onResume(); viewFlipper.startFlipping(); } @Override public View getItemView(View convertView, int position) { View item = null; TextView textView; if (convertView == null) { item = View.inflate(this, R.layout.item, null); } else { item = convertView; } textView = (TextView) item.findViewById(R.id.textview); textView.setText("测试数据:" + position); return item; } @Override public int getCount() { return 8; } }
上述就是小编为大家分享的怎么在Android中自定义ViewFlipper实现一个滚动效果了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联行业资讯频道。
网站标题:怎么在Android中自定义ViewFlipper实现一个滚动效果
文章路径:http://azwzsj.com/article/jdgghp.html