當前位置:首頁 » 安卓系統 » android圓動畫

android圓動畫

發布時間: 2022-12-29 12:47:58

A. Android動畫之ViewPropertyAnimator(專用於view的屬性動畫)

屬性動畫對比原來的視圖動畫有很多的優點,屬性動畫可以對所有的對象做動畫操作,但Android開發中需要做動畫最多的還是View,如果只是對一個view做動畫,很少的幾個屬性還行,如果是對同一個view的十幾個屬性同時做動畫,相信屬性動畫的寫法是比較繁瑣的。ViewPropertyAnimator從名字就可以看出是專用於View的屬性動畫,在API12被提供。ViewPropertyAnimator專用於操作View動畫,語法更加簡潔,使用更加方便。
developer: https://developer.android.google.cn/reference/android/view/ViewPropertyAnimator

如何獲取ViewPropertyAnimator 對象:
ViewPropertyAnimator 沒有構造函數,通過View.animate()方法可以方便的獲取ViewPropertyAnimator 對象,此時獲取的動畫對象就專用於操作當前view。

setDuration(); //設置動畫時長
setInterpolator(); //設置插值器
setStartDelay(); //設置延遲開始時間
start(); //立刻開始動畫
cancel(); //取消動畫

明顯看到圖片先往下走了一段,然後向左的動畫才開始執行。

鏈式操作
ViewPropertyAnimator viewPropertyAnimator = gongxiang.animate().setDuration(2000).translationY(300).scaleX(0.5f);

以上函數的執行,會導致相同動畫的cancle,以上函數都是除了坐標相關都是以view左上角為坐標原點。

方法都比較簡單,下面距幾個列子:

首先利用translationX,translationXBy來區分By的意義:
ViewPropertyAnimator viewPropertyAnimator = gongxiang.animate().setDuration(3000).translationY(300);

ViewPropertyAnimator viewPropertyAnimator = gongxiang.animate().setDuration(3000).translationYBy(300);

translationYBy 可以多次移動View,translationY多次執行沒有效果。

ViewPropertyAnimator viewPropertyAnimator = gongxiang.animate().setDuration(3000).x(100).y(100);

ViewPropertyAnimator viewPropertyAnimator = gongxiang.animate().setDuration(2000).rotation(270);

據觀察withEndAction相當於動畫開始,withStartAction相當於動畫結束。

可以看到可以添加setUpdateListener監聽,但無法通過這個監聽函數監聽到當前動畫的信息,所以這個監聽函數可能用處不大。

Animation動畫概述和執行原理
Android動畫之補間動畫TweenAnimation
Android動畫之逐幀動畫FrameAnimation
Android動畫之插值器簡介和系統默認插值器
Android動畫之插值器Interpolator自定義
Android動畫之視圖動畫的缺點和屬性動畫的引入
Android動畫之ValueAnimator用法和自定義估值器
Android動畫之ObjectAnimator實現補間動畫和ObjectAnimator自定義屬性
Android動畫之ObjectAnimator中ofXX函數全解析-自定義Property,TypeConverter,TypeEvaluator
Android動畫之AnimatorSet聯合動畫用法
Android動畫之LayoutTransition布局動畫
Android動畫之共享元素動畫
Android動畫之ViewPropertyAnimator(專用於view的屬性動畫)
Android動畫之Activity切換動畫overridePendingTransition實現和Theme Xml方式實現
Android動畫之ActivityOptionsCompat概述
Android動畫之場景變換Transition動畫的使用
Android動畫之Transition和TransitionManager使用
Android動畫之圓形揭露動畫Circular Reveal
Android 動畫之 LayoutAnimation 動畫
Android動畫之視圖動畫的缺點和屬性動畫的引入

B. android中怎麼讓圓形進度條動畫

首先解決問題一的問題: 你肯定使用了系統的 oncreateDialog 和 showdialog 方法了,所以 這個就會顯示一次 第二次不顯示了 , 你應該調用 dialog方法裡面的 show()方法 來顯示,這樣每次顯示那個progressbar都會在轉圈 問題二: 你說下面設置了一個白色背景,但是下面還是有個框,不過我有個疑問,如果你設置為白色背景,那麼你的轉條默認是白色的不就看不見了嗎?好了這個問題不糾結了!那個黑色的是邊框導致的,去掉邊框就行了 一般我們採用的是自定義dialog,也就是寫一個類來繼承dialog,這個時候的構造函數是這個: public MyDialog(Context context, int theme) { super(context, theme); this.context = context; init(); } 這個theme是什麼東西呢? 就是一個style樣式 如下: <resources>    <style name="dialog" parent="@android:style/Theme.Dialog">        <item name="android:windowFrame">@null</item><!--邊框--        <item name="android:windowIsFloating">true</item><!--是否浮現在activity之上--       <item name="android:windowIsTranslucent">false</item><!--半透明--        <item name="android:windowNoTitle">true</item><!--無標題--        <item name="android:windowBackground">@color/transparent</item><!--背景透明--        <item name="android:backgroundDimEnabled">false</item><!--模糊--    </style></resources>  其他代碼: @Override public boolean onKeyDown(int keyCode, KeyEvent event) { Dialog dialog = new MyDialog(this, R.style.MyDialog); android.view.WindowManager.LayoutParams pa3 = new android.view.WindowManager.LayoutParams(); pa3.height = android.view.WindowManager.LayoutParams.WRAP_CONTENT; pa3.width = android.view.WindowManager.LayoutParams.WRAP_CONTENT; pa3.x = 0;//x 起點 pa3.y = 0;//y起點 Window window = dialog.getWindow(); window.setAttributes(pa3); dialog.show(); return super.onKeyDown(keyCode, event); } 之所以android.view.WindowManager.LayoutParams我要這么寫,因為LayoutParams太多了,我害怕你找半天,兄弟對你夠好了吧! 要是選為精彩回答 那就謝謝你了!

C. Carson帶你學Android:常見的三種動畫類型

Android 動畫主要分為分為兩大類(三種):

下面。我將簡單介紹這兩大類、三種 Android 常用動畫

根據不同的動畫效果,補間動畫分為4種動畫:

具體效果分別如下:

較為復雜的個性化動畫效果。

將動畫拆分後的圖片幀

在 Android 3.0 ( API 11 )後才提供的一種全新動畫模式

與屬性相關、更加復雜的動畫效果。

不定期分享關於 安卓開發 的干貨,追求 短、平、快 ,但 卻不缺深度

D. 如何在android studio 中實現圓餅圖的轉動

/**
* 隨手記中可以任意旋轉的炫酷餅圖的實現原理
*
* 小記:
* 在實現的過程中,主要是用到了一些數學計算來實現角度和屏幕位置坐標的計算
* 關於任意兩個點之間的角度計算的問題,一開始想了很久沒有結果,最後,一個偶然的靈光,讓整個
* 事情變得簡單起來,就是計算任意兩個點相對於中心坐標的角度的時候,首先,計算
* 每個點相對於x軸正方向的角度,這樣,總可以將其轉化為計算直角三角形的內角計算問題
* 再將兩次計算的角度進行減法運算,就實現了。是不是很簡單?呵呵,對於像我們這樣數學
* 沒有學好的開發者來說,也只有這樣化難為簡了
*
* @author liner
*
*/
public class PieChart extends View{
public static final String TAG = "PieChart";
public static final int ALPHA = 100;
public static final int ANIMATION_DURATION = 800;
public static final int ANIMATION_STATE_RUNNING = 1;
public static final int ANIMATION_STATE_DOWN = 2;
/**
* 不要問我這個值是怎麼設置的。這個就是圖片中的一大塊圓形區域對應的長方形四個邊的坐標位置
* 具體的值,自己需要多次嘗試並調整了。這樣,我們的餅圖就是相對於這個區域來畫的
*/
private static final RectF OVAL = new RectF(18,49,303,340);
private int[] colors; //每部分的顏色值
private int[] values; //每部分的大小
private int[] degrees; //值轉換成角度
private String[] titles; //每部分的內容
private Paint paint;
private Paint maskPaint;
private Paint textPaint;
private Point lastEventPoint;
private int currentTargetIndex = -1;
private Point center; //這個是餅圖的中心位置
private int eventRadius = 0; //事件距離餅圖中心的距離
//測試的時候使用的
//private ChartClickListener clickListener;
private Bitmap mask; //用於遮罩的Bitmap
private int startDegree = 90; //讓初始的時候,圓餅是從箭頭位置開始畫出的
private int animState = ANIMATION_STATE_DOWN;
private boolean animEnabled = false;
private long animStartTime;
public PieChart(Context context) {
super(context);
init();
}
public PieChart(Context context, AttributeSet attrs){
this(context, attrs, 0);
}
public PieChart(Context context, AttributeSet attrs, int defStyle){
super(context, attrs, defStyle);
init();
}
private void init(){
paint = new Paint();
maskPaint = new Paint();
textPaint = new Paint();
textPaint.setColor(Color.WHITE);
textPaint.setTypeface(Typeface.DEFAULT_BOLD);
textPaint.setAlpha(100);
textPaint.setTextSize(16);
values = new int[]{
60,
90,
30,
50,
70
};
//titles = new String[]{
//"川菜",
//"徽菜",
//"粵菜",
//"閩菜",
//"湘菜"
//};
//測試文字居中顯示
titles = new String[]{
"我是三歲",
"說了算四大皆空",
"士大",
"史蒂芬森地",
"湘"
};
colors = new int[]{
Color.argb(ALPHA, 249, 64, 64),
Color.argb(ALPHA, 0, 255, 0),
Color.argb(ALPHA, 255, 0, 255),
Color.argb(ALPHA, 255, 255, 0),
Color.argb(ALPHA, 0, 255, 255)
};
degrees = getDegrees();
//Drawable d = getResources().getDrawable(R.drawable.mask);
mask = BitmapFactory.decodeResource(getResources(), R.drawable.mask);
//獲取初始位置的時候,下方箭頭所在的區域
animEnabled = true; //同時,啟動動畫
}
//public void setChartClickListener(ChartClickListener l){
//this.clickListener = l;
//}
//計算總和
private int sum(int[] values){
int sum = 0;
for(int i=0; i<values.length;i++){
sum += values[i];
}
return sum;
}
/**
* 根據每部分所佔的比例,來計算每個區域在整個圓中所佔的角度
* 但是,有個小細節,就是計算的時候注意,可能並不能整除的情況,這個時候,為了
* 避免所有的角度和小於360度的情況,姑且將剩餘的部分送給某個部分,反正也不影響
* @return
*/
private int[] getDegrees(){
int sum = this.sum(values);
int[] degrees = new int[values.length];
for(int i=0; i<values.length; i++){
degrees[i] = (int)Math.floor((double)((double)values[i]/(double)sum)*360);
//Log.v("Angle", angles[i]+"");
}
int angleSum = this.sum(degrees);
if(angleSum != 360){
//上面的計算可能導致和小於360
int c = 360 - angleSum;
degrees[values.length-1] += c; //姑且讓最後一個的值稍大點
}
return degrees;
}
/**
* 重寫這個方法來畫出整個界面
*/
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if(animEnabled){
/**
* 說明是啟動的時候,需要旋轉著畫出餅圖
*/
Log.e(TAG, "anim enabled");
if(animState == ANIMATION_STATE_DOWN){
animStartTime = SystemClock.uptimeMillis();
animState = ANIMATION_STATE_RUNNING;

}

final long currentTimeDiff = SystemClock.uptimeMillis() - animStartTime;
int currentMaxDegree = (int)((float)currentTimeDiff/ANIMATION_DURATION*360f);
Log.e(TAG, "當前最大的度數為:"+currentMaxDegree);
if(currentMaxDegree >= 360){
//動畫結束狀態,停止繪制
currentMaxDegree = 360;
animState = ANIMATION_STATE_DOWN;
animEnabled = false;
}
int[] degrees = getDegrees();
int startAngle = this.startDegree;

//獲取當前時刻最大可以旋轉的角度所位於的區域
int maxIndex = getEventPart(currentMaxDegree);

//根據不同的顏色畫餅圖
for(int i=0; i<= maxIndex; i++){
int currentDegree = degrees[i];

if(i== maxIndex){
//對於當前最後一個繪制區域,可能只是一部分,需要獲取其偏移量
currentDegree = getOffsetOfPartStart(currentMaxDegree, maxIndex);
}

if(i > 0){
//注意,每次畫餅圖,記得計算startAngle
startAngle += degrees[i-1];
}

paint.setColor(colors[i]);
canvas.drawArc(OVAL, startAngle, currentDegree, true, paint);
}

if(animState == ANIMATION_STATE_DOWN){
//如果動畫結束了,則調整當前箭頭位於所在區域的中心方向
onStop();

}else{
postInvalidate();
}

}else{
int[] degrees = getDegrees();
int startAngle = this.startDegree;

/**
* 每個區域的顏色不同,但是這里只要控制好每個區域的角度就可以了,整個是個圓
*/
for(int i=0; i<values.length; i++){
paint.setColor(colors[i]);
if(i>0){
startAngle += degrees[i-1];
}
canvas.drawArc(OVAL, startAngle, degrees[i], true, paint);
}
}


/**
* 畫出餅圖之後,畫遮罩圖片,這樣圖片就位於餅圖之上了,形成了遮罩的效果
*/
canvas.drawBitmap(mask, 0, 0, maskPaint);

/**
* 根據當前計算得到的箭頭所在區域顯示該區域代表的信息
*/
if(currentTargetIndex >= 0){
String title = titles[currentTargetIndex];
textPaint.setColor(colors[currentTargetIndex]);
//簡單作個計算,讓文字居中顯示
int width = title.length()*17;
canvas.drawText(title, 157-width/2+3, 383, textPaint);
}
}

/**
* 處理餅圖的轉動
*/
public boolean onTouchEvent(MotionEvent event){

if(animEnabled && animState == ANIMATION_STATE_RUNNING){
return super.onTouchEvent(event);
}

Point eventPoint = getEventAbsoluteLocation(event);
computeCenter(); //計算中心坐標

//計算當前位置相對於x軸正方向的角度
//在下面這個方法中計算了eventRadius的
int newAngle = getEventAngle(eventPoint, center);

int action = event.getAction();

switch (action) {
case MotionEvent.ACTION_DOWN:
lastEventPoint = eventPoint;
if(eventRadius > getRadius()){
/**
* 只有點在餅圖內部才需要處理轉動,否則直接返回
*/
Log.e(TAG, "當前位置超出了半徑:"+eventRadius+">"+getRadius());
return super.onTouchEvent(event);
}
break;
case MotionEvent.ACTION_MOVE:
//這里處理滑動
rotate(eventPoint, newAngle);
//處理之後,記得更新lastEventPoint
lastEventPoint = eventPoint;
break;
case MotionEvent.ACTION_UP:
onStop();
break;
default:
break;
}

return true;
}

/**
* 當我們停止旋轉的時候,如果當前下方箭頭位於某個區域的非中心位置,則我們需要計算
* 偏移量,並且將箭頭指向中心位置
*/
private void onStop() {
int targetAngle = getTargetDegree();
currentTargetIndex = getEventPart(targetAngle);
int offset = getOffsetOfPartCenter(targetAngle, currentTargetIndex);
/**
* offset>0,說明當前箭頭位於中心位置右邊,則所有區域沿著順時針旋轉offset大小的角度
* offset<0,正好相反
*/
startDegree += offset;
postInvalidateDelayed(200);
}
private void rotate(Point eventPoint, int newDegree) {
//計算上一個位置相對於x軸正方向的角度
int lastDegree = getEventAngle(lastEventPoint, center);

/**
* 其實轉動就是不斷的更新畫圓弧時候的起始角度,這樣,每次從新的起始角度重畫圓弧就形成了轉動的效果
*/
startDegree += newDegree-lastDegree;

//轉多圈的時候,限定startAngle始終在-360-360度之間
if(startDegree >= 360){
startDegree -= 360;
}else if(startDegree <= -360){
startDegree += 360;
}

Log.e(TAG, "當前startAngle:"+startDegree);

//獲取當前下方箭頭所在的區域,這樣在onDraw的時候就會轉到不同區域顯示的是當前區域對應的信息
int targetDegree = getTargetDegree();
currentTargetIndex = getEventPart(targetDegree);

//請求重新繪制界面,調用onDraw方法
postInvalidate();
}


/**
* 獲取當前事件event相對於屏幕的坐標
* @param event
* @return
*/
protected Point getEventAbsoluteLocation(MotionEvent event){
int[] location = new int[2];
this.getLocationOnScreen(location); //當前控制項在屏幕上的位置

int x = (int)event.getX();
int y = (int)event.getY();

x += location[0];
y += location[1]; //這樣x,y就代表當前事件相對於整個屏幕的坐標

Point p = new Point(x, y);

Log.v(TAG, "事件坐標:"+p.toString());

return p;
}
/**
* 獲取當前餅圖的中心坐標,相對於屏幕左上角
*/
protected void computeCenter(){
if(center == null){
int x = (int)OVAL.left + (int)((OVAL.right-OVAL.left)/2f);
int y = (int)OVAL.top + (int)((OVAL.bottom - OVAL.top)/2f)+50; //狀態欄的高度是50
center = new Point(x,y);
//Log.v(TAG, "中心坐標:"+center.toString());
}
}
/**
* 獲取半徑
*/
protected int getRadius(){
int radius = (int)((OVAL.right-OVAL.left)/2f);
//Log.v(TAG, "半徑:"+radius);
return radius;
}
/**
* 獲取事件坐標相對於餅圖的中心x軸正方向的角度
* 這里就是坐標系的轉換,本例中使用餅圖的中心作為坐標中心,就是我們從初中到大學一直使用的"正常"坐標系。
* 但是涉及到圓的轉動,本例中一律相對於x正方向順時針來計算某個事件在坐標系中的位置
* @param eventPoint
* @param center
* @return
*/
protected int getEventAngle(Point eventPoint, Point center){
int x = eventPoint.x - center.x;//x軸方向的偏移量
int y = eventPoint.y - center.y; //y軸方向的偏移量
//Log.v(TAG, "直角三角形兩直邊長度:"+x+","+y);
double z = Math.hypot(Math.abs(x), Math.abs(y)); //求直角三角形斜邊的長度
//Log.v(TAG, "斜邊長度:"+z);
eventRadius = (int)z;
double sinA = (double)Math.abs(y)/z;
//Log.v(TAG, "sinA="+sinA);
double asin = Math.asin(sinA); //求反正玄,得到當前點和x軸的角度,是最小的那個
//Log.v(TAG, "當前相對偏移角度的反正弦:"+asin);
int degree = (int)(asin/3.14f*180f);
//Log.v(TAG, "當前相對偏移角度:"+angle);
//下面就需要根據x,y的正負,來判斷當前點和x軸的正方向的夾角
int realDegree = 0;
if(x<=0 && y<=0){
//左上方,返回180+angle
realDegree = 180+degree;
}else if(x>=0 && y<=0){
//右上方,返回360-angle
realDegree = 360-degree;
}else if(x<=0 && y>=0){
//左下方,返回180-angle
realDegree = 180-degree;
}else{
//右下方,直接返回
realDegree = degree;
}
//Log.v(TAG, "當前事件相對於中心坐標x軸正方形的順時針偏移角度為:"+realAngle);
return realDegree;
}
/**
* 獲取當前下方箭頭位置相對於startDegree的角度值
* 注意,下方箭頭相對於x軸正方向是90度
* @return
*/
protected int getTargetDegree(){
int targetDegree = -1;
int tmpStart = startDegree;
/**
* 如果當前startAngle為負數,則直接+360,轉換為正值
*/
if(tmpStart < 0){
tmpStart += 360;
}
if(tmpStart < 90){
/**
* 如果startAngle小於90度(可能為負數)
*/
targetDegree = 90 - tmpStart;
}else{
/**
* 如果startAngle大於90,由於在每次計算startAngle的時候,限定了其最大為360度,所以
* 直接可以按照如下公式計算
*/
targetDegree = 360 + 90 - tmpStart;
部分代碼 網路知道限制字數 下載附件查看

E. Android開發,怎樣做一個畫圓圈的動畫效果

畫圓應該很簡單,自動循環的話,你就每隔一段時間刷新一下view

F. Android 圓形進度條控制項

自定義控制項圓形進度條,支持頂部圓環漸變色,以及動畫功能.

res/vasues/attrs.xml

G. Android 使用圓形揭露動畫巧妙地隱藏或顯示View

在開發過程中,我們經常會遇到需要顯示或隱藏View視圖的情況,如果在隱藏或顯示View的過程中加上動畫,能讓交互更加的友好和動感,本文將介紹如何使用圓形揭露動畫巧妙地隱藏或顯示View。

圓形揭露動畫是動畫的一種,是由ViewAnimationUtils類提供的,調用ViewAnimationUtils.createCircularReveal()方法可以創建圓形揭露動畫,使用此動畫要求API級別為21及以上版本,createCircularReveal()方法的參數如下:

簡易布局如下:

首先要計算得出View相對於自身的中心點的X坐標和Y坐標,然後調用Math.hypot()方法計算得出圓形的半徑,接著調用ViewAnimationUtils.createCircularReveal(imageView, ivXCenter, ivYCenter, circleRadius, 0f)創建圓形揭露動畫,增加動畫執行的Listener,在動畫執行結束後調用imageView.setVisibility(View.GONE),最後啟動動畫,示例如下:

使用圓形揭露動畫顯示View,先計算得出View相對於自身的中心點的X坐標和Y坐標,然後算出圓形的半徑,接著創建圓形揭露動畫,此時的起始半徑是0f,結束半徑是圓形的半徑,調用imageView.setVisibility(View.VISIBLE),最後啟動動畫,示例如下:

使用圓形揭露動畫隱藏或顯示View,主要是計算出View相對於自身的中心點的X坐標和Y坐標,並計算出圓形半徑,在調用ViewAnimationUtils.createCircularReveal()方法創建的時候要注意起始半徑和結束半徑的填寫,隱藏View的時候在動畫執行完畢後setVisibility()方法隱藏,顯示View的時候,在動畫啟動前調用setVisibility()方法顯示。

H. Android 動畫效果為:月亮沿著橢圓形軌跡運行

我這里有個android的動畫效果集合

包括Activity轉跳,控制項,listview等等的動畫源代碼

你可以下載來看看


I. android 自定義圓怎麼動態設置圓的半徑

使用ObjectAnimation,或者ValueAnimation都可以實現這個效果
ObjectAnimation可以動態設置控制項有get與set方法的屬性進行改變,給你的圓添加一個半徑的屬性,並添加get與set。

參考
//通過AnimatiorSet來設計同步執行的多個屬性動畫
ObjectAnimator animator1 = ObjectAnimator.ofFloat(imageView, "translationX", 0F, 360F);//X軸平移旋轉
ObjectAnimator animator2 = ObjectAnimator.ofFloat(imageView, "translationY", 0F, 360F);//Y軸平移旋轉
ObjectAnimator animator3 = ObjectAnimator.ofFloat(imageView, "rotation", 0F, 360F);//360度旋轉
AnimatorSet set = new AnimatorSet();
//set.playSequentially(animator1, animator2, animator3);//分步執行
//set.playTogether(animator1, animator2, animator3);//同步執行

//屬性動畫的執行順序控制
// 先同步執行動畫animator2和animator3,然後再執行animator1
set.play(animator3).with(animator1);
set.play(animator2).after(animator3);

set.setDuration(1000);
set.start();

2.通過ValueAnimation來動態改變圓半徑的屬性
參考
public void onClick(View view) {
final ValueAnimator animator = ValueAnimator.ofInt(1, 100);
animator.setDuration(5000);
animator.setInterpolator(new LinearInterpolator());//線性效果變化
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
Integer integer = (Integer) animator.getAnimatedValue();
button.setText("" + integer);
}
});
animator.start();
}

J. Android 中的動畫有哪幾類,它們的特點和區別是什麼

Android 中的動畫有三類,它們的特點和區別如下:
###幀動畫
> 一張張圖片不斷的切換,形成動畫效果
* 在drawable目錄下定義xml文件,子節點為animation-list,在這里定義要顯示的圖片和每張圖片的顯示時長
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false">
<item android:drawable="@drawable/g1" android:ration="200" />
<item android:drawable="@drawable/g2" android:ration="200" />
<item android:drawable="@drawable/g3" android:ration="200" />
</animation-list>
* 在屏幕上播放幀動畫
ImageView iv = (ImageView) findViewById(R.id.iv);
//把動畫文件設置為imageView的背景
iv.setBackgroundResource(R.drawable.animations);
AnimationDrawable ad = (AnimationDrawable) iv.getBackground();
//播放動畫
ad.start();
###補間動畫
* 原形態變成新形態時為了過渡變形過程,生成的動畫就叫補間動畫
* 位移、旋轉、縮放、透明
#####位移:
* 參數10指的是X的起點坐標,但不是指屏幕x坐標為10的位置,而是imageview的 真實X + 10
* 參數150指的是X的終點坐標,它的值是imageview的 真實X + 150
//創建為位移動畫對象,設置動畫的初始位置和結束位置
TranslateAnimation ta = new TranslateAnimation(10, 150, 20, 140);
* x坐標的起點位置,如果相對於自己,傳0.5f,那麼起點坐標就是 真實X + 0.5 * iv寬度
* x坐標的終點位置,如果傳入2,那麼終點坐標就是 真實X + 2 * iv的寬度
* y坐標的起點位置,如果傳入0.5f,那麼起點坐標就是 真實Y + 0.5 * iv高度
* y坐標的終點位置,如果傳入2,那麼終點坐標就是 真實Y + 2 * iv高度

TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 2, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 2)
* 動畫播放相關的設置

//設置動畫持續時間
ta.setDuration(2000);
//動畫重復播放的次數
ta.setRepeatCount(1);
//動畫重復播放的模式
ta.setRepeatMode(Animation.REVERSE);
//動畫播放完畢後,組件停留在動畫結束的位置上
ta.setFillAfter(true);
//播放動畫
iv.startAnimation(ta);
#####縮放:
* 參數0.1f表示動畫的起始寬度是真實寬度的0.1倍
* 參數4表示動畫的結束寬度是真實寬度的4倍
* 縮放的中心點在iv左上角

ScaleAnimation sa = new ScaleAnimation(0.1f, 4, 0.1f, 4);
* 參數0.1f和4意義與上面相同
* 改變縮放的中心點:傳入的兩個0.5f,類型都是相對於自己,這兩個參數改變了縮放的中心點
* 中心點x坐標 = 真實X + 0.5 * iv寬度
* 中心點Y坐標 = 真實Y + 0.5 * iv高度

ScaleAnimation sa = new ScaleAnimation(0.1f, 4, 0.1f, 4, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
#####透明:
* 0為完全透明,1為完全不透明

AlphaAnimation aa = new AlphaAnimation(0, 0.5f);

#####旋轉:
* 20表示動畫開始時的iv的角度
* 360表示動畫結束時iv的角度
* 默認旋轉的圓心在iv左上角

RotateAnimation ra = new RotateAnimation(20, 360);
* 20,360的意義和上面一樣
* 指定圓心坐標,相對於自己,值傳入0.5,那麼圓心的x坐標:真實X + iv寬度 * 0.5
* 圓心的Y坐標:真實Y + iv高度 * 0.5

RotateAnimation ra = new RotateAnimation(20, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
#####所有動畫一起飛

//創建動畫集合
AnimationSet set = new AnimationSet(false);
//往集合中添加動畫
set.addAnimation(aa);
set.addAnimation(sa);
set.addAnimation(ra);
iv.startAnimation(set);

---
#屬性動畫
* 補間動畫,只是一個動畫效果,組件其實還在原來的位置上,xy沒有改變
###位移:
* 第一個參數target指定要顯示動畫的組件
* 第二個參數propertyName指定要改變組件的哪個屬性
* 第三個參數values是可變參數,就是賦予屬性的新的值
* 傳入0,代表x起始坐標:當前x + 0
* 傳入100,代表x終點坐標:當前x + 100

//具有get、set方法的成員變數就稱為屬性
ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "translationX", 0, 100) ;

###縮放:
* 第三個參數指定縮放的比例
* 0.1是從原本高度的十分之一開始
* 2是到原本高度的2倍結束

ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "scaleY", 0.1f, 2);
###透明:
* 透明度,0是完全透明,1是完全不透明

ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "alpha", 0.1f, 1);
###旋轉
* rotation指定是順時針旋轉
* 20是起始角度
* 270是結束角度

ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "rotation", 20, 270);
* 屬性指定為rotationX是豎直翻轉
* 屬性指定為rotationY是水平翻轉

ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "rotationY", 20, 180);

###可變參數
* 第三個參數可變參數可以傳入多個參數,可以實現往回位移(旋轉、縮放、透明)

ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "translationX", 0, 70, 30, 100) ;

熱點內容
linuxgrepor 發布:2025-05-07 20:41:11 瀏覽:507
王者榮耀qq職業為什麼都在安卓 發布:2025-05-07 20:39:28 瀏覽:118
如何根據客戶部件配置伺服器 發布:2025-05-07 20:25:05 瀏覽:631
現在的網路攝像機的密碼是多少 發布:2025-05-07 20:10:24 瀏覽:771
一個ip是幾台伺服器 發布:2025-05-07 19:45:09 瀏覽:963
phpftp獲取 發布:2025-05-07 19:44:08 瀏覽:677
天龍什麼腳本免費 發布:2025-05-07 19:17:15 瀏覽:841
安卓手機如何下載素材 發布:2025-05-07 19:16:30 瀏覽:840
外星人筆記本最高配置是哪個 發布:2025-05-07 19:13:38 瀏覽:554
靜態編譯器使用方法 發布:2025-05-07 19:13:31 瀏覽:422