【问题标题】:How to move Aeroplane picture in circle in android programmatically?如何以编程方式在android中移动飞机图片?
【发布时间】:2018-01-20 10:17:23
【问题描述】:

我知道如何在使用此源代码时在 android 中移动图像Move image in circle 这段代码在圆形移动图像视图中非常完美,但我想将飞机图片移动成圆形,我希望它看起来像飞机的真实运动。谁能帮帮我

【问题讨论】:

  • move the imageview in circlemove aeroplane picture in circle 有什么区别? real motion of airplane 到底长什么样?不清楚。
  • move the imageview in circle and move aeroplane picture in circle 之间到底有什么区别:/
  • 你可以应用圆形动画,如果你的真实动作是指一些特殊效果,那么你可以使用gif图像
  • @UsmanRana 先生,我有一张飞机的照片,我可以把它移动成圆形,但问题是它不遵循圆形的形状,你知道它应该像真正的飞机一样移动圆圈,但这里只有飞机在圆圈中移动的图像,它看起来不像真正的圆圈运动
  • 这样的话飞机在做圆周运动时也应该转弯,对吧?但是你有 2D 图像而不是像统一游戏那样的 3D 模型。为了更好地查看,您可以使用 svg 或 gif 图片。

标签: android animation imageview


【解决方案1】:
public class AnimationView extends View {

    Paint paint;
    Bitmap bm;
    int bm_offsetX, bm_offsetY;
    Path animPath;
    PathMeasure pathMeasure;
    float pathLength;
    float step;   //distance each step
    float distance;  //distance moved
    float[] pos;
    float[] tan;
    Matrix matrix;

    public AnimationView(Context context) {
        super(context);
        initMyView();
    }

    public AnimationView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initMyView();
    }

    public AnimationView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initMyView();
    }

    public void initMyView(){
        paint = new Paint();
        paint.setColor(Color.BLUE);
        paint.setStrokeWidth(4);
        paint.setStyle(Paint.Style.STROKE);
        DashPathEffect dashPath = new DashPathEffect(new float[]{6,6}, (float)2.0);

        paint.setPathEffect(dashPath);

        bm = BitmapFactory.decodeResource(getResources(), R.drawable.airplane);
        bm_offsetX = bm.getWidth()/2;
        bm_offsetY = bm.getHeight()/2;

        animPath = new Path();
        animPath.addCircle(600, 600, 500, Path.Direction.CW);
        animPath.close();

        pathMeasure = new PathMeasure(animPath, false);
        pathLength = pathMeasure.getLength();

        step = 1;
        distance = 0;
        pos = new float[2];
        tan = new float[2];

        matrix = new Matrix();
    }

    @Override
    protected void onDraw(Canvas canvas) {

        canvas.drawPath(animPath, paint);

        if(distance < pathLength){
            pathMeasure.getPosTan(distance, pos, tan);

            matrix.reset();
            float degrees = (float)(Math.atan2(tan[0], tan[1])*0.0/Math.PI);
            matrix.postRotate(degrees, bm_offsetX, bm_offsetY);
            matrix.postTranslate(pos[0]-bm_offsetX, pos[1]-bm_offsetY);

            canvas.drawBitmap(bm, matrix, null);

            distance += step;
        }else{
            distance = 0;
        }

        invalidate();
    }

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-08
相关资源
最近更新 更多