国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Android > 正文

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

2019-12-12 04:00:14
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

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

代碼很簡(jiǎn)單,直接上源碼

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="開(kāi)始"/>  <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{ /** 表盤(pán)圖片 */ private ImageView mDialIv; /** 開(kāi)始按鈕 */ 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)聽(tīng)  */ 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)畫(huà)  */ private void showAnimation() {  // 獲取自定義動(dòng)畫(huà)實(shí)例  CustomRotateAnim rotateAnim = CustomRotateAnim.getCustomRotateAnim();  // 一次動(dòng)畫(huà)執(zhí)行1秒  rotateAnim.setDuration(1000);  // 設(shè)置為循環(huán)播放  rotateAnim.setRepeatCount(-1);  // 設(shè)置為勻速  rotateAnim.setInterpolator(new LinearInterpolator());  // 開(kāi)始播放動(dòng)畫(huà)  mDialIv.startAnimation(rotateAnim); }}

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

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

/** * 左右搖擺動(dòng)畫(huà) * 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)畫(huà)實(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)畫(huà)的View空間寬高,parentWidth和parentHeight代表該View控件所在的父控件寬高。
我們需要使用當(dāng)前View的寬高來(lái)確定搖擺的旋轉(zhuǎn)點(diǎn),所以在initialize中獲取View控件的寬高。

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

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 迁安市| 广灵县| 客服| 四川省| 吉木萨尔县| 积石山| 勃利县| 西城区| 康平县| 南昌市| 内乡县| 林周县| 和顺县| 淄博市| 白城市| 辽源市| 阿拉善右旗| 龙海市| 房产| 静海县| 婺源县| 葫芦岛市| 镇安县| 监利县| 永城市| 西安市| 巴彦淖尔市| 三明市| 忻城县| 丹阳市| 会同县| 黄龙县| 永福县| 荣昌县| 临高县| 乌拉特前旗| 梓潼县| 崇信县| 台中县| 抚远县| 香河县|