欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android自定義ViewGroup實(shí)現(xiàn)可滾動(dòng)的橫向布局(2)

 更新時(shí)間:2016年12月20日 14:07:20   作者:封魔之殤  
這篇文章主要介紹了Android自定義ViewGroup實(shí)現(xiàn)可滾動(dòng)的橫向布局,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

上一篇文章自定義viewgroup(1)地址:http://www.dbjr.com.cn/article/100608.htm

這里直接代碼:

package com.example.libingyuan.horizontallistview.ScrollViewGroup;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Scroller;

/**
 * 自定義ViewGroup
 * 在橫向布局的基礎(chǔ)上,增加啦滾動(dòng)效果,但是沒(méi)有邊界限制
 */
public class ScrollViewGroup extends ViewGroup {
  private Scroller mScroller;
  private float mLastMotionX = 0;

  public ScrollViewGroup(Context context) {
    this(context, null);
  }

  public ScrollViewGroup(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
  }

  public ScrollViewGroup(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init(context);
  }

  private void init(Context context) {
    mScroller = new Scroller(context);
  }

  @Override
  public void computeScroll() {
    if (mScroller.computeScrollOffset()) {
      scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
      postInvalidate();
    }
  }


  @Override
  public boolean onTouchEvent(MotionEvent event) {
    // TODO Auto-generated method stub
    int action = event.getAction();
    float x = event.getX();
    switch (action) {
      case MotionEvent.ACTION_DOWN:
        if (!mScroller.isFinished()) {
          mScroller.abortAnimation();
        }
        mLastMotionX = event.getX();
        break;
      case MotionEvent.ACTION_MOVE:
        float delt = mLastMotionX - x;
        mLastMotionX = x;
        scrollBy((int) delt, 0);
        break;
      case MotionEvent.ACTION_UP:
        invalidate();
        break;

      default:
        break;
    }

    return true;
  }

  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    //重新設(shè)置寬高
    this.setMeasuredDimension(measureWidth(widthMeasureSpec, heightMeasureSpec), measureHeight(widthMeasureSpec, heightMeasureSpec));
  }

  /**
   * 測(cè)量寬度
   */
  private int measureWidth(int widthMeasureSpec, int heightMeasureSpec) {
    // 寬度
    int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
    int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
    //父控件的寬(wrap_content)
    int width = 0;
    int childCount = getChildCount();

    //重新測(cè)量子view的寬度,以及最大高度
    for (int i = 0; i < childCount; i++) {
      View child = getChildAt(i);
      measureChild(child, widthMeasureSpec, heightMeasureSpec);
      MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
      int childWidth = child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
      width += childWidth;
    }
    return modeWidth == MeasureSpec.EXACTLY ? sizeWidth : width;
  }

  /**
   * 測(cè)量高度
   */
  private int measureHeight(int widthMeasureSpec, int heightMeasureSpec) {
    //高度
    int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);
    int modeHeight = MeasureSpec.getMode(heightMeasureSpec);
    //父控件的高(wrap_content)
    int height = 0;
    int childCount = getChildCount();

    //重新測(cè)量子view的寬度,以及最大高度
    for (int i = 0; i < childCount; i++) {
      View child = getChildAt(i);
      measureChild(child, widthMeasureSpec, heightMeasureSpec);
      MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
      int childHeight = child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;
      height += childHeight;
    }
    height = height / childCount;
    return modeHeight == MeasureSpec.EXACTLY ? sizeHeight : height;
  }

  @Override
  protected void onLayout(boolean changed, int l, int t, int r, int b) {
    int childLeft = 0;
    int childWidth;
    int height = getHeight();
    int childCount = getChildCount();
    for (int i = 0; i < childCount; i++) {
      View child = getChildAt(i);
      MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
      childWidth = child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
      child.layout(childLeft, 0, childLeft + childWidth, height);
      childLeft += childWidth;
    }
  }

  @Override
  public LayoutParams generateLayoutParams(AttributeSet attrs) {
    return new MarginLayoutParams(getContext(), attrs);
  }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android?Retrofit使用詳細(xì)教程

    Android?Retrofit使用詳細(xì)教程

    Retrofit是Android用來(lái)接口請(qǐng)求的網(wǎng)絡(luò)框架,內(nèi)部是基于OkHttp實(shí)現(xiàn)的,retrofit負(fù)責(zé)接口請(qǐng)求的封裝,retrofit可以直接將接口數(shù)據(jù)解析為Bean類(lèi)、List集合等,直接簡(jiǎn)化了中間繁瑣的數(shù)據(jù)解析過(guò)程,這篇文章主要介紹了Android?Retrofit使用詳情,需要的朋友可以參考下
    2024-03-03
  • Android基于widget組件實(shí)現(xiàn)物體移動(dòng)/控件拖動(dòng)功能示例

    Android基于widget組件實(shí)現(xiàn)物體移動(dòng)/控件拖動(dòng)功能示例

    這篇文章主要介紹了Android基于widget組件實(shí)現(xiàn)物體移動(dòng)/控件拖動(dòng)功能,結(jié)合實(shí)例形式分析了widget組件在桌面應(yīng)用中的事件響應(yīng)與屬性動(dòng)態(tài)操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2016-10-10
  • 基于Android Flutter編寫(xiě)貪吃蛇游戲

    基于Android Flutter編寫(xiě)貪吃蛇游戲

    貪吃蛇是一款足夠經(jīng)典的游戲。本文將利用Android中的Flutter編寫(xiě)這一經(jīng)典的小游戲,文中的示例代碼講解詳細(xì),感興趣的可以了解一下
    2022-03-03
  • Android實(shí)現(xiàn)長(zhǎng)按圓環(huán)動(dòng)畫(huà)View效果的思路代碼

    Android實(shí)現(xiàn)長(zhǎng)按圓環(huán)動(dòng)畫(huà)View效果的思路代碼

    這篇文章主要介紹了Android實(shí)現(xiàn)長(zhǎng)按圓環(huán)動(dòng)畫(huà)View效果,本文給大家分享實(shí)現(xiàn)思路,通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-09-09
  • Kotlin設(shè)計(jì)模式之委托模式使用方法詳解

    Kotlin設(shè)計(jì)模式之委托模式使用方法詳解

    Kotlin提供了兩個(gè)本機(jī)功能來(lái)實(shí)現(xiàn)委托模式,第一個(gè)是接口委托(例如策略模式),另一種是屬性委托,它專(zhuān)注于類(lèi)成員/屬性(例如延遲加載、observable等),它們共同提供了一組豐富而簡(jiǎn)潔的功能,通過(guò)本博客,您將了解在什么情況下使用此模式
    2023-09-09
  • TextView實(shí)現(xiàn)圖文混合編排的方法

    TextView實(shí)現(xiàn)圖文混合編排的方法

    這篇文章主要為大家詳細(xì)介紹了TextView實(shí)現(xiàn)圖文混合編排的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • Android基于ImageView繪制的開(kāi)關(guān)按鈕效果示例

    Android基于ImageView繪制的開(kāi)關(guān)按鈕效果示例

    這篇文章主要介紹了Android基于ImageView繪制的開(kāi)關(guān)按鈕效果,結(jié)合實(shí)例形式分析了Android使用ImageView進(jìn)行按鈕繪制的界面布局、功能實(shí)現(xiàn)及相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2017-03-03
  • Android 跨進(jìn)程通Messenger(簡(jiǎn)單易懂)

    Android 跨進(jìn)程通Messenger(簡(jiǎn)單易懂)

    這篇文章主要介紹了Android Messenger跨進(jìn)程通的相關(guān)資料,非常簡(jiǎn)單容易理解,對(duì)android messenger 進(jìn)程通訊的相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧
    2016-08-08
  • Android開(kāi)發(fā)之多線程中實(shí)現(xiàn)利用自定義控件繪制小球并完成小球自動(dòng)下落功能實(shí)例

    Android開(kāi)發(fā)之多線程中實(shí)現(xiàn)利用自定義控件繪制小球并完成小球自動(dòng)下落功能實(shí)例

    這篇文章主要介紹了Android開(kāi)發(fā)之多線程中實(shí)現(xiàn)利用自定義控件繪制小球并完成小球自動(dòng)下落功能的方法,涉及Android多線程編程及圖形繪制相關(guān)技巧,需要的朋友可以參考下
    2015-12-12
  • Android通用LoadingView加載框架詳解

    Android通用LoadingView加載框架詳解

    這篇文章主要為大家詳細(xì)介紹了Android通用LoadingView加載框架的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-07-07

最新評(píng)論