網(wǎng)站首頁 編程語言 正文
本文實(shí)例為大家分享了Android Studio實(shí)現(xiàn)簡單補(bǔ)間動畫的具體代碼,供大家參考,具體內(nèi)容如下
1、動畫發(fā)在res/anim/,創(chuàng)建new/Directory
2、創(chuàng)建動畫, ?平移,縮放,旋轉(zhuǎn),改變透明度
//平移 <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> ? <translate ? ? ? android:fromXDelta="0.0" ? ? android:fromYDelta="0.0" ? ? android:toXDelta="100" ? ? android:toYDelta="0.0" ? ? android:repeatMode="reverse" ? ? android:repeatCount="infinite" ? ? android:duration="4000" ? ? /> ? </set> //縮放 <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <scale ? ? ? android:repeatMode="reverse" ? ? android:repeatCount="infinite" ? ? android:duration="3000" ? ? android:fromXScale="1.0" ? ? android:fromYScale="1.0" ? ? android:toXScale="0.5" ? ? android:toYScale="0.5" ? ? android:pivotX="50%" ? ? android:pivotY="50%"/> ? </set> //旋轉(zhuǎn) <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> ? ? ? <rotate android:fromDegrees="0" ? ? ? ? android:toDegrees="360" ? ? ? ? android:pivotX="50%" ? ? ? ? android:pivotY="50%" ? ? ? ? android:repeatMode="reverse" ? ? ? ? android:repeatCount="infinite" ? ? ? ? android:duration="1000" ? ? ? ? /> ? </set> //改變透明度 <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <alpha ? ? android:interpolator="@android:anim/linear_interpolator" ? ? android:repeatMode="reverse" ? ? android:repeatCount="infinite" ? ? android:duration="1000" ? ? android:fromAlpha="1.0" ? ? android:toAlpha="0.0"/> ? </set>
?3、布局activity_main.xml,點(diǎn)擊按鈕圖片發(fā)送變化,圖片需要自己下載添加44
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? xmlns:app="http://schemas.android.com/apk/res-auto" ? ? xmlns:tools="http://schemas.android.com/tools" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent" ? ? android:layout_weight="1" ? ? android:orientation="vertical" ? ? tools:context=".MainActivity"> ? ? ? ? <ImageView ? ? ? ? ? android:layout_width="300dp" ? ? ? ? ? android:layout_height="300dp" ? ? ? ? ? android:layout_centerInParent="true" ? ? ? ? ? android:layout_gravity="center" ? ? ? ? ? android:id="@+id/fk" ? ? ? ? ? android:src="@drawable/ff"/> ? ? ?<LinearLayout ? ? ? ?android:layout_width="match_parent" ? ? ? ?android:layout_height="match_parent" ? ? ? android:orientation="horizontal"> ? ? ? ? <Button ? ? ? ?android:layout_width="wrap_content" ? ? ? ?android:layout_height="wrap_content" ? ? ? ?android:text="平移" ? ? ? ?android:textSize="24sp" ? ? ? ?android:layout_weight="1" ? ? ? ? android:gravity="center" ? ? ? ? ? android:layout_gravity="bottom" ? ? ? ?android:id="@+id/but"/> ? ? ? <Button ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? android:text="縮放" ? ? ? ? ? android:textSize="24sp" ? ? ? ? ? android:layout_gravity="bottom" ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? android:gravity="center" ? ? ? ? ? android:id="@+id/but1"/> ? ? ? <Button ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? android:text="旋轉(zhuǎn)" ? ? ? ? ? android:textSize="24sp" ? ? ? ? ? android:gravity="center" ? ? ? ? ? android:layout_gravity="bottom" ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? android:id="@+id/but2"/> ? ? ? <Button ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? android:text="改變透明度" ? ? ? ? ? android:textSize="24sp" ? ? ? ? ? android:gravity="center" ? ? ? ? ? android:layout_gravity="bottom" ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? android:id="@+id/but3"/> ? ?</LinearLayout> ? </RelativeLayout>
4、邏輯代碼MainActivity
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
? ? private ImageView imageView;
? ? private Button button;
? ? private Button button1;
? ? private Button button2;
? ? private Button button3;
?
? ? @RequiresApi(api = Build.VERSION_CODES.N)
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ? //要用控件,定id
? ? ? ? button=findViewById(R.id.but);
? ? ? ? button1=findViewById(R.id.but1);
? ? ? ? button2=findViewById(R.id.but2);
? ? ? ? button3=findViewById(R.id.but3);
?
? ? ? ? imageView=findViewById(R.id.fk);
? ? ? ? button.setOnClickListener(this);
? ? ? ? button1.setOnClickListener(this);
? ? ? ? button2.setOnClickListener(this);
? ? ? ? button3.setOnClickListener(this);
?
? ? }
?
? ? @Override
? ? public void onClick(View view) {
? ? ? ? switch (view.getId()){
? ? ? ? ? ? case R.id.but:
? ? ? ? ? ? ? ? Animation translation= AnimationUtils.loadAnimation(this,R.anim.translate);
? ? ? ? ? ? ? ? imageView.startAnimation(translation);
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case R.id.but1:
? ? ? ? ? ? ? ? Animation scale= AnimationUtils.loadAnimation(this,R.anim.scale);
? ? ? ? ? ? ? ? imageView.startAnimation(scale);
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case R.id.but2:
? ? ? ? ? ? ? ? Animation rotate= AnimationUtils.loadAnimation(this,R.anim.rotate);
? ? ? ? ? ? ? ? imageView.startAnimation(rotate);
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case R.id.but3:
? ? ? ? ? ? ? ? Animation alpha= AnimationUtils.loadAnimation(this,R.anim.alpha);
? ? ? ? ? ? ? ? imageView.startAnimation(alpha);
? ? ? ? ? ? ? ? break;
? ? ? ? }
?
? ? }
}
原文鏈接:https://blog.csdn.net/weixin_63307826/article/details/123941507
相關(guān)推薦
- 2022-01-12 解決element-ui 日期選擇器提交后臺數(shù)據(jù)不準(zhǔn)確問題
- 2022-10-08 C語言深入分析浮點(diǎn)型數(shù)據(jù)存儲_C 語言
- 2023-01-02 Flutter狀態(tài)管理Provider示例解析_Android
- 2022-06-28 C#操作串口通信協(xié)議Modbus的常用方法介紹_C#教程
- 2022-05-03 python單例模式之selenium?driver實(shí)現(xiàn)單例_python
- 2023-05-12 Python?pandas?的索引方式?data.loc[],data[][]示例詳解_python
- 2022-11-29 如果服務(wù)器出現(xiàn)內(nèi)存泄漏,堆內(nèi)存緩慢上漲,一段時間后觸發(fā)了fullGc,如何快速定位?
- 2022-12-23 C++類成員函數(shù)中的名字查找問題_C 語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支