大佬教程收集整理的这篇文章主要介绍了android – 如何从夹点的中心缩放列表项,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
public class ZoomListView extends ListView { private static final int INVALID_POINTER_ID = -1; privatE int mActivePointerId = INVALID_POINTER_ID; private ScaleGestureDetector mScaleDetector; private float mScaleFactor = 1.f; private float maxWidth = 0.0f; private float maxHeight = 0.0f; private float mLastTouchX; private float mLastTouchY; private float mposx; private float mposy; private float width; private float height; public ZoomListView(Context context) { super(context); mScaleDetector = new ScaleGestureDetector(getContext(),new ScaleListener()); } public ZoomListView(Context context,AttributeSet attrs) { super(context,attrs); mScaleDetector = new ScaleGestureDetector(getContext(),AttributeSet attrs,int defStyleAttr) { super(context,attrs,defStyleAttr); mScaleDetector = new ScaleGestureDetector(getContext(),new ScaleListener()); } @Override protected void onMeasure(int widthMeasureSpec,int heightMeasureSpeC) { width = MeasureSpec.getSize(widthMeasureSpec); height = MeasureSpec.getSize(heightMeasureSpec); super.onMeasure(widthMeasureSpec,heightMeasureSpec); } @Override public Boolean onTouchEvent(@NonNull MotionEvent ev) { super.onTouchEvent(ev); final int action = ev.getAction(); mScaleDetector.onTouchEvent(ev); switch (action & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: { final float x = ev.getX(); final float y = ev.getY(); mLastTouchX = x; mLastTouchY = y; mActivePointerId = ev.getPointerId(0); break; } case MotionEvent.ACTION_MOVE: { final int pointerIndex = ev.findPointerIndex(mActivePointerId); final float x = ev.getX(pointerIndeX); final float y = ev.getY(pointerIndeX); final float dx = x - mLastTouchX; final float dy = y - mLastTouchY; mposx += dx; mposy += dy; if (mposx > 0.0f) mposx = 0.0f; else if (mposx < maxWidth) mposx = maxWidth; if (mposy > 0.0f) mposy = 0.0f; else if (mposy < maxHeight) mposy = maxHeight; mLastTouchX = x; mLastTouchY = y; invalidate(); break; } case MotionEvent.ACTION_UP: { mActivePointerId = INVALID_POINTER_ID; break; } case MotionEvent.ACTION_CANCEL: { mActivePointerId = INVALID_POINTER_ID; break; } case MotionEvent.ACTION_POINTER_UP: { final int pointerIndex = (action & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT; final int pointerId = ev.getPointerId(pointerIndeX); if (pointerId == mActivePointerId) { final int newPointerIndex = pointerIndex == 0 ? 1 : 0; mLastTouchX = ev.getX(newPointerIndeX); mLastTouchY = ev.getY(newPointerIndeX); mActivePointerId = ev.getPointerId(newPointerIndeX); } break; } } return true; } @Override public void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.save(Canvas.MATRIX_SAVE_FLAG); canvas.translate(mposx,mposy); canvas.scale(mScaleFactor,mScaleFactor); canvas.restore(); } @Override protected void dispatchDraw(@NonNull Canvas canvas) { canvas.save(Canvas.MATRIX_SAVE_FLAG); if (mScaleFactor == 1.0f) { mposx = 0.0f; mposy = 0.0f; } canvas.translate(mposx,mScaleFactor); super.dispatchDraw(canvas); canvas.restore(); invalidate(); } private class ScaleListener extends ScaleGestureDetector.SimpLeonScaleGestureListener { @Override public Boolean onScale(ScaleGestureDetector detector) { mScaleFactor *= detector.getScaleFactor(); mScaleFactor = Math.max(1.0f,Math.min(mScaleFactor,4.0f)); maxWidth = width - (width * mScaleFactor); maxHeight = height - (height * mScaleFactor); invalidate(); return true; } } }@H_675_2@这是我的XML布局(我已经只显示了ZoomListView):
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://scheR_175_11845@as.android.com/apk/res/android" xmlns:tools="http://scheR_175_11845@as.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:BACkground="#ffffff" android:gravity="top" android:visibility="visible" > <libraries.ZoomListView android:id="@+id/lstv_book_reader" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="0dp" android:paddingTop="0dp" > </libraries.ZoomListView> </RelativeLayout> </RelativeLayout>@H_675_2@这使我能够放大我的书籍阅读器.我的书籍读者看起来像 @H_675_2@ @H_675_2@问题:
_____________ |0,0 x | |y | | | Suppose this the device screen | | | | ______________@H_675_2@而且你想将它从
_____________ | | | | | | Suppose this the device screen | y | | X 0,0| ______________@H_675_2@所以要实现这个:
canvas.scale(mScaleFactor,mScaleFactor);@H_675_2@到这个:
canvas.scale(mScaleFactor,-mScaleFactor);@H_675_2@也看到这个与你的问题有关的问题:
以上是大佬教程为你收集整理的android – 如何从夹点的中心缩放列表项全部内容,希望文章能够帮你解决android – 如何从夹点的中心缩放列表项所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。