本文是在我的文章android圖片處理,讓圖片變成圓形 的基礎上繼續寫的,可以去看看,直接看也沒關係,也能看懂
1、首先在res檔案夾下建立一個名字為anim的檔案夾,名字不要寫錯
2、在anim裡面建立一個xlm檔案:img_animation.xml,這個名字隨便寫都可以,注意不要大寫,裡面的代碼如下:
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" > <rotate android:duration="5000" android:fromDegrees="0" android:pivotX="50%" android:pivotY="50%" android:repeatCount="-1" android:repeatMode="restart" android:toDegrees="360" /></set>
具體含義是:
duration:時間</span>
fromDegrees="0": 從幾度開始轉</span>t
oDegrees="360" : 旋轉多少度</span>
pivotX="50%:旋轉中心距離view的左頂點為50%距離,
pivotY="50%: 距離view的上邊緣為50%距離
repeatCount="-1":重複次數,-1為一直重複
repeatMode="restart":重複模式,restart從頭開始重複
布局檔案代碼沒變,依舊是:放一個控制項就行了
</ xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ff00ff" ><com.example.circleimageview.CircleImageView android:id="@+id/imageview" android:layout_width="100dp" android:layout_height="100dp" android:layout_centerInParent="true" android:src="@drawable/control_image" /></RelativeLayout>
你也可以寫成一個普通的控制項都可以實現旋轉
複製代碼 代碼如下:
<span style="font-family: Arial, Helvetica, sans-serif;">package com.example.circleimageview;</span>import android.app.Activity;
import android.os.Bundle;import android.view.animation.Animation;import android.view.animation.AnimationUtils;import android.view.animation.LinearInterpolator;import android.widget.ImageView;public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ImageView imageView = (ImageView) findViewById(R.id.imageview); //動畫 Animation animation = AnimationUtils.loadAnimation(this, R.anim.img_animation); LinearInterpolator lin = new LinearInterpolator();//設定動畫勻速運動 animation.setInterpolator(lin); imageView.startAnimation(animation); }}
是不是很簡單,運行效果如下:錄製的有點問題,實際上是勻速地。
以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。