如何在安卓画布中进行 2D 图形过渡



我在android中使用paintcanvas画了一个圆。

我想向某个方向移动圆圈,可以使用任何 API 方法吗?还是我必须使用循环并每次更改位置?

嗨,您可以

参考下面的代码,在 onTouchEvent 中,您可以处理方向并移动对象。

教程2D.java主要活动

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Window;
public class Tutorial2D extends Activity {
    Square drawView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        drawView = new Square(this);
        drawView.setBackgroundColor(Color.WHITE);
        setContentView(drawView);
    }
}

方.java

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.View;
class Square extends View {
    Bitmap mBitmap;
    Paint paint = new Paint();
    public Square(Context context) {
        super(context);
    }
    @Override
    public void onDraw(Canvas canvas) {
        canvas.rotate(direction, mCenterX, mCenterY);
        paint.setColor(Color.BLACK);
        paint.setStrokeWidth(3);
        canvas.drawRect(230, 230, 280, 280, paint);
        paint.setStrokeWidth(0);
        paint.setColor(Color.CYAN);
        canvas.drawRect(233, 260, 277, 277, paint);
        paint.setColor(Color.YELLOW);
        canvas.drawRect(233, 233, 277, 260, paint);
    }
    private float mCenterX, mCenterY;
    private float direction = 0;
    private float sX, sY;
    private float startDirection = 0;
    private void touchStart(float x, float y) {
        mCenterX = this.getWidth() / 2;
        mCenterY = this.getHeight() / 2;
        sX = x;
        sY = y;
    }
    private void touchMove(float x, float y) {
        // this calculate the angle the image rotate
        float angle = (float) angleBetween2Lines(mCenterX, mCenterY, sX, sY, x,
                y);
        direction = (float) Math.toDegrees(angle) * -1 + startDirection;
        this.invalidate();
    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getX();
        float y = event.getY();
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            // record the start position of finger
            touchStart(x, y);
            break;
        case MotionEvent.ACTION_MOVE:
            // update image angle
            touchMove(x, y);
            break;
        case MotionEvent.ACTION_UP:
            startDirection = direction;
            break;
        }
        return true;
    }
    public double angleBetween2Lines(float centerX, float centerY, float x1,
            float y1, float x2, float y2) {
        double angle1 = Math.atan2(y1 - centerY, x1 - centerX);
        double angle2 = Math.atan2(y2 - centerY, x2 - centerX);
        return angle1 - angle2;
    }
}

这很难回答,因为这取决于真正的应用程序是什么。因此,也许最好从较小的组件(如ImageView)构建"图片",然后使用动画来移动ImageView。如果没有,您可能需要重新绘制画布。

最新更新