Android ImageView 图片抛物线动画的实现方案

想实现抛物线动画,必须知道抛物线的方程,这时候数学其作用了,假如有如图的抛物线:

,按照抛物线的方程特别,知道任何的三点可以确定一条抛物线,由已知抛物线的标注

方程为 y = ax² + bx + c;假设A1坐标为(0,0),A2坐标为(300,0),A3坐标为(150,300);联合解方程得知该抛物线的方程为 y =  -1/75 x² + 4x;由此方程,我们可以确定抛物线x和y的关系了,下面的事情就简单了。

在新的API中,有ObjectAnimator动画,在这个动画里面,我们可以做一些我们想要的东西了。关于ObjectAnimator的用法,大家自己找资料去看吧:下面直接给出源码:

//分300步进行移动动画
	final int count = 300;

	/**
	 * 要start 动画的那张图片的ImageView
	 * @param imageView
	 */
	private void startAnimation(final ImageView imageView) {

		Keyframe[] keyframes = new Keyframe[count];
		final float keyStep = 1f / (float) count;
		float key = keyStep;
		for (int i = 0; i < count; ++i) {
			keyframes[i] = Keyframe.ofFloat(key, i + 1);
			key += keyStep;
		}

		PropertyValuesHolder pvhX = PropertyValuesHolder.ofKeyframe("translationX", keyframes);
		key = keyStep;
		for (int i = 0; i < count; ++i) {
			keyframes[i] = Keyframe.ofFloat(key, -getY(i + 1));
			key += keyStep;
		}

		PropertyValuesHolder pvhY = PropertyValuesHolder.ofKeyframe("translationY", keyframes);
		ObjectAnimator yxBouncer = ObjectAnimator.ofPropertyValuesHolder(imageView, pvhY, pvhX).setDuration(1500);
		yxBouncer.setInterpolator(new BounceInterpolator());
		yxBouncer.start();
	}

	final float a = -1f / 75f;

	/**
	 * 这里是根据三个坐标点{(0,0),(300,0),(150,300)}计算出来的抛物线方程
	 * 
	 * @param x
	 * @return
	 */
	private float getY(float x) {
		return a * x * x + 4 * x;
	}


调用的时候很简单:startAnimation(imageView) 即可,PropertyValuesHolder,等等自己查资料吧。


附上已知抛物线三点求抛物线方程的算法:

package com.freesonfish;

public class ParabolaAlgorithm {

	public static void main(String[] args) {
		final float[][] points = { { 6, 15 }, { 15, 70 }, { 40, 60 } };
		calculate(points);
	}

	/**
	 * a = (y1 * (x2 - x3) + y2 * (x3 - x1) + y3 * (x1 - x2)) / (x1 * x1 * (x2 -
	 * x3) + x2 * x2 * (x3 - x1) + x3 * x3 * (x1 - x2)) 
	 * b = (y1 - y2) / (x1 - x2) - a * (x1 + x2); 
	 * c = y1 - (x1 * x1) * a - x1 * b;
	 */
	private static void calculate(float[][] points) {
		float x1 = points[0][0];
		float y1 = points[0][1];
		float x2 = points[1][0];
		float y2 = points[1][1];
		float x3 = points[2][0];
		float y3 = points[2][1];

		final float a = (y1 * (x2 - x3) + y2 * (x3 - x1) + y3 * (x1 - x2))
				/ (x1 * x1 * (x2 - x3) + x2 * x2 * (x3 - x1) + x3 * x3 * (x1 - x2));
		final float b = (y1 - y2) / (x1 - x2) - a * (x1 + x2);
		final float c = y1 - (x1 * x1) * a - x1 * b;
		
		System.out.println("-a->" + a + " b->" +b + " c->" +c);
	}
}

附上demo文件地址: 点击下载

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
你可以通过使用属性动画实现点击 ImageView 图片转圈的效果。下面是一个简单的示例代码: ```java import android.animation.ObjectAnimator; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.view.animation.LinearInterpolator; import android.widget.ImageView; public class MainActivity extends AppCompatActivity { private ImageView imageView; private ObjectAnimator rotationAnimator; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageView = findViewById(R.id.imageView); // 设置属性动画 rotationAnimator = ObjectAnimator.ofFloat(imageView, "rotation", 0f, 360f); rotationAnimator.setDuration(1000); // 设置动画时长为1秒 rotationAnimator.setInterpolator(new LinearInterpolator()); // 设置动画插值器为线性插值器 rotationAnimator.setRepeatCount(ObjectAnimator.INFINITE); // 设置动画重复次数为无限次 rotationAnimator.setRepeatMode(ObjectAnimator.RESTART); // 设置动画重复模式为重新开始 imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (rotationAnimator.isRunning()) { rotationAnimator.cancel(); // 如果动画正在进行,则取消动画 } else { rotationAnimator.start(); // 否则开始动画 } } }); } } ``` 这段代码首先获取到一个 ImageView 实例,然后创建了一个属性动画 `rotationAnimator` 来实现图片的旋转效果。在点击 ImageView 的时候,通过判断动画是否正在进行来决定是开始动画还是取消动画。注意需要在布局文件中添加一个 ImageView,并将其 id 设置为 `imageView`。 希望这可以帮到你!如果还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值