一指变焦 +在 Android 中旋转

发布于 2024-10-20 18:41:30 字数 5311 浏览 0 评论 0 原文

我正在根据教程在android中开发缩放控件: Android 单指缩放教程

public abstract class Dynamics {
/**
 * The maximum delta time, in milliseconds, between two updates
 */
private static final int MAX_TIMESTEP = 50;

/** The current position */
protected float mPosition;

/** The current velocity */
protected float mVelocity;

/** The current maximum position */
protected float mMaxPosition = Float.MAX_VALUE;

/** The current minimum position */
protected float mMinPosition = -Float.MAX_VALUE;

/** The time of the last update */
protected long mLastTime = 0;

/**
 * Sets the state of the dynamics object. Should be called before starting
 * to call update.
 * 
 * @param position The current position.
 * @param velocity The current velocity in pixels per second.
 * @param now The current time
 */
public void setState(final float position, final float velocity, final long now) {
    mVelocity = velocity;
    mPosition = position;
    mLastTime = now;
}

/**
 * Returns the current position. Normally used after a call to update() in
 * order to get the updated position.
 * 
 * @return The current position
 */
public float getPosition() {
    return mPosition;
}

/**
 * Gets the velocity. Unit is in pixels per second.
 * 
 * @return The velocity in pixels per second
 */
public float getVelocity() {
    return mVelocity;
}

/**
 * Used to find out if the list is at rest, that is, has no velocity and is
 * inside the the limits. Normally used to know if more calls to update are
 * needed.
 * 
 * @param velocityTolerance Velocity is regarded as 0 if less than
 *            velocityTolerance
 * @param positionTolerance Position is regarded as inside the limits even
 *            if positionTolerance above or below
 * 
 * @return true if list is at rest, false otherwise
 */
public boolean isAtRest(final float velocityTolerance, final float positionTolerance) {
    final boolean standingStill = Math.abs(mVelocity) < velocityTolerance;
    final boolean withinLimits = mPosition - positionTolerance < mMaxPosition
            && mPosition + positionTolerance > mMinPosition;
    return standingStill && withinLimits;
}

/**
 * Sets the maximum position.
 * 
 * @param maxPosition The maximum value of the position
 */
public void setMaxPosition(final float maxPosition) {
    mMaxPosition = maxPosition;
}

/**
 * Sets the minimum position.
 * 
 * @param minPosition The minimum value of the position
 */
public void setMinPosition(final float minPosition) {
    mMinPosition = minPosition;
}

/**
 * Updates the position and velocity.
 * 
 * @param now The current time
 */
public void update(final long now) {
    int dt = (int)(now - mLastTime);
    if (dt > MAX_TIMESTEP) {
        dt = MAX_TIMESTEP;
    }

    onUpdate(dt);

    mLastTime = now;
}

/**
 * Gets the distance to the closest limit (max and min position).
 * 
 * @return If position is more than max position: distance to max position. If
 *         position is less than min position: distance to min position. If
 *         within limits: 0
 */
protected float getDistanceToLimit() {
    float distanceToLimit = 0;

    if (mPosition > mMaxPosition) {
        distanceToLimit = mMaxPosition - mPosition;
    } else if (mPosition < mMinPosition) {
        distanceToLimit = mMinPosition - mPosition;
    }

    return distanceToLimit;
}

/**
 * Updates the position and velocity.
 * 
 * @param dt The delta time since last time
 */
abstract protected void onUpdate(int dt);
}

public class SpringDynamics extends Dynamics {

/** Friction factor */
private float mFriction;

/** Spring stiffness factor */
private float mStiffness;

/** Spring damping */
private float mDamping;

/**
 * Set friction parameter, friction physics are applied when inside of snap
 * bounds.
 * 
 * @param friction Friction factor
 */
public void setFriction(float friction) {
    mFriction = friction;
}

/**
 * Set spring parameters, spring physics are applied when outside of snap
 * bounds.
 * 
 * @param stiffness Spring stiffness
 * @param dampingRatio Damping ratio, < 1 underdamped, > 1 overdamped
 */
public void setSpring(float stiffness, float dampingRatio) {
    mStiffness = stiffness;
    mDamping = dampingRatio * 2 * (float)Math.sqrt(stiffness);
}

/**
 * Calculate acceleration at the current state
 * 
 * @return Current acceleration
 */
private float calculateAcceleration() {
    float acceleration;

    final float distanceFromLimit = getDistanceToLimit();
    if (distanceFromLimit != 0) {
        acceleration = distanceFromLimit * mStiffness - mDamping * mVelocity;
    } else {
        acceleration = -mFriction * mVelocity;
    }

    return acceleration;
}

@Override
protected void onUpdate(int dt) {
    // Calculate dt in seconds as float
    final float fdt = dt / 1000f;

    // Calculate current acceleration
    final float a = calculateAcceleration();

    // Calculate next position based on current velocity and acceleration
    mPosition += mVelocity * fdt + .5f * a * fdt * fdt;

    // Update velocity
    mVelocity += a * fdt;
}

}

有人能给我一些关于如何将图像旋转也融入其中的想法吗(类似于 iPhone 的捏合)?

谢谢

I was developing a zoom control in android based on the tutorial: Android one finger zoom tutorial

public abstract class Dynamics {
/**
 * The maximum delta time, in milliseconds, between two updates
 */
private static final int MAX_TIMESTEP = 50;

/** The current position */
protected float mPosition;

/** The current velocity */
protected float mVelocity;

/** The current maximum position */
protected float mMaxPosition = Float.MAX_VALUE;

/** The current minimum position */
protected float mMinPosition = -Float.MAX_VALUE;

/** The time of the last update */
protected long mLastTime = 0;

/**
 * Sets the state of the dynamics object. Should be called before starting
 * to call update.
 * 
 * @param position The current position.
 * @param velocity The current velocity in pixels per second.
 * @param now The current time
 */
public void setState(final float position, final float velocity, final long now) {
    mVelocity = velocity;
    mPosition = position;
    mLastTime = now;
}

/**
 * Returns the current position. Normally used after a call to update() in
 * order to get the updated position.
 * 
 * @return The current position
 */
public float getPosition() {
    return mPosition;
}

/**
 * Gets the velocity. Unit is in pixels per second.
 * 
 * @return The velocity in pixels per second
 */
public float getVelocity() {
    return mVelocity;
}

/**
 * Used to find out if the list is at rest, that is, has no velocity and is
 * inside the the limits. Normally used to know if more calls to update are
 * needed.
 * 
 * @param velocityTolerance Velocity is regarded as 0 if less than
 *            velocityTolerance
 * @param positionTolerance Position is regarded as inside the limits even
 *            if positionTolerance above or below
 * 
 * @return true if list is at rest, false otherwise
 */
public boolean isAtRest(final float velocityTolerance, final float positionTolerance) {
    final boolean standingStill = Math.abs(mVelocity) < velocityTolerance;
    final boolean withinLimits = mPosition - positionTolerance < mMaxPosition
            && mPosition + positionTolerance > mMinPosition;
    return standingStill && withinLimits;
}

/**
 * Sets the maximum position.
 * 
 * @param maxPosition The maximum value of the position
 */
public void setMaxPosition(final float maxPosition) {
    mMaxPosition = maxPosition;
}

/**
 * Sets the minimum position.
 * 
 * @param minPosition The minimum value of the position
 */
public void setMinPosition(final float minPosition) {
    mMinPosition = minPosition;
}

/**
 * Updates the position and velocity.
 * 
 * @param now The current time
 */
public void update(final long now) {
    int dt = (int)(now - mLastTime);
    if (dt > MAX_TIMESTEP) {
        dt = MAX_TIMESTEP;
    }

    onUpdate(dt);

    mLastTime = now;
}

/**
 * Gets the distance to the closest limit (max and min position).
 * 
 * @return If position is more than max position: distance to max position. If
 *         position is less than min position: distance to min position. If
 *         within limits: 0
 */
protected float getDistanceToLimit() {
    float distanceToLimit = 0;

    if (mPosition > mMaxPosition) {
        distanceToLimit = mMaxPosition - mPosition;
    } else if (mPosition < mMinPosition) {
        distanceToLimit = mMinPosition - mPosition;
    }

    return distanceToLimit;
}

/**
 * Updates the position and velocity.
 * 
 * @param dt The delta time since last time
 */
abstract protected void onUpdate(int dt);
}

public class SpringDynamics extends Dynamics {

/** Friction factor */
private float mFriction;

/** Spring stiffness factor */
private float mStiffness;

/** Spring damping */
private float mDamping;

/**
 * Set friction parameter, friction physics are applied when inside of snap
 * bounds.
 * 
 * @param friction Friction factor
 */
public void setFriction(float friction) {
    mFriction = friction;
}

/**
 * Set spring parameters, spring physics are applied when outside of snap
 * bounds.
 * 
 * @param stiffness Spring stiffness
 * @param dampingRatio Damping ratio, < 1 underdamped, > 1 overdamped
 */
public void setSpring(float stiffness, float dampingRatio) {
    mStiffness = stiffness;
    mDamping = dampingRatio * 2 * (float)Math.sqrt(stiffness);
}

/**
 * Calculate acceleration at the current state
 * 
 * @return Current acceleration
 */
private float calculateAcceleration() {
    float acceleration;

    final float distanceFromLimit = getDistanceToLimit();
    if (distanceFromLimit != 0) {
        acceleration = distanceFromLimit * mStiffness - mDamping * mVelocity;
    } else {
        acceleration = -mFriction * mVelocity;
    }

    return acceleration;
}

@Override
protected void onUpdate(int dt) {
    // Calculate dt in seconds as float
    final float fdt = dt / 1000f;

    // Calculate current acceleration
    final float a = calculateAcceleration();

    // Calculate next position based on current velocity and acceleration
    mPosition += mVelocity * fdt + .5f * a * fdt * fdt;

    // Update velocity
    mVelocity += a * fdt;
}

}

Can someone give me some idea as to how to incorporate rotation of image into this as well (similar to the iphone pinch)?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

丑丑阿 2024-10-27 18:41:30

要旋转图像,请参阅 ImageView#setImageMatrix( )Matrix 类。使用Matrix,您可以对图像执行多种类型的变换,包括旋转。

To rotate image, see ImageView#setImageMatrix() and the Matrix class. Using Matrix, you can perform several types of transformations against the image, including rotation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文