欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android編程實(shí)現(xiàn)ImageView圖片拋物線動(dòng)畫效果的方法

 更新時(shí)間:2015年10月26日 15:38:25   作者:freesonhp  
這篇文章主要介紹了Android編程實(shí)現(xiàn)ImageView圖片拋物線動(dòng)畫效果的方法,實(shí)例分析了Android實(shí)現(xiàn)拋物線運(yùn)動(dòng)的算法原理與相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了Android編程實(shí)現(xiàn)ImageView圖片拋物線動(dòng)畫效果的方法。分享給大家供大家參考,具體如下:

想實(shí)現(xiàn)拋物線動(dòng)畫,必須知道拋物線的方程,這時(shí)候數(shù)學(xué)其作用了,假如有如圖的拋物線:

按照拋物線的方程特別,知道任何的三點(diǎn)可以確定一條拋物線,由已知拋物線的標(biāo)注

方程為 y = ax² + bx + c;假設(shè)A1坐標(biāo)為(0,0),A2坐標(biāo)為(300,0),A3坐標(biāo)為(150,300);聯(lián)合解方程得知該拋物線的方程為 y = -1/75 x² + 4x;由此方程,我們可以確定拋物線x和y的關(guān)系了,下面的事情就簡單了。

在新的API中,有ObjectAnimator動(dòng)畫,在這個(gè)動(dòng)畫里面,我們可以做一些我們想要的東西了。關(guān)于ObjectAnimator的用法,大家自己找資料去看吧:下面直接給出源碼:

//分300步進(jìn)行移動(dòng)動(dòng)畫 
final int count = 300; 
/** 
* 要start 動(dòng)畫的那張圖片的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; 
/** 
* 這里是根據(jù)三個(gè)坐標(biāo)點(diǎn){(0,0),(300,0),(150,300)}計(jì)算出來的拋物線方程 
* 
* @param x 
* @return 
*/ 
private float getY(float x) { 
  return a * x * x + 4 * x; 
}

調(diào)用的時(shí)候很簡單:startAnimation(imageView) 即可,PropertyValuesHolder,等等自己查資料吧。

附上已知拋物線三點(diǎn)求拋物線方程的算法:

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); 
 } 
}

希望本文所述對大家Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評論