我再這里用到了動(dòng)畫(huà)
網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)!專(zhuān)注于網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開(kāi)發(fā)、重慶小程序開(kāi)發(fā)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了紫金免費(fèi)建站歡迎大家使用!
先貼出代碼吧
//layout
android:layout_width="match_parent"
android:layout_height="match_parent" >
android:id="@+id/imgpic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="center"
android:src="@drawable/left" >//圖片可更改
android:id="@+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="141dp"
android:text="ToggleButton" />
//動(dòng)畫(huà)路徑 res/anim/tip.xml
android:fromDegrees="0"
android:toDegrees="359"
android:duration="1000"
android:repeatCount="-1"
android:pivotX="50%"
android:pivotY="50%" />
動(dòng)畫(huà)的參數(shù)設(shè)置:
android:fromDegrees="0" //起始角度
android:toDegrees="359" //結(jié)束的角度度數(shù),負(fù)數(shù)表示逆時(shí)針,正數(shù)表示順時(shí)針。如2圈則比 //android:fromDegrees大720即可
android:duration="1000" //表示從android:fromDegrees轉(zhuǎn)動(dòng)到android:toDegrees所花費(fèi)的時(shí)間, //單位為毫秒。可以用來(lái)計(jì)算速度。
android:repeatCount="-1" //重復(fù)的次數(shù),負(fù)數(shù)表示一直旋轉(zhuǎn)
android:pivotX="50%" //旋轉(zhuǎn)中心的坐標(biāo)
android:pivotY="50%" />
//activity
package com.example.pictween;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.ImageButton;
import android.widget.Toast;
import android.widget.ToggleButton;
public class MainActivity extends Activity {
private Animation operatingAnim;
private ToggleButton togbtn;
private boolean flag = false;
private ImageButton imgbtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
togbtn = (ToggleButton) findViewById(R.id.toggleButton1);
togbtn.setText("關(guān)閉");
imgbtn = (ImageButton) findViewById(R.id.imgpic);
operatingAnim = AnimationUtils.loadAnimation(this, R.anim.tip);
LinearInterpolator lin = new LinearInterpolator();
operatingAnim.setInterpolator(lin);
togbtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
flag = !flag;
if(flag){
togbtn.setText("開(kāi)啟");
startAnimation();
}else{
togbtn.setText("關(guān)閉");
stopAnimation();
}
}
});
}
public void startAnimation(){
if (operatingAnim != null) {
imgbtn.startAnimation(operatingAnim);
}
}
public void stopAnimation(){
imgbtn.clearAnimation();
}
}