android – 如何从夹点的中心缩放列表项

android – 如何从夹点的中心缩放列表项,第1张

概述我正在为 Android的简单的书籍阅读器应用程序工作.我已经使用列表视图为此. 我正在做的是将远程图像从服务器加载到我的Listview(使用Picasso库),并且正常工作. 我也想在我的阅读器中缩放功能,我用ZoomListView类来实现这一点. public class ZoomListView extends ListView {private static final int IN 我正在为 Android的简单的书籍阅读器应用程序工作.我已经使用列表视图为此.

我正在做的是将远程图像从服务器加载到我的ListvIEw(使用Picasso库),并且正常工作.

我也想在我的阅读器中缩放功能,我用ZoomListVIEw类来实现这一点.

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());}@OverrIDeprotected voID onMeasure(int wIDthMeasureSpec,int heightmeasureSpec) {    wIDth = MeasureSpec.getSize(wIDthMeasureSpec);    height = MeasureSpec.getSize(heightmeasureSpec);    super.onMeasure(wIDthMeasureSpec,heightmeasureSpec);}@OverrIDepublic 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;}@OverrIDepublic voID onDraw(Canvas canvas) {    super.onDraw(canvas);    canvas.save(Canvas.MATRIX_SAVE_FLAG);    canvas.translate(mPosX,mPosY);    canvas.scale(mScaleFactor,mScaleFactor);    canvas.restore();}@OverrIDeprotected 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;    }}}

这是我的XML布局(我已经只显示了ZoomListVIEw):

<?xml version="1.0" enCoding="utf-8"?><relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"xmlns:tools="http://schemas.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>

这使我能够放大我的书籍阅读器.我的书籍读者看起来像

问题:
我在捏捏缩放中遇到问题.当我捏捏缩放从列表视图的左上角开始.

让我用图像描述

1捏

2电流输出

注意:这放大了从topleft角落的列表

3推荐输出

解决方法 这意味着您要更改缩放(缩放)的默认属性始终从(0,0)开始,即:
_____________|0,0 x       ||y           ||            |   Suppose this the device screen|            ||            |______________

而且你想将它从

_____________|            ||            ||            |   Suppose this the device screen|          y ||       X 0,0|______________

所以要实现这个:
从此更改您的代码:

canvas.scale(mScaleFactor,mScaleFactor);

到这个:

canvas.scale(mScaleFactor,-mScaleFactor);

也看到这个与你的问题有关的问题:
Android: Drawing to canvas,way to make bottom left correspond to (0,0)?我有我从这个例子和代码片段中得到你的解决方案.

总结

以上是内存溢出为你收集整理的android – 如何从夹点的中心缩放列表项全部内容,希望文章能够帮你解决android – 如何从夹点的中心缩放列表项所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1133984.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存