android编程图像移动,在android中以圆形路径移动图像

我认为你有两个选择:要么你创建一个自定义动画,要么你创建你的ImageView然后使用表面自己沿着路径绘制它.

第一个选项更容易,并且可能会给出更好的结果,因为在动画类中,使用插值器(线性时间,快速启动,结束正常等)处理时序.我强烈建议你编写自定义动画,因为我不明白为什么你不想使用Animation类(动画图像正是你想要的).

编辑:我花了一些时间并实现了以下内容.它不是一尘不染,而是在圆形路径中为图像设置动画.

活动:

public class MainActivity extends Activity {

private ImageView image;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

image = (ImageView) findViewById(R.id.image);

image.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

Animation anim = new MyAnimation(image, 100);

anim.setDuration(3000);

image.startAnimation(anim);

}

});

}

}

动画类:

public class MyAnimation extends Animation {

private View view;

private float cx, cy; // center x,y position of circular path

private float prevX, prevY; // previous x,y position of image during animation

private float r; // radius of circle

private float prevDx, prevDy;

/**

* @param view - View that will be animated

* @param r - radius of circular path

*/

public MyAnimation(View view, float r){

this.view = view;

this.r = r;

}

@Override

public boolean willChangeBounds() {

return true;

}

@Override

public void initialize(int width, int height, int parentWidth, int parentHeight) {

// calculate position of image center

int cxImage = width / 2;

int cyImage = height / 2;

cx = view.getLeft() + cxImage;

cy = view.getTop() + cyImage;

// set previous position to center

prevX = cx;

prevY = cy;

}

@Override

protected void applyTransformation(float interpolatedTime, Transformation t) {

if(interpolatedTime == 0){

t.getMatrix().setTranslate(prevDx, prevDy);

return;

}

float angleDeg = (interpolatedTime * 360f + 90) % 360;

float angleRad = (float) Math.toRadians(angleDeg);

// r = radius, cx and cy = center point, a = angle (radians)

float x = (float) (cx + r * Math.cos(angleRad));

float y = (float) (cy + r * Math.sin(angleRad));

float dx = prevX - x;

float dy = prevY - y;

prevX = x;

prevY = y;

prevDx = dx;

prevDy = dy;

t.getMatrix().setTranslate(dx, dy);

}

}

XML布局:

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:id="@+id/image"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerHorizontal="true"

android:layout_centerVertical="true"

android:src="@drawable/ic_launcher" />

你可能需要在这里和那里调整它以获得你想要的东西,但这可以作为基础.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值