interpolatorandroid
Ⅰ android 是怎麼通過在XML中配置的控制項屬性得到具體的view對象
我可能表達的不是很清楚,那就拿個具體的例子來說明吧
比如說,在Activity中我們需要用到一個ProgressBar控制項,我們一般先在layout下的main.xml中進行配置
Xml代碼
<ProgressBar
android:id="@+id/pb1"
android:layout_width="fill_parent"
android:layout_height="20dip"
<span style="color: #ff0000;"> android:indeterminateOnly="false"</span>
android:layout_gravity="center_vertical"
android:progressDrawable="@android:drawable/progress_horizontal"
android:indeterminateDrawable="@android:drawable/progress_indeterminate_horizontal"
android:minHeight="20dip"
android:maxHeight="20dip"
/>
我們看 android:indeterminateOnly="false" 這行代碼的
一般我們如果要將在代碼中創建一個ProgressBar,但是不通過配置文件得到。
代碼如下:
java代碼
ProgressBar mProgressBar=new ProgressBar(context);
<span style="color: #ff0000;">mProgressBar.setIndeterminate(false); </span>
mProgressBar.setProgressDrawable(getResources().getDrawable
(android.R.drawable.progress_horizontal));
mProgressBar.setIndeterminateDrawable(getResources().getDrawable
(android.R.drawable.progress_indeterminate_horizontal));
本來我們是希望創建一個普通的能顯示進度的橫條ProgressBar
但是我們發現progressBar中的進度無法更新。
我們來看進度更新的源代碼setProgress():
Java代碼
@android.view.RemotableViewMethod
synchronized void setProgress(int progress, boolean fromUser) {
<span style="color: #ff0000;"> if (mIndeterminate) {
return;
}</span>
if (progress < 0) {
progress = 0;
}
if (progress > mMax) {
progress = mMax;
}
if (progress != mProgress) {
mProgress = progress;
refreshProgress(R.id.progress, mProgress, fromUser);
}
}
關鍵是: if (mIndeterminate) { return; }
原來mIndeterminate 的值為true的話,函數直接返回了,也就是我們不能設置progressBar的進度(mProgress)
現在我們的任務是將mIndeterminate 屬性設置為false
但是我們發現 mProgressBar.setIndeterminate(false); 這行代碼並沒有設置mIndeterminate 屬性為false
我們看ProgressBar中的源代碼:
Java代碼
@android.view.RemotableViewMethod
public synchronized void setIndeterminate(boolean indeterminate) {
<span style="color: #ff0000;"> if ((!mOnlyIndeterminate || !mIndeterminate) && indeterminate != mIndeterminate) {</span>
mIndeterminate = indeterminate;
if (indeterminate) {
// swap between indeterminate and regular backgrounds
mCurrentDrawable = mIndeterminateDrawable;
startAnimation();
} else {
mCurrentDrawable = mProgressDrawable;
stopAnimation();
}
}
}
看這行代碼: if ((!mOnlyIndeterminate || !mIndeterminate) && indeterminate != mIndeterminate) {
我們發現當mOnlyIndeterminate 和mIndeterminate 之前都為true時,我們並不能將mIndeterminate 從true改變為false
google後,有人通過反射機制將ProgressBar中的mOnlyIndeterminate 設置為false(具體請看:關於使用代碼來創建ProgressBar )
我現在我就在想 既然 ProgressBar中的mOnlyIndeterminate 和mIndeterminate屬性都是private,而且都不能通過get和set方法來對其進行操作,那麼 android 通過在XML中配置的控制項屬性,是怎麼被轉換成真正的java類呢?
求大神解惑!
問題補充
over140 寫道
你應該看一下他源碼里關於這個的構造函數部分的代碼,注意父類裡面可能也有代碼,他並不是轉換成java類,只是讀取從XML屬性讀取想要的參數。
你指的是ProgressBar類中的構造函數嗎? 我之前就仔細看了這些東西
ProgressBar中構造函數源碼:
Java代碼
/**
* Create a new progress bar with range 0...100 and initial progress of 0.
* @param context the application environment
*/
public ProgressBar(Context context) {
this(context, null);
}
public ProgressBar(Context context, AttributeSet attrs) {
this(context, attrs, com.android.internal.R.attr.progressBarStyle);
}
public ProgressBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mUiThreadId = Thread.currentThread().getId();
initProgressBar();
TypedArray a =
context.obtainStyledAttributes(attrs, R.styleable.ProgressBar, defStyle, 0);
mNoInvalidate = true;
Drawable drawable = a.getDrawable(R.styleable.ProgressBar_progressDrawable);
if (drawable != null) {
drawable = tileify(drawable, false);
setProgressDrawable(drawable);
}
mDuration = a.getInt(R.styleable.ProgressBar_indeterminateDuration, mDuration);
mMinWidth = a.getDimensionPixelSize(R.styleable.ProgressBar_minWidth, mMinWidth);
mMaxWidth = a.getDimensionPixelSize(R.styleable.ProgressBar_maxWidth, mMaxWidth);
mMinHeight = a.getDimensionPixelSize(R.styleable.ProgressBar_minHeight, mMinHeight);
mMaxHeight = a.getDimensionPixelSize(R.styleable.ProgressBar_maxHeight, mMaxHeight);
mBehavior = a.getInt(R.styleable.ProgressBar_indeterminateBehavior, mBehavior);
final int resID = a.getResourceId(
com.android.internal.R.styleable.ProgressBar_interpolator,
android.R.anim.linear_interpolator); // default to linear interpolator
if (resID > 0) {
setInterpolator(context, resID);
}
setMax(a.getInt(R.styleable.ProgressBar_max, mMax));
setProgress(a.getInt(R.styleable.ProgressBar_progress, mProgress));
setSecondaryProgress(
a.getInt(R.styleable.ProgressBar_secondaryProgress, mSecondaryProgress));
drawable = a.getDrawable(R.styleable.ProgressBar_indeterminateDrawable);
if (drawable != null) {
drawable = tileifyIndeterminate(drawable);
setIndeterminateDrawable(drawable);
}
mOnlyIndeterminate = a.getBoolean(
R.styleable.ProgressBar_indeterminateOnly, mOnlyIndeterminate);
mNoInvalidate = false;
setIndeterminate(mOnlyIndeterminate || a.getBoolean(
R.styleable.ProgressBar_indeterminate, mIndeterminate));
a.recycle();
}
看以上代碼,發現ProgressBar類並沒有使用其父類的構造方法, 它的三個構造方法最終都需要進入到第三個構造方法內,
再看這一句
Java代碼
initProgressBar();
其中具體源碼如下:
Java代碼
private void initProgressBar() {
mMax = 100;
mProgress = 0;
mSecondaryProgress = 0;
mIndeterminate = false;
mOnlyIndeterminate = false;
mDuration = 4000;
mBehavior = AlphaAnimation.RESTART;
mMinWidth = 24;
mMaxWidth = 48;
mMinHeight = 24;
mMaxHeight = 48;
}
看 這兩句:
Java代碼
mIndeterminate = false;
mOnlyIndeterminate = false;
奇怪 在這個初始化ProgressBar的過程中,明明將mIndeterminate和mOnlyIndeterminate屬性設置為false
但是我們初始化後的進度條,顯示後同樣是不會進度的(圖如下)
也就是說 mIndeterminate屬性依然是true(具體請看ProgressBar中的setProgress()方法)
繼續看構造方法中的其他內容時,發現關鍵所在:
Java代碼
mOnlyIndeterminate = a.getBoolean(
R.styleable.ProgressBar_indeterminateOnly, mOnlyIndeterminate);
mNoInvalidate = false;
setIndeterminate(mOnlyIndeterminate || a.getBoolean(
R.styleable.ProgressBar_indeterminate, mIndeterminate));
這兩句將mOnlydeterminate和mIndeterminate屬性都設置為true,
看到這里,發現這兩句都用到 a 這個對象,a 具體是什麼呢?
Java代碼
TypedArray a =
context.obtainStyledAttributes(attrs, R.styleable.ProgressBar, defStyle, 0);
那我們能不能通過改變傳入 構造函數 的參數 來使 a 發生相應的變化,進而使mOnlyIndeterminate和mIndeterminate屬性設置為 true 呢?
再往下深入,發現看的我那是一頭霧水,完全找不到方向,所以只得放棄這個猜想。
到最後,我發現 如果要通過正常的java訪問機制(當然排除使用反射機制),來操作mOnlyIndeterminate和mIndeterminate屬性是不可能的,
你說: 他並不是轉換成java類,只是讀取從XML屬性讀取想要的參數。
我們一般是通過findViewById()來根據layout中的XML文件設置的控制項屬性得到 具體的View控制項對象(關於轉換成java類,我錯了,其實我想說的是怎麼得到的這個對象),
這個過程是不是也是通過反射機制來完成的?假如是的話,能給我提供些具體過程的資料呢?
Ⅱ android 如何讓控制項慢慢展開
Android為了用戶獲得更好的體驗,引入了動畫的概念,有逐禎的方式,所以為了讓控制項展開,可以利用Anima這個類提供的方法,可以參考這位前輩的方式,代碼如下:
注釋已經很清楚了,在普及以下android動畫的概念:
Tween Animation有四種形式:
1.漸變透明度動畫效果。
2.漸變尺寸伸縮動畫效果。
3.畫面位置移動動畫效果。
4.畫面旋轉動畫效果。
這四種動畫實現方式都是通過Animation類和AnimationUtils配合實現。
可以通過xml實現:動畫的XML文件在工程中res/anim目錄。還有一種就是我上面所說的逐禎動畫了,具體的用法可以再網路一下,有很多資料可以參考。
Ⅲ android中怎麼定義旋轉動畫的旋轉速度
android源代碼之Rotate旋轉動畫
標簽為旋轉節點
Tween一共為我們提供了3種動畫渲染模式。
android:interpolator="@android:anim/accelerate_interpolator" 設置動畫渲染器為加速動畫(動畫播放中越來越快)
android:interpolator="@android:anim/decelerate_interpolator" 設置動畫渲染器為減速動畫(動畫播放中越來越慢)
android:interpolator="@android:anim/accelerate_decelerate_interpolator" 設置動畫渲染器為先加速在減速(開始速度最快 逐漸減慢)
如果不寫的話 默認為勻速運動
android:fromDegrees="+360"設置動畫開始的角度
android:toDegrees="0"設置動畫結束的角度
這個動畫布局設置動畫將向左做360度旋轉加速運動。
android:interpolator="@android:anim/accelerate_interpolator"
android:fromDegrees="+360"
android:toDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:ration="2000"
/>
復制代碼
代碼實現
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
public class RotateActivity extends Activity {
/**向左旋轉動畫按鈕**/
Button mButton0 = null;
/**向右旋轉動畫按鈕**/
Button mButton1 = null;
/**顯示動畫的ImageView**/
ImageView mImageView = null;
/**向左旋轉動畫**/
Animation mLeftAnimation = null;
/**向右旋轉動畫**/
Animation mRightAnimation = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.retate);
/**拿到ImageView對象**/
mImageView = (ImageView)findViewById(R.id.imageView);
/**載入向左與向右旋轉動畫**/
mLeftAnimation = AnimationUtils.loadAnimation(this, R.anim.retateleft);
mRightAnimation = AnimationUtils.loadAnimation(this, R.anim.retateright);
mButton0 = (Button)findViewById(R.id.button0);
mButton0.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
/**播放向左旋轉動畫**/
mImageView.startAnimation(mLeftAnimation);
}
});
mButton1 = (Button)findViewById(R.id.button1);
mButton1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
/**播放向右旋轉動畫**/
mImageView.startAnimation(mRightAnimation);
}
});
}
}
Ⅳ 如何實現Rotate旋轉動畫的android源代碼
android源代碼之Rotate旋轉動畫
標簽為旋轉節點
Tween一共為我們提供了3種動畫渲染模式。
android:interpolator="@android:anim/accelerate_interpolator" 設置動畫渲染器為加速動畫(動畫播放中越來越快)
android:interpolator="@android:anim/decelerate_interpolator" 設置動畫渲染器為減速動畫(動畫播放中越來越慢)
android:interpolator="@android:anim/accelerate_decelerate_interpolator" 設置動畫渲染器為先加速在減速(開始速度最快 逐漸減慢)
如果不寫的話 默認為勻速運動
android:fromDegrees="+360"設置動畫開始的角度
android:toDegrees="0"設置動畫結束的角度
這個動畫布局設置動畫將向左做360度旋轉加速運動。
android:interpolator="@android:anim/accelerate_interpolator"
android:fromDegrees="+360"
android:toDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:ration="2000"
/>
復制代碼
代碼實現
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
public class RotateActivity extends Activity {
/**向左旋轉動畫按鈕**/
Button mButton0 = null;
/**向右旋轉動畫按鈕**/
Button mButton1 = null;
/**顯示動畫的ImageView**/
ImageView mImageView = null;
/**向左旋轉動畫**/
Animation mLeftAnimation = null;
/**向右旋轉動畫**/
Animation mRightAnimation = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.retate);
/**拿到ImageView對象**/
mImageView = (ImageView)findViewById(R.id.imageView);
/**載入向左與向右旋轉動畫**/
mLeftAnimation = AnimationUtils.loadAnimation(this, R.anim.retateleft);
mRightAnimation = AnimationUtils.loadAnimation(this, R.anim.retateright);
mButton0 = (Button)findViewById(R.id.button0);
mButton0.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
/**播放向左旋轉動畫**/
mImageView.startAnimation(mLeftAnimation);
}
});
mButton1 = (Button)findViewById(R.id.button1);
mButton1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
/**播放向右旋轉動畫**/
mImageView.startAnimation(mRightAnimation);
}
});
}
}
學習更多關於android源代碼,可以查詢
Ⅳ 請教高手,android 頁面跳轉 屏幕向左或者向右移動出現 ,急、急。。。謝謝了。
在跳轉到頁面前的startActivity()方法後調用
overridePendingTransition(R.anim.activity_open_enter, 0);方法
在返回頁面前的finish()方法後調用
overridePendingTransition(0, R.anim.activity_close_exit);方法
就可以了
其中activity_open_enter內容為:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@anim/decelerate_interpolator">
<translate android:fromXDelta="33%" android:toXDelta="0"
android:ration="@android:integer/config_shortAnimTime"/>
</set>
activity_close_exit內容為:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@anim/decelerate_interpolator">
<translate android:fromXDelta="0%" android:toXDelta="33%"
android:ration="@android:integer/config_shortAnimTime"/>
</set>
不過要注意overridePendingTransition是API Level5中的方法,也就是說2。01之前的sdk中都不可用
Ⅵ 安卓編程 如何實現點擊才滑動
intent.setClass(First.this,Second.class);
startActivity(intent);
// 添加Activity間的跳轉動畫
overridePendingTransition(R.anim.anim_in, R.anim.anim_out);
anim.anim_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:Android="http://schemas.android.com/apk/res/android"
Android:interpolator="@android:anim/decelerate_interpolator" >
<scale
Android:ration="2000"
Android:fromXScale="2.0"
Android:fromYScale="2.0"
Android:pivotX="50%"
Android:pivotY="50%"
Android:toXScale="1.0"
Android:toYScale="1.0" />
</set>
anim.anim_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:Android="http://schemas.android.com/apk/res/android"
Android:interpolator="@android:anim/decelerate_interpolator"
Android:zAdjustment="top" >
<scale
Android:ration="2000"
Android:fromXScale="0.5"
Android:fromYScale="0.5"
Android:pivotX="50%p"
Android:pivotY="50%p"
Android:toXScale="2.0"
Android:toYScale="2.0" />
<alpha
Android:ration="2000"
Android:fromAlpha="1.0"
Android:toAlpha="0" />
</set>
代碼不會給你,長不長的不說,主要是不讓給。思路其實很簡單,就是響應屏幕觸點事件,判斷觸點區域,切換界面圖片。最簡單的實現:6張圖,分別是一張觸摸圖(就是你圖中那個黃邊的鎖),一張區域整體透明圖,四張上下左右響應變化整體圖,然後根據觸摸的位置切換圖片就行了。(其中還是有些小麻煩和小技巧,你自己試試就知道了,不難解決)。
Ⅶ 如何設定Android Activity間切換時的動畫
轉載本代碼示例介紹如何設定Activity間切換時的動畫效果。本示例使用Eclipse的Android工程編譯測試。
1. 定義清單文件(AndroidManifest.xml)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http。//schemas。android。com/apk/res/android"
package="my.android.test"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Animation"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Controls1"
android:label="@string/app_name"
android:theme="@android:style/Theme.Light">
</activity>
</application>
<uses-sdk android:minSdkVersion="9" />
</manifest>
2. 定義字元串資源(res/values/strings.xml)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World,Aniation!</string>
<string name="app_name">Animation</string>
<string name="activity_animation_msg">Press a button to launch an activity with a custom animation.</string>
<string name="activity_animation_fade">Fade in</string>
<string name="activity_animation_zoom">Zoom in</string>
<string name="controls_1_save">Save</string>
<string name="controls_1_checkbox_1">Checkbox 1</string>
<string name="controls_1_checkbox_2">Checkbox 2</string>
<string name="controls_1_radiobutton_1">RadioButton 1</string>
<string name="controls_1_radiobutton_2">RadioButton 2</string>
<string name="controls_1_star">Star</string>
<string name="textColorPrimary">textColorPrimary</string>
<string name="textColorSecondary">textColorSecondary</string>
<string name="textColorTertiary">textColorTertiary</string>
<string name="listSeparatorTextViewStyle">listSeparatorTextViewStyle</string>
</resources>
3. 定義漸入動畫資源定義(res/anim/fade.xml、res/anim/hold.xml)
res/anim/fade.xml文件
<?xml version="1.0" encoding="utf-8"?>
<!-- 聲明動畫對象的透明度,本例使用漸入的方式,顯示Activity
屬性說明參照zoom_exit.xml -->
<alpha xmlns:android="http。//schemas。android。com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromAlpha="0.0" android:toAlpha="1.0"
android:ration="@android:integer/config_longAnimTime" />
res/anim/hold.xml文件
<?xml version="1.0" encoding="utf-8"?>
<!-- 聲明動畫對象的水平和垂直移動量,本例使用水平移動方式,
android:interpolator:指定在設定時間內動畫移動過程中插補器,用於改善動畫的平滑度
android:fromXDelta:指定動畫開始時,動畫對象的水平位置,可以用像素智設定,也可以用
相對父窗口寬度的百分比來設定。
android:toXDelta:指定動畫結束時,動畫對象的水平位置,可以用像素值來設定,也可以用
相對父窗口寬度的百分比來設定。
android:ration:指定動畫的播放時間
-->
<translate xmlns:android="http。//schemas。android。com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromXDelta="0" android:toXDelta="0"
android:ration="@android:integer/config_longAnimTime" />
4. 定義縮放動畫資源定義(res/anim/zoom_enter.xml、res/anim/zoom_exit.xml)
res/anim/zoom_enter.xml文件
<?xml version="1.0" encoding="utf-8"?>
<!-- 聲明動畫對象進入屏幕時的動畫資源
android:interpolator:指定在設定時間內動畫移動過程中插補器,用於改善動畫的平滑度
-->
<set xmlns:android="http。//schemas。android。com/apk/res/android"
android:interpolator="@android:anim/decelerate_interpolator">
<!-- 聲明動畫對象進入屏幕時的縮放動畫,
屬性說明參照zoom_exit.xml -->
<scale android:fromXScale="2.0" android:toXScale="1.0"
android:fromYScale="2.0" android:toYScale="1.0"
android:pivotX="50%p" android:pivotY="50%p"
android:ration="@android:integer/config_mediumAnimTime" />
</set>
res/anim/zoom_exit.xml文件
<?xml version="1.0" encoding="utf-8"?>
<!-- 聲明Activity退出時使用的動畫資源
android:interpolator:指定在設定時間內動畫移動過程中插補器,用於改善動畫的平滑度
android:zAdjustment:允許再動畫播放期間,調整播放內容在Z軸方向的順序,normal(0):真正播放的
動畫內容保持當前的Z軸順序,top(1):在動畫播放期間,強制把當前播放的內容放到其他內容的上面;
bottom(-1):在動畫播放期間,強制把當前播放的內容放到其他內容之下。
-->
<set xmlns:android="http。//schemas。android。com/apk/res/android"
android:interpolator="@android:anim/decelerate_interpolator"
android:zAdjustment="top">
<!-- 指定動畫對象的縮放因子和播放時間
android:fromXScale和android:toXScale指定X軸的動畫開始和結束時的縮放因子
android:fromYScale和android:toYScale指定Y軸的動畫開始和結束時的縮放因子
android:pivoteX:在動畫對象被縮放時,X軸要保留的原始尺寸的百分比。
android:pivoteY:在動畫對象被縮放時,Y軸要保留的原始尺寸的百分比。
android:ration指定動畫的播放時間
-->
<scale android:fromXScale="1.0" android:toXScale=".5"
android:fromYScale="1.0" android:toYScale=".5"
android:pivotX="50%p" android:pivotY="50%p"
android:ration="@android:integer/config_mediumAnimTime" />
<!-- 定義動畫對象的透明度,該動畫在動畫縮放之後播放。
android:fromAlpha:指定動畫初始時的透明度
android:toAlpha:指定動畫結束時的透明度
android:ratiion:指定動畫透明處理的執行時間
-->
<alpha android:fromAlpha="1.0" android:toAlpha="0"
android:ration="@android:integer/config_mediumAnimTime"/>
</set>
Ⅷ android開發中如何旋轉布局
樓主你好,這個可以通過動畫來達到這個效果的,代碼如下:
只要把您的layout對象傳進去就行了
public void showAnimation(View mView)
{
final float centerX = mView.getWidth() / 2.0f;
final float centerY = mView.getHeight() / 2.0f;
//這個是設置需要旋轉的角度,我設置的是180度
RotateAnimation rotateAnimation = new RotateAnimation(0, 180, centerX,
centerY);
//這個是設置通話時間的
rotateAnimation.setDuration(1000*3);
rotateAnimation.setFillAfter(true);
mView.startAnimation(rotateAnimation);
}
Ⅸ 安卓開發旋轉動畫實現的時候為什麼會刷新界面
Android 平台提供了兩類動畫,一類是 Tween 動畫,即通過對場景里的對象不斷做圖像變換(平移、縮放、旋轉)產生動畫效果;第二類是 Frame 動畫,即順序播放事先做好的圖像,跟電影類似。本文分析 Tween動畫的rotate實現旋轉效果。 在新浪微博客戶端中各個操作進行中時activity的右上角都會有個不停旋轉的圖標,類似刷新的效果,給用戶以操作中的提示。這種非模態的提示方式推薦使用,那麼下面就分享下如何實現這種效果吧 1、定義一個ImageView 定義一個ImageView是為了裝載圖片,其中的圖片將被rotate用來進行旋轉,其他View亦可。 資源文件為 Java代碼 xmlns:android="schemas/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> android:id="@+id/infoOperating" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/operating" android:scaleType="center"> 其中的android:src為圖片內容,可使用附件中的圖片。 java代碼為 Java代碼 ImageView infoOperatingIV = (ImageView)findViewById(R.id.infoOperating); 2、定義rotate旋轉效果 在res/anim文件夾下新建tip.xml文件,內容如下 Java代碼 android:fromDegrees="0" android:toDegrees="359" android:ration="500" android:repeatCount="-1" android:pivotX="50%" android:pivotY="50%" /> 含義表示從0到359度開始循環旋轉,0-359(若設置成360在停止時會出現停頓現象)度旋轉所用時間為500ms,旋轉中心距離view的左頂點為50%距離,距離view的上邊緣為50%距離,即正中心,具體每個含義見下面的具體屬性介紹。 java代碼為 Java代碼 Animation operatingAnim = AnimationUtils.loadAnimation(this, R.anim.tip); LinearInterpolator lin = new LinearInterpolator(); operatingAnim.setInterpolator(lin); setInterpolator表示設置旋轉速率。LinearInterpolator為勻速效果,Accelerateinterpolator為加速效果、DecelerateInterpolator為減速效果,具體可見下面android:interpolator的介紹。 a. 關於其中的屬性意義如下(紅色部分加以注意): android:fromDegrees 起始的角度度數 android:toDegrees 結束的角度度數,負數表示逆時針,正數表示順時針。如10圈則比android:fromDegrees大3600即可 android:pivotX 旋轉中心的X坐標 浮點數或是百分比。浮點數表示相對於Object的左邊緣,如5; 百分比表示相對於Object的左邊緣,如5%; 另一種百分比表示相對於父容器的左邊緣,如5%p; 一般設置為50%表示在Object中心 android:pivotY 旋轉中心的Y坐標 浮點數或是百分比。浮點數表示相對於Object的上邊緣,如5; 百分比表示相對於Object的上邊緣,如5%; 另一種百分比表示相對於父容器的上邊緣,如5%p; 一般設置為50%表示在Object中心 android:ration 表示從android:fromDegrees轉動到android:toDegrees所花費的時間,單位為毫秒。可以用來計算速度。 android:interpolator表示變化率,但不是運行速度。一個插補屬性,可以將動畫效果設置為加速,減速,反復,反彈等。默認為開始和結束慢中間快, android:startOffset 在調用start函數之後等待開始運行的時間,單位為毫秒,若為10,表示10ms後開始運行 android:repeatCount 重復的次數,默認為0,必須是int,可以為-1表示不停止 android:repeatMode 重復的模式,默認為restart,即重頭開始重新運行,可以為reverse即從結束開始向前重新運行。在android:repeatCount大於0或為infinite時生效 android:detachWallpaper 表示是否在壁紙上運行 android:zAdjustment 表示被animated的內容在運行時在z軸上的位置,默認為normal。 normal保持內容當前的z軸順序 top運行時在最頂層顯示 bottom運行時在最底層顯示 b. 運行速度 運行速度為運行時間(android:ration)除以運行角度差(android:toDegrees-android:fromDegrees),比如android:ration為1000,android:toDegrees為360,android:fromDegrees為0就表示1秒轉1圈。 c. 循環運行 Java代碼 android:fromDegrees="0" android:toDegrees="360" android:repeatCount="-1" android:repeatCount="-1"即表示循環運行,配合上android:fromDegrees="0" android:toDegrees="360"表示不間斷 3、開始和停止旋轉 在操作開始之前調用 Java代碼 if (operatingAnim != null) { infoOperatingIV.startAnimation(operatingAnim); } 在操作完成時調用 Java代碼 infoOperatingIV.clearAnimation(); 許多朋友不知道如何停止旋轉animation,所以強制設置rotate轉動多少圈表示操作,但卻無法與操作實際的進度匹配上,實際上只要如上代碼所示清除animation即可。 其他: 對於上面的轉動在橫屏(被設置為了不重繪activity)時會出現問題,即旋轉中心偏移,導致動畫旋轉偏離原旋轉中心。解決如下 Java代碼 @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); if (operatingAnim != null && infoOperatingIV != null && operatingAnim.hasStarted()) { infoOperatingIV.clearAnimation(); infoOperatingIV.startAnimation(operatingAnim); } }
Ⅹ android 怎麼實現沿控制項方向做位移動畫
在Android開發,我們會經常使用到位移動畫,一般情況下位移動畫有兩種實現方式,一種是直接通過Java代碼去實現,另外一種是通過配置文件實現動畫,下面是兩種動畫的基本是使用方法:
純Java代碼實現:
[java] view plain
//創建漸變動畫
Animation animation = new TranslateAnimation(0, 0, 300, 300);
animation.setDuration(1500);
animation.setRepeatCount(1);//動畫的重復次數
animation.setFillAfter(true);//設置為true,動畫轉化結束後被應用
imageView1.startAnimation(animation);//開始動畫
通過配置文件實現:
1、首先要在res目錄下建立一個anim文件,在anim建立一個alpha1.xml文件如下:
[java] view plain
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:ration="1500"
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="200"
android:toYDelta="300"
android:repeatCount="3"
android:interpolator="@android:anim/cycle_interpolator"
android:repeatMode="reverse"
/>
</set>
2、載入動畫
[java] view plain
Animation animation = AnimationUtils.loadAnimation(this, R.anim.translate);
imageView1.startAnimation(animation);//開始動畫
案例下載地址:http://download.csdn.net/detail/u013043346/9374204