怎么在Android中自定義ViewFlipper實現(xiàn)一個滾動效果

這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)怎么在Android中自定義ViewFlipper實現(xiàn)一個滾動效果,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

10年的昭化網(wǎng)站建設(shè)經(jīng)驗,針對設(shè)計、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時及時工作處理。成都全網(wǎng)營銷推廣的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整昭化建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計,從而大程度地提升瀏覽體驗。創(chuàng)新互聯(lián)建站從事“昭化網(wǎng)站設(shè)計”,“昭化網(wǎng)站推廣”以來,每個客戶項目都認(rèn)真落實執(zhí)行。

Android是什么

Android是一種基于Linux內(nèi)核的自由及開放源代碼的操作系統(tǒng),主要使用于移動設(shè)備,如智能手機(jī)和平板電腦,由美國Google公司和開放手機(jī)聯(lián)盟領(lǐng)導(dǎo)及開發(fā)。

代碼:

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;
 
 //數(shù)據(jù)下標(biāo)
 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 數(shù)據(jù)count
   */
  public int getCount();
 
 }
 
}

再來看看調(diào)用部分:

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("測試數(shù)據(jù):" + position);
  return item;
 }
 
 @Override
 public int getCount() {
  return 8;
 }
}

上述就是小編為大家分享的怎么在Android中自定義ViewFlipper實現(xiàn)一個滾動效果了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

當(dāng)前標(biāo)題:怎么在Android中自定義ViewFlipper實現(xiàn)一個滾動效果
標(biāo)題來源:http://bm7419.com/article48/jdgghp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、建站公司動態(tài)網(wǎng)站、手機(jī)網(wǎng)站建設(shè)、做網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)