Java 在活动中使用外部库中的变量
我试图在活动中使用外部库中的变量“mCurrentPage”。此库使用FrameLayout中的视图,并将其显示为可以滑动的书页。我想做的是,如果FrameLayout的X视图在前面,则执行一些操作。(请不要使用rowntofront()) 我尝试过使用getElevation(),但它需要API>21 实际上,该代码中的变量“mCurrentPage”完成了这项工作。但是我也需要在其他活动中使用它(或模仿它),我只是不知道这个变量是如何工作的,它的值是从哪里来的,以便告诉前面的当前视图是什么Java 在活动中使用外部库中的变量,java,android,xml,Java,Android,Xml,我试图在活动中使用外部库中的变量“mCurrentPage”。此库使用FrameLayout中的视图,并将其显示为可以滑动的书页。我想做的是,如果FrameLayout的X视图在前面,则执行一些操作。(请不要使用rowntofront()) 我尝试过使用getElevation(),但它需要API>21 实际上,该代码中的变量“mCurrentPage”完成了这项工作。但是我也需要在其他活动中使用它(或模仿它),我只是不知道这个变量是如何工作的,它的值是从哪里来的,以便告诉前面的当前视图是什么
public class PageTurnLayout extends FrameLayout {
private Point mLastTouchPoint;
private Rect mTopViewRect;
private Rect mBottomViewRect;
private Paint mPaint;
private int mCurrentPage;
private int mPageTouchSlop;
private boolean mIsTurning;
private PageTurnDirection mDirection;
private float mFirstX;
private Handler mHandler = new Handler();
public PageTurnLayout(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public PageTurnLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public PageTurnLayout(Context context) {
super(context);
init();
}
private void init() {
setWillNotDraw(false);
mPaint = new Paint();
mBottomViewRect = new Rect();
mTopViewRect = new Rect();
mPageTouchSlop = (int) getResources().getDimension(R.dimen.touch_start_padding);
}
protected boolean isTouchAPageTurnStart(MotionEvent ev) {
if (ev.getAction() != MotionEvent.ACTION_DOWN)
return false;
return isTouchNearEdge(ev);
}
protected boolean isTouchNearEdge(MotionEvent ev) {
if (Math.abs(ev.getX() - getMeasuredWidth()) < mPageTouchSlop)
return true;
else if (ev.getX() < mPageTouchSlop)
return true;
return false;
}
protected PageTurnDirection getPageTurnDirection(MotionEvent ev) {
if(mFirstX - ev.getX() == 0.0f)
return null;
PageTurnDirection direction = mFirstX - ev.getX() > 0 ? PageTurnDirection.LEFT : PageTurnDirection.RIGHT;
return direction;
}
protected boolean shouldTurn() {
if(mDirection == null)
return false;
if(mDirection == PageTurnDirection.LEFT && mCurrentPage == getChildCount() - 1)
return false;
else if(mDirection == PageTurnDirection.RIGHT && mCurrentPage == 0)
return false;
return true;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return true;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN && !mIsTurning) {
mIsTurning = isTouchAPageTurnStart(event);
if (!mIsTurning) {
return false;
} else {
invalidate();
mLastTouchPoint = new Point((int) event.getX(), (int) event.getY());
mFirstX = event.getX();
return true;
}
} else if (event.getAction() == MotionEvent.ACTION_MOVE && mIsTurning) {
if(mDirection == null) {
//get the page turn direction
mDirection = getPageTurnDirection(event);
//if we shouldn't turn then abort everything and reset it
if(!shouldTurn()) {
mDirection = null;
mIsTurning = false;
return false;
}
}
mLastTouchPoint = new Point((int) event.getX(), (int) event.getY());
invalidate();
} else if (event.getAction() == MotionEvent.ACTION_UP && mIsTurning) {
int halfWidth = getMeasuredWidth() / 2;
if (mLastTouchPoint.x > halfWidth) {
final Runnable animationRunnable = new Runnable() {
public void run() {
mLastTouchPoint.x += 20;
invalidate();
if (mLastTouchPoint.x < getMeasuredWidth())
mHandler.post(this);
else {
mIsTurning = false;
if (mDirection == PageTurnDirection.RIGHT)
mCurrentPage--;
mDirection = null;
}
}
};
mHandler.post(animationRunnable);
} else {
final Runnable animationRunnable = new Runnable() {
public void run() {
mLastTouchPoint.x -= 20;
invalidate();
if (mLastTouchPoint.x > -(getMeasuredWidth() / 2)) {
mHandler.post(this);
} else {
mIsTurning = false;
if (mDirection == PageTurnDirection.LEFT)
mCurrentPage++;
mDirection = null;
}
}
};
mHandler.post(animationRunnable);
}
}
return true;
}
公共类PageTurnLayout扩展了FrameLayout{
专用点和接触点;
私有矩形mTopViewRect;
私有矩形MBOTOMVIEWRECT;
私人油漆;
私有int mCurrentPage;
姆帕格图什洛普私人酒店;
私有布尔错误转换;
私有页面转向;
私人浮动mFirstX;
私有处理程序mHandler=新处理程序();
公共页面布局(上下文、属性集属性){
超级(上下文,attrs);
init();
}
公共页面布局(上下文、属性集属性、int-defStyle){
超级(上下文、属性、定义样式);
init();
}
公共页面布局(上下文){
超级(上下文);
init();
}
私有void init(){
setWillNotDraw(假);
mPaint=新油漆();
mBottomViewRect=新的Rect();
mTopViewRect=新的Rect();
mpagetouchlop=(int)getResources().getDimension(R.dimen.touch\u start\u padding);
}
受保护的布尔值isTouchAPageTurnStart(运动事件ev){
if(ev.getAction()!=MotionEvent.ACTION\u向下)
返回false;
返回近边缘(ev);
}
受保护的布尔isTouchNearEdge(运动事件ev){
if(Math.abs(ev.getX()-getMeasuredWidth())0?PageTurnDirection.LEFT:PageTurnDirection.RIGHT;
返回方向;
}
受保护的布尔值shouldTurn(){
if(mDirection==null)
返回false;
如果(mDirection==PageTurnDirection.LEFT&&mCurrentPage==getChildCount()-1)
返回false;
else if(mDirection==PageTurnDirection.RIGHT&&mCurrentPage==0)
返回false;
返回true;
}
@凌驾
公共布尔值onInterceptTouchEvent(MotionEvent ev){
返回true;
}
@凌驾
公共布尔onTouchEvent(运动事件){
if(event.getAction()==MotionEvent.ACTION\u向下旋转&&!错误旋转){
错误转动=isTouchAPageTurnStart(事件);
如果(!旋转错误){
返回false;
}否则{
使无效();
mLastTouchPoint=新点((int)event.getX(),(int)event.getY());
mFirstX=event.getX();
返回true;
}
}else if(event.getAction()==MotionEvent.ACTION\u移动和错误转动(&M){
if(mDirection==null){
//获取翻页方向
mDirection=getPageTurnDirection(事件);
//如果我们不该转向,则中止所有操作并重置它
如果(!shouldTurn()){
mDirection=null;
错误转动=错误;
返回false;
}
}
mLastTouchPoint=新点((int)event.getX(),(int)event.getY());
使无效();
}else if(event.getAction()==MotionEvent.ACTION\u向上旋转和旋转错误(&M){
int halfWidth=getMeasuredWidth()/2;
如果(mLastTouchPoint.x>半宽){
final Runnable animationRunnable=new Runnable(){
公开募捐{
mLastTouchPoint.x+=20;
使无效();
if(mLastTouchPoint.x-(getMeasuredWidth()/2)){
mHandler.post(本);
}否则{
错误转动=错误;
如果(mDirection==PageTurnDirection.LEFT)
mCurrentPage++;
mDirection=null;
}
}
};
mHandler.post(animationRunnable);
}
}
返回true;
}
将mCurrentPage
的类修改为公共类,或者为其创建一个getter。或者使用反射来获取私有字段值。谢谢,我创建了一个getter,但是在我的活动PageTurnLayout frame=new PageTurnLayout(此)中出现空指针异常
我想我需要一个上下文,但我真的不知道要在其中放什么。它与公共页面布局(上下文){super(上下文);init();}
`NULL指针异常`显示堆栈跟踪有关