最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android自定義Animation實(shí)現(xiàn)View搖擺效果

 更新時(shí)間:2017年01月17日 15:13:15   作者:ZHU_文濤  
這篇文章主要為大家詳細(xì)介紹了Android自定義Animation實(shí)現(xiàn)View搖擺效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

使用自定義Animation,實(shí)現(xiàn)View的左右搖擺效果,如圖所示:

代碼很簡單,直接上源碼

activity_maini.xml布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="#ffffff"
 android:gravity="center"
 android:orientation="vertical">

 <!--圖片-->
 <ImageView
  android:id="@+id/iv_dial"
  android:layout_width="200dp"
  android:layout_height="200dp"
  android:src="@drawable/img"/>

 <!--控制按鈕-->
 <LinearLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_gravity="bottom"
  android:gravity="center"
  android:orientation="horizontal">

  <Button
   android:id="@+id/btn_start"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="開始"/>

  <Button
   android:id="@+id/btn_end"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="結(jié)束"/>

 </LinearLayout>

</LinearLayout>

也可以用其它的View控件替代ImageView,都是可以實(shí)現(xiàn)搖擺效果的

主界面MainActivity

/**
 * 主界面
 * Created by zhuwentao on 2016-08-08.
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener{

 /** 表盤圖片 */
 private ImageView mDialIv;

 /** 開始按鈕 */
 private Button mStartBtn;

 /** 結(jié)束按鈕 */
 private Button mEndBtn;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  initUI();
  initListener();
 }

 /**
  * 初始化UI
  */
 private void initUI() {
  mDialIv = (ImageView) findViewById(R.id.iv_dial);
  mStartBtn = (Button) findViewById(R.id.btn_start);
  mEndBtn = (Button) findViewById(R.id.btn_end);
 }

 /**
  * 初始化監(jiān)聽
  */
 private void initListener() {
  mStartBtn.setOnClickListener(this);
  mEndBtn.setOnClickListener(this);
 }

 @Override
 public void onClick(View v) {
  switch (v.getId()) {
   case R.id.btn_start:
    showAnimation();
    break;
   case R.id.btn_end:
    mDialIv.clearAnimation();
    break;
  }
 }

 /**
  * 設(shè)置動(dòng)畫
  */
 private void showAnimation() {
  // 獲取自定義動(dòng)畫實(shí)例
  CustomRotateAnim rotateAnim = CustomRotateAnim.getCustomRotateAnim();
  // 一次動(dòng)畫執(zhí)行1秒
  rotateAnim.setDuration(1000);
  // 設(shè)置為循環(huán)播放
  rotateAnim.setRepeatCount(-1);
  // 設(shè)置為勻速
  rotateAnim.setInterpolator(new LinearInterpolator());
  // 開始播放動(dòng)畫
  mDialIv.startAnimation(rotateAnim);
 }
}

setRepeatCount()設(shè)置的是重復(fù)播放動(dòng)畫的次數(shù),-1是為了讓它循環(huán)播放,setRepeatCount(0)代表的是執(zhí)行一次,setRepeatCount(1)代表重復(fù)1次,即動(dòng)畫執(zhí)行2次。
setInterpolator()方法是設(shè)置插值器,用來指定動(dòng)畫的效果,這里使用系統(tǒng)提供的LinearInterpolator()勻速變化效果。

自定義的CustomRotateAnim動(dòng)畫需要繼承Animation,這里只要實(shí)現(xiàn)它的initialize()和applyTransformation()方法就好

/**
 * 左右搖擺動(dòng)畫
 * Created by zhuwentao on 2016-08-08.
 */
public class CustomRotateAnim extends Animation {

 /** 控件寬 */
 private int mWidth;

 /** 控件高 */
 private int mHeight;

 /** 實(shí)例 */
 private static CustomRotateAnim rotateAnim;

 /**
  * 獲取動(dòng)畫實(shí)例
  * @return 實(shí)例
  */
 public static CustomRotateAnim getCustomRotateAnim() {
  if (null == rotateAnim) {
   rotateAnim = new CustomRotateAnim();
  }
  return rotateAnim;
 }

 @Override
 public void initialize(int width, int height, int parentWidth, int parentHeight) {
  this.mWidth = width;
  this.mHeight = height;
  super.initialize(width, height, parentWidth, parentHeight);
 }

 @Override
 protected void applyTransformation(float interpolatedTime, Transformation t) {
  // 左右搖擺
  t.getMatrix().setRotate((float)(Math.sin(interpolatedTime*Math.PI*2)*50), mWidth/2, mHeight/2);
  super.applyTransformation(interpolatedTime, t);
 }
}

initialize(int width, int height, int parentWidth, int parentHeight)中,width和height代表指定播放動(dòng)畫的View空間寬高,parentWidth和parentHeight代表該View控件所在的父控件寬高。
我們需要使用當(dāng)前View的寬高來確定搖擺的旋轉(zhuǎn)點(diǎn),所以在initialize中獲取View控件的寬高。

applyTransformation()方法是動(dòng)畫具體的實(shí)現(xiàn)方法,在系統(tǒng)繪制動(dòng)畫時(shí)會(huì)反復(fù)調(diào)用這個(gè)方法,每調(diào)用一次applyTransformation()方法,其中的interpolatedTime參數(shù)都會(huì)改變一次,值從0到1遞增,當(dāng)interpolatedTime的值為1時(shí)則動(dòng)畫結(jié)束。
Transformatio類是一個(gè)變換的矩陣,通過改變?cè)摼仃嚲涂梢詫?shí)現(xiàn)各種復(fù)雜的效果。
復(fù)寫這個(gè)方法,在里面就可以實(shí)現(xiàn)我們自定義的動(dòng)畫效果了。

相關(guān)文章

最新評(píng)論

浮梁县| 新沂市| 东乌珠穆沁旗| 渑池县| 穆棱市| 丘北县| 内丘县| 阳西县| 红原县| 桓仁| 阿克| 资中县| 桂林市| 鸡西市| 农安县| 岢岚县| 泗洪县| 泽库县| 枣阳市| 如东县| 手机| 临朐县| 贵阳市| 奎屯市| 曲周县| 龙陵县| 成都市| 乳山市| 赤城县| 武威市| 招远市| 新源县| 集安市| 河西区| 定南县| 泾川县| 甘孜| 丹江口市| 青田县| 南阳市| 资讯 |