Android,沿路径移动位图?

发布于 2024-10-07 22:11:34 字数 127 浏览 5 评论 0原文

我想知道是否可以从路径中选择坐标来随着时间的推移绘制位图,例如,我有一张太阳的图像,我想随着时间的推移沿着弧形路径移动它。

有没有什么方法可以定义这样的路径,然后沿着它移动,这样我就不必进行数学计算?

谢谢。

I would like to know if it's possible to select coordinates from a path to draw a bitmap over time, for example, I have an image of a sun, and I would like to move it, over time, along an arc path.

Is there some way to define a path like this and then move along it, so that I don't have to calculate it mathematically?

Thanks.

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

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

发布评论

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

评论(3

静若繁花 2024-10-14 22:11:34

是的,可以沿着路径移动图像。我将提供简单的解决方案来展示原理。以下代码将移动和旋转您的图像。如果不需要旋转,请删除 TANGENT_MATRIX_FLAG 标志。

import android.graphics.*;
//somewhere global
int iCurStep = 0;// current step

//don't forget to initialize
Path pathMoveAlong = new Path();
private static Bitmap bmImage = null;

@Override
protected void onDraw(Canvas canvas) {
    Matrix mxTransform = new Matrix();
    PathMeasure pm = new PathMeasure(pathMoveAlong, false);
    float fSegmentLen = pm.getLength() / 20;//20 animation steps

    if (iCurStep <= 20) {
        pm.getMatrix(fSegmentLen * iCurStep, mxTransform,
            PathMeasure.POSITION_MATRIX_FLAG + PathMeasure.TANGENT_MATRIX_FLAG);
        canvas.drawBitmap(bmImage, mxTransform, null);
        iCurStep++;
        invalidate();
    } else {
        iCurStep = 0;
    };
};

Yes, it's possible to move image along path. I will provide simple solution to show the principle. The following code will move and rotate your image. If you don't need rotation remove the TANGENT_MATRIX_FLAG flag.

import android.graphics.*;
//somewhere global
int iCurStep = 0;// current step

//don't forget to initialize
Path pathMoveAlong = new Path();
private static Bitmap bmImage = null;

@Override
protected void onDraw(Canvas canvas) {
    Matrix mxTransform = new Matrix();
    PathMeasure pm = new PathMeasure(pathMoveAlong, false);
    float fSegmentLen = pm.getLength() / 20;//20 animation steps

    if (iCurStep <= 20) {
        pm.getMatrix(fSegmentLen * iCurStep, mxTransform,
            PathMeasure.POSITION_MATRIX_FLAG + PathMeasure.TANGENT_MATRIX_FLAG);
        canvas.drawBitmap(bmImage, mxTransform, null);
        iCurStep++;
        invalidate();
    } else {
        iCurStep = 0;
    };
};
遗弃M 2024-10-14 22:11:34

以下是我使用的动画师:

目的:沿路径“路径”移动视图“视图”

v21+:

ValueAnimator pathAnimator = ObjectAnimator.ofFloat(view, "x", "y", path)

v11+:

ValueAnimator pathAnimator = ValueAnimator.ofFloat(0.0f, 1.0f);

pathAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
float[] point = new float[2];

@Override
    public void onAnimationUpdate(ValueAnimator animation) {
        float val = animation.getAnimatedFraction();
        PathMeasure pathMeasure = new PathMeasure(path, true);
        pathMeasure.getPosTan(pathMeasure.getLength() * val, point, null);
        view.setX(point[0]);
        view.setY(point[1]);
    }
});

类似要求: https:// /stackoverflow.com/a/30254715/4344057

Here are the animators I use:

Purpose: Move View "view" along Path "path"

v21+:

ValueAnimator pathAnimator = ObjectAnimator.ofFloat(view, "x", "y", path)

v11+:

ValueAnimator pathAnimator = ValueAnimator.ofFloat(0.0f, 1.0f);

pathAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
float[] point = new float[2];

@Override
    public void onAnimationUpdate(ValueAnimator animation) {
        float val = animation.getAnimatedFraction();
        PathMeasure pathMeasure = new PathMeasure(path, true);
        pathMeasure.getPosTan(pathMeasure.getLength() * val, point, null);
        view.setX(point[0]);
        view.setY(point[1]);
    }
});

Similar requirement to: https://stackoverflow.com/a/30254715/4344057

一身仙ぐ女味 2024-10-14 22:11:34

我正在考虑一种解决方案:

<前><代码>_
/\

如果您的弧具有这种形状(意味着小于半个圆并且水平放置),那么您可以迭代 x 值,并为此 x 获取路径上的 y。然后将位图移动到该位置。

I'm thinking of one solution:

         _
       /   \

If your arc has this kind of shape (meaning less than half a circle and positioned horizontaly) then you could iterate the x value and for that x get the y that's on the path. Then move your bitmap to that position.

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