如何在Android Studio中为实心三角形(由路径绘制)设置动画

时间:2019-01-27 15:40:03

标签: android-studio animation path polygon

我正在尝试为Android构建类似于拼图的应用程序,其中涉及用户通过touch事件在屏幕上移动多边形块。

例如,我可以绘制一个三角形并填充它。我还可以在屏幕上平滑移动它,但是留下痕迹-这样屏幕就可以快速填充:-( 所以我想知道如何调整onDraw方法,以便不包括三角形的先前位置。还是还有其他技术?这个问题以前只被问过一次,但没有得到满意的答案。我对Android的工作还很陌生,所以我相信一位好心的专家将能够为我指明正确的方向!

视图:

公共类GameView扩展了View {

public Triangle T1;
Paint paint = new Paint();
private Path path;

public GameView(Context context) {
    super(context);
    paint.setStyle(Paint.Style.FILL);
    paint.setColor(Color.GREEN);
    initTriangle();
}

public void initTriangle() {
    int T1Points[][] = new int[3][2];

    T1Points[0][0] = -200;
    T1Points[0][1] = -100;
    T1Points[1][0] = 200;
    T1Points[1][1] = -100;
    T1Points[2][0] = 0;
    T1Points[2][1] = 100;

    float[] position = new float[2];
    position[0] = (float) 200.0;
    position[1] = (float) 100.0;

    T1 = new Triangle("T1", T1Points,  position);

    path = T1.getPath();
}

@Override
public boolean onTouchEvent(MotionEvent event) {

    float[] pos = new float[2];
    pos[0] = event.getX(); //nextx;
    pos[1] = event.getY(); //nexty;
    T1.setPosition(pos );
    Path path = new Path();
    path = T1.getPath();
    postInvalidate();
    return true;
}

protected void onDraw(Canvas canvas) {
    canvas.drawPath(path, paint);
}

}

三角形类:

公共类三角形{

public int[][] myPoints;
public Path path;
public String myname;
public float[] position;

public Triangle (String name, int[][] newpoints,  float[] posn) {

    myPoints = new int[4][2];
    int i;
    for (i = 0; i < 3; i++) {
        myPoints[i][0] = newpoints[i][0];
        myPoints[i][1] = newpoints[i][1];
    }
    myPoints[3][0] = newpoints[0][0];  // closed circuit for future needs
    myPoints[3][1] = newpoints[0][1];

    path = new Path();
    position = new float[2];
    position[0] =  posn[0];
    position[1] =  posn[1];
    myname = name;
    updatePath();
}

public void setPosition(float[] newPosition){

    position[0] = newPosition[0];
    position[1] = newPosition[1];
    updatePath();
}

public void updatePath(){

    int startx = myPoints[0][0] + Math.round(position[0]);
    int starty = myPoints[0][1] + Math.round(position[1]);

    path.moveTo(startx,starty);
    for (int i = 1; i < myPoints.length; i++)
    {
        int newx = myPoints[i][0] + Math.round(position[0]);
        int newy = myPoints[i][1] + Math.round(position[1]);

        path.lineTo(newx,newy);
    }
    path.close();
}

public Path getPath()   {
    return path;
}

}

主要活动:

公共类MainActivity扩展了AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    uk.mydomain.animatedtriangle.GameView gameView = new uk.mydomain.animatedtriangle.GameView(this);
    RelativeLayout relativeLayout = new RelativeLayout(this);
    RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT,
            RelativeLayout.LayoutParams.MATCH_PARENT);
    relativeLayout.setLayoutParams(relativeParams);
    setContentView(relativeLayout);

    relativeLayout.addView(gameView);
}

1 个答案:

答案 0 :(得分:0)

排序!只需在onDraw的末尾添加path.reset()。