Android屬性動(dòng)畫(huà)特點(diǎn)詳解
本文實(shí)例為大家分享了Android屬性動(dòng)畫(huà)使用的具體代碼,供大家參考,具體內(nèi)容如下
MainActivity.java
/*
屬性動(dòng)畫(huà)的特點(diǎn):動(dòng)畫(huà)效果會(huì)改變控件的位置.且開(kāi)啟動(dòng)畫(huà)的是動(dòng)畫(huà)對(duì)象,而不是控件對(duì)象.
只有旋轉(zhuǎn)的屬性動(dòng)畫(huà)是經(jīng)常用的,注意參數(shù).
注意:這些方法都是安卓在3.0以后出現(xiàn)的新特性,所以要把AndroidManifest.xml里的android:minSdkVersion值修改為11以上
*/
//注釋后面有222的暫時(shí)不用管.
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private ImageButton imageView;
private Button alpha_bt;
private Button rotationY_bt;
private Button scaleX_bt;
private Button translationX_bt;
private Button AnimatorSet_bt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 對(duì)控件進(jìn)行初始化
init();
// 此處用的是xml的形式.引用在Xml里的屬性動(dòng)畫(huà)資源. AnimatorInflater.loadAnimator(上下文,R.animator..)222
Animator Xmlanimator = AnimatorInflater.loadAnimator(this, R.animator.objectanimator);
// 把要做動(dòng)畫(huà)控件對(duì)象放進(jìn)去.Animator.setTarget(View對(duì)象);222
Xmlanimator.setTarget(imageView);
// 開(kāi)啟動(dòng)畫(huà).Animator.start.222
Xmlanimator.start();
}
// 對(duì)于控件進(jìn)行初始化
private void init() {
//找到ImageView控件對(duì)象
imageView = (ImageButton) findViewById(R.id.animation_iv);
//找到Button控件對(duì)象.
alpha_bt = (Button) findViewById(R.id.alpha_bt);
rotationY_bt = (Button) findViewById(R.id.rotationY_bt);
scaleX_bt = (Button) findViewById(R.id.scaleX_bt);
translationX_bt = (Button) findViewById(R.id.translationY_bt);
AnimatorSet_bt = (Button) findViewById(R.id.AnimatorSet_bt);
//為button設(shè)置點(diǎn)擊事件
alpha_bt.setOnClickListener(this);
rotationY_bt.setOnClickListener(this);
scaleX_bt.setOnClickListener(this);
translationX_bt.setOnClickListener(this);
AnimatorSet_bt.setOnClickListener(this);
}
/**
* 根據(jù)點(diǎn)擊事件類(lèi)型.調(diào)用控件做屬性動(dòng)畫(huà)的
*
* @param view
*/
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.alpha_bt:
//做透明動(dòng)畫(huà),參數(shù)1:View,代表你要修改那個(gè)控件的屬性. 參數(shù)2:propertyName代表實(shí)現(xiàn)什么樣子的動(dòng)畫(huà):"alpha",String類(lèi)型.
//參數(shù)3:float... values,控件修改的參數(shù),new float[]{0.0f, 0.2f, 0.4f, 0.6f, 0.8f, 1.0f}
ObjectAnimator alpha = ObjectAnimator.ofFloat(imageView, "alpha", new float[]{0.0f, 0.2f, 0.4f, 0.6f, 0.8f, 1.0f});
//設(shè)置動(dòng)畫(huà)執(zhí)行時(shí)長(zhǎng).setDuration
alpha.setDuration(2000);
//設(shè)置動(dòng)畫(huà)執(zhí)行的模式setRepeatMode,參數(shù)用ObjectAnimator引用.
alpha.setRepeatMode(ObjectAnimator.RESTART);
//設(shè)置動(dòng)畫(huà)執(zhí)行的次數(shù).setRepeatCount
alpha.setRepeatCount(1);
//使用ObjectAnimator對(duì)象開(kāi)啟動(dòng)畫(huà).
alpha.start();
break;
case R.id.rotationY_bt:
//做旋轉(zhuǎn)動(dòng)畫(huà),"rotationY".rotationX,rotation new float[]{90f, 180f, 270f, 360f}
ObjectAnimator rotationY = ObjectAnimator.ofFloat(imageView, "rotationY", new float[]{90f, 180f, 270f, 360f});
rotationY.setDuration(2000);
rotationY.setRepeatMode(ObjectAnimator.RESTART);
rotationY.setRepeatCount(1);
rotationY.start();
break;
case R.id.scaleX_bt:
//做縮放動(dòng)畫(huà),scaleX,scaleY new float[]{1f, 2f, 3f, 4f, 5f, 6f,1f}
ObjectAnimator scaleX = ObjectAnimator.ofFloat(imageView, "scaleX", new float[]{1f, 2f, 3f, 4f, 5f, 6f, 1f});
scaleX.setDuration(2000);
scaleX.setRepeatMode(ObjectAnimator.RESTART);
scaleX.setRepeatCount(1);
scaleX.start();
break;
case R.id.translationY_bt:
//做平移動(dòng)畫(huà),translationY,translationX new float[]{10f, 20f, 30f, 40f, 60f, 80f}
ObjectAnimator translationY = ObjectAnimator.ofFloat(imageView, "translationY", new float[]{10f, 20f, 30f, 40f, 60f, 80f});
translationY.setDuration(2000);
translationY.setRepeatMode(ObjectAnimator.RESTART);
translationY.setRepeatCount(1);
translationY.start();
//做動(dòng)畫(huà)集合AnimatorSet,分別創(chuàng)建兩個(gè)動(dòng)畫(huà)對(duì)象.注意playTogether(動(dòng)畫(huà)對(duì)象...)和playSequentially的區(qū)別.最后開(kāi)啟動(dòng)畫(huà).start
case R.id.AnimatorSet_bt:
AnimatorSet set = new AnimatorSet();
ObjectAnimator oa = ObjectAnimator.ofFloat(imageView, "translationX", new float[]{10f, 20f, 30f, 40f, 60f, 80f});
oa.setDuration(3000);
ObjectAnimator oa2 = ObjectAnimator.ofFloat(imageView, "translationY", new float[]{-10f, -20f, -30f, -40f, -60f, -80f});
oa2.setDuration(3000);
set.playTogether(oa, oa2);
set.start();
break;
default:
break;
}
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- 注意background的屬性置為null -->
<Button
android:id="@+id/alpha_bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="alpha"/>
<Button
android:id="@+id/translationY_bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="translationY"/>
<Button
android:id="@+id/scaleX_bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="scaleX"/>
<Button
android:id="@+id/rotationY_bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="rotationY"/>
<Button
android:id="@+id/AnimatorSet_bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AnimatorSet"/>
<ImageButton
android:onClick="yyyy"
android:id="@+id/animation_iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/a8"
android:background="@null"/>
</LinearLayout>
在res目錄下創(chuàng)建animator文件夾在文件夾里創(chuàng)建以下xml
objectanimator.xml
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android" android:propertyName="rotationX" android:duration="3000" android:repeatCount="1" android:repeatMode="reverse" android:startOffset="0" android:valueFrom="360.0"> </objectAnimator> <!--注意:在xml定義動(dòng)畫(huà)類(lèi)的屬性,浮點(diǎn)型小數(shù),直接寫(xiě)小數(shù)即可,不用再帶f. 提示:控件位移的參照單位在xml文件里有所不同,不過(guò)更簡(jiǎn)單,不用再特意去定義參照物屬性了,直接是根據(jù)值,區(qū)分兩種方式: 一種是以整個(gè)屏幕為參照物,在xml文件屬性定義值是int%p; 一種以控件自身大小為參照物,在xml文件屬性定義值是int-->
---------------------
作者:FanRQ_
來(lái)源:CSDN
原文:https://blog.csdn.net/FanRQ_/article/details/84072052
版權(quán)聲明:本文為博主原創(chuàng)文章,轉(zhuǎn)載請(qǐng)附上博文鏈接!
- Android動(dòng)畫(huà)系列之屬性動(dòng)畫(huà)的基本使用教程
- Android屬性動(dòng)畫(huà)實(shí)現(xiàn)圖片從左到右逐漸消失
- Android動(dòng)畫(huà)教程之屬性動(dòng)畫(huà)詳解
- Android利用屬性動(dòng)畫(huà)實(shí)現(xiàn)優(yōu)酷菜單
- Android使用屬性動(dòng)畫(huà)如何自定義倒計(jì)時(shí)控件詳解
- Android屬性動(dòng)畫(huà)之ValueAnimator代碼詳解
- Android 屬性動(dòng)畫(huà)ValueAnimator與插值器詳解
- Android源碼解析之屬性動(dòng)畫(huà)詳解
- Android深入分析屬性動(dòng)畫(huà)源碼
相關(guān)文章
Android?中?FrameLayout?布局及屬性的使用詳解
這篇文章主要介紹了Android?中?FrameLayout?布局及屬性的使用,FrameLayout?在實(shí)現(xiàn)簡(jiǎn)單布局時(shí)非常方便,特別適用于疊加式布局,如顯示疊加的圖層或浮動(dòng)按鈕等,需要的朋友可以參考下2024-03-03
Android開(kāi)發(fā)進(jìn)階自定義控件之滑動(dòng)開(kāi)關(guān)實(shí)現(xiàn)方法【附demo源碼下載】
這篇文章主要介紹了Android開(kāi)發(fā)進(jìn)階自定義控件之滑動(dòng)開(kāi)關(guān)實(shí)現(xiàn)方法,結(jié)合實(shí)例形式詳細(xì)分析了Android自定義開(kāi)關(guān)控件的原理、實(shí)現(xiàn)步驟與相關(guān)操作技巧,需要的朋友可以參考下2016-08-08
Android Studio開(kāi)發(fā)之 JNI 篇的簡(jiǎn)單示例
本篇文章主要介紹了Android Studio開(kāi)發(fā)之 JNI 篇的簡(jiǎn)單示例,它提供了若干的API實(shí)現(xiàn)了Java和其他語(yǔ)言的通信,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
Android Canvas和Bitmap結(jié)合繪圖詳解流程
在 Android Canvas 上繪圖非常難,在繪圖時(shí)需要理解許多不同的類(lèi)和概念。這篇文章中,將介紹 Android 框架中可用的一些類(lèi),它們可以讓畫(huà)布使用時(shí)更輕松2021-11-11
Android 利用方向傳感器實(shí)現(xiàn)指南針具體步驟
Android利用方向傳感器實(shí)現(xiàn)指南針功能,聽(tīng)起來(lái)還不錯(cuò)吧,下面與大家分享下具體的實(shí)現(xiàn)步驟,感興趣的朋友可以參考下哈2013-06-06
實(shí)例講解Android應(yīng)用開(kāi)發(fā)中Fragment生命周期的控制
這篇文章主要介紹了Android應(yīng)用開(kāi)發(fā)中Fragment生命周期的控制,Fragment依賴(lài)于Activity,所以生命周期方面也受Activity的影響,需要的朋友可以參考下2016-02-02
解析android中隱藏與顯示軟鍵盤(pán)及不自動(dòng)彈出鍵盤(pán)的實(shí)現(xiàn)方法
本篇文章對(duì)android中隱藏與顯示軟鍵盤(pán)以及不自動(dòng)彈出鍵盤(pán)的方法進(jìn)行了分析介紹。需要的朋友參考下2013-05-05

