Android自定義viewgroup快速滑動(dòng)(4)
上一篇文章自定義viewgroup(3)地址:http://www.dbjr.com.cn/article/100618.htm
代碼:
package com.example.libingyuan.horizontallistview.ScrollViewGroup;
import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Scroller;
/**
* 自定義ViewGroup
* 增加了加速度滑動(dòng)
*/
public class ScrollViewGroup extends ViewGroup {
//滾動(dòng)計(jì)算輔助類
private Scroller mScroller;
//手指落點(diǎn)的X坐標(biāo)
private float mLastMotionX = 0;
//屏幕寬度
private int screenWidth;
//手指加速度輔助類
private VelocityTracker mVelocityTracker;
//每秒移動(dòng)的最小dp
private int mMinimumVelocity;
//每秒移動(dòng)的最大dp
private int mMaximumVelocity;
/**
* 使用new關(guān)鍵字創(chuàng)建對(duì)象的時(shí)候調(diào)用
*
* @param context 上下文
*/
public ScrollViewGroup(Context context) {
this(context, null);
}
/**
* 在XML文件中使用的時(shí)候調(diào)用
*
* @param context 上下文
* @param attrs 屬性:如 android:layout_width="wrap_content"
*/
public ScrollViewGroup(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
/**
* 在xml文件中調(diào)用,并且使用了自定義屬性的時(shí)候調(diào)用
*
* @param context 上下文
* @param attrs 屬性:如 android:layout_width="wrap_content"
* @param defStyleAttr 自定義屬性的id
*/
public ScrollViewGroup(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
/**
* 初始化方法
* 初始化滾動(dòng)輔助類Scroller以及計(jì)算出屏幕寬度
*
* @param context 上下文
*/
private void init(Context context) {
//初始化輔助類
mScroller = new Scroller(context);
//獲取屏幕寬度
WindowManager manager = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics outMetrics = new DisplayMetrics();
manager.getDefaultDisplay().getMetrics(outMetrics);
screenWidth = outMetrics.widthPixels;
//獲取最小和最大的移動(dòng)距離
final ViewConfiguration configuration = ViewConfiguration.get(context);
mMinimumVelocity = configuration.getScaledMinimumFlingVelocity();
mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
}
/**
* 滾動(dòng)時(shí)需要重寫(xiě)的方法,用于控制滾動(dòng)
*/
@Override
public void computeScroll() {
//判斷滾動(dòng)時(shí)候停止
if (mScroller.computeScrollOffset()) {
//滾動(dòng)到指定的位置
scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
//這句話必須寫(xiě),否則不能實(shí)時(shí)刷新
postInvalidate();
}
}
/**
* 手指觸屏事件監(jiān)聽(tīng)
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
int action = event.getAction();
//獲取現(xiàn)在手指所在的位置的x坐標(biāo)
float x = event.getX();
//判斷觸發(fā)的時(shí)間
switch (action) {
//按下事件
case MotionEvent.ACTION_DOWN:
//初始化或服用加速度測(cè)試器
initOrResetVelocityTracker();
//測(cè)試器添加按下事件
mVelocityTracker.addMovement(event);
//如果停止?jié)L動(dòng)則取消動(dòng)畫(huà)(即手指按下就停止?jié)L動(dòng))
if (!mScroller.isFinished()) {
mScroller.abortAnimation();
}
//獲取現(xiàn)在的x坐標(biāo)
mLastMotionX = event.getX();
break;
//移動(dòng)事件
case MotionEvent.ACTION_MOVE:
//測(cè)試器添加移動(dòng)事件
if (mVelocityTracker != null) {
mVelocityTracker.addMovement(event);
}
//計(jì)算移動(dòng)的偏移量
float delt = mLastMotionX - x;
//重置手指位置
mLastMotionX = x;
//滾動(dòng)
scrollBy((int) delt, 0);
break;
//手指抬起事件
case MotionEvent.ACTION_UP:
//測(cè)試器添加抬起事件
mVelocityTracker.addMovement(event);
//添加加速度的測(cè)試時(shí)間,這里是測(cè)量1000毫秒內(nèi)的加速度
mVelocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
//獲取x方向加速度
float pxsec = mVelocityTracker.getXVelocity();
//得到最后一個(gè)子View
View lastChild = getChildAt(getChildCount() - 1);
//獲取滑動(dòng)的最大滑動(dòng)距離(最后一個(gè)Child的右邊框的坐標(biāo)減去屏幕的寬度)
int finalyChild = (int) (lastChild.getX() + lastChild.getWidth() - screenWidth);
//如果x的加速度大于系統(tǒng)設(shè)定的最小移動(dòng)距離,就可以慣性滑動(dòng)
if (Math.abs(pxsec) > mMinimumVelocity)
mScroller.fling(getScrollX(), 0, (int) -pxsec, 0, 0, finalyChild, 0, 0);
//如果滑動(dòng)的距離小于第一個(gè)控件的最左邊(0)則回彈至(0,0)點(diǎn)
if (getScrollX() < 0) {
scrollTo(0, 0);
}
//如果滑動(dòng)的距離大于最大可滑動(dòng)距離則滑動(dòng)到最后一個(gè)子View
if (getScrollX() >= finalyChild)
scrollTo(finalyChild, 0);
//刷新界面
invalidate();
//清空測(cè)試器
recycleVelocityTracker();
break;
default:
break;
}
return true;
}
/**
* 創(chuàng)建或復(fù)用加速度測(cè)試器
*/
private void initOrResetVelocityTracker() {
if (mVelocityTracker == null) {
mVelocityTracker = VelocityTracker.obtain();
} else {
mVelocityTracker.clear();
}
}
/**
* 回收加速度測(cè)試器,防止內(nèi)存泄漏
*/
private void recycleVelocityTracker() {
if (mVelocityTracker != null) {
mVelocityTracker.recycle();
mVelocityTracker = null;
}
}
@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;
}
/**
* 給子布局設(shè)定位置
*/
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int childLeft = 0;//子View左邊的間距
int childWidth;//子View的寬度
int height = getHeight();//屏幕的寬度
int childCount = getChildCount();//子View的數(shù)量
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í)有所幫助,也希望大家多多支持腳本之家。
- Android繼承ViewGroup實(shí)現(xiàn)Scroll滑動(dòng)效果的方法示例
- Android中實(shí)現(xiàn)可滑動(dòng)的Tab的3種方式
- Android中實(shí)現(xiàn)監(jiān)聽(tīng)ScrollView滑動(dòng)事件
- android中使用Activity實(shí)現(xiàn)監(jiān)聽(tīng)手指上下左右滑動(dòng)
- android 通過(guò)向viewpage中添加listview來(lái)完成滑動(dòng)效果(類似于qq滑動(dòng)界面)
- Android中實(shí)現(xiàn)水平滑動(dòng)(橫向滑動(dòng))ListView示例
- 解析Android中實(shí)現(xiàn)滑動(dòng)翻頁(yè)之ViewFlipper的使用詳解
- android開(kāi)發(fā)教程之實(shí)現(xiàn)滑動(dòng)關(guān)閉fragment示例
- Android利用ViewPager實(shí)現(xiàn)滑動(dòng)廣告板實(shí)例源碼
- Android自定義ViewGroup實(shí)現(xiàn)彈性滑動(dòng)效果
相關(guān)文章
Android基于Toolbar實(shí)現(xiàn)頂部標(biāo)題欄及后退鍵
這篇文章主要介紹了Android基于Toolbar實(shí)現(xiàn)頂部標(biāo)題欄及后退鍵,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09
Android Retrofit2網(wǎng)路編程實(shí)現(xiàn)方法詳解
這篇文章主要介紹了Android Retrofit2網(wǎng)路編程實(shí)現(xiàn)方法詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-12-12
android判斷動(dòng)畫(huà)已結(jié)束示例代碼
添加一個(gè)動(dòng)畫(huà)效果,發(fā)現(xiàn)動(dòng)畫(huà)沒(méi)執(zhí)行完 就直接跳轉(zhuǎn)或者finish掉,添加動(dòng)畫(huà)監(jiān)聽(tīng)事件即可,示例代碼如下2014-10-10
Android編程之簡(jiǎn)單啟動(dòng)畫(huà)面實(shí)現(xiàn)方法
這篇文章主要介紹了Android編程之簡(jiǎn)單啟動(dòng)畫(huà)面實(shí)現(xiàn)方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了開(kāi)機(jī)啟動(dòng)畫(huà)面的制作步驟及布局、Activity跳轉(zhuǎn)、權(quán)限控制等的相關(guān)操作技巧,需要的朋友可以參考下2016-11-11
Android之用PopupWindow實(shí)現(xiàn)彈出菜單的方法詳解
本篇文章是對(duì)在Android中,用PopupWindow實(shí)現(xiàn)彈出菜單的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
解析android中隱藏與顯示軟鍵盤(pán)及不自動(dòng)彈出鍵盤(pán)的實(shí)現(xiàn)方法
本篇文章對(duì)android中隱藏與顯示軟鍵盤(pán)以及不自動(dòng)彈出鍵盤(pán)的方法進(jìn)行了分析介紹。需要的朋友參考下2013-05-05
Android開(kāi)發(fā)實(shí)現(xiàn)從相冊(cè)中選擇照片功能詳解
這篇文章主要介紹了Android開(kāi)發(fā)實(shí)現(xiàn)從相冊(cè)中選擇照片功能,涉及Android權(quán)限控制、事件綁定、文件路徑與獲取等相關(guān)操作技巧,需要的朋友可以參考下2019-03-03
A07_TimePicker & DatePicker & AnalogClock & Digi
本文將帶領(lǐng)大家一起學(xué)習(xí)時(shí)間日期和時(shí)鐘的設(shè)置。A07_TimePicker & DatePicker & AnalogClock & DigitalClock 的設(shè)置,感興趣的朋友可以參考下哈2013-06-06
Kotlin 匿名類實(shí)現(xiàn)接口和抽象類的區(qū)別詳解
這篇文章主要介紹了Kotlin 匿名類實(shí)現(xiàn)接口和抽象類的區(qū)別詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03

