您的位置:首页 > 移动开发 > Android开发

android 旋转按钮和旋转变化动画

2012-08-30 11:08 411 查看
RotateAnimation类:旋转变化动画类

RotateAnimation类是Android系统中的旋转变化动画类,用于控制View对象的旋转动作,该类继承于Animation类。RotateAnimation类中的很多方法都与Animation类一致,该类中最常用的方法便是RotateAnimation构造方法。

【基本语法】public RotateAnimation (float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)

参数说明

fromDegrees:旋转的开始角度。

toDegrees:旋转的结束角度。

pivotXType:X轴的伸缩模式,可以取值为ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。

pivotXValue:X坐标的伸缩值。

pivotYType:Y轴的伸缩模式,可以取值为ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。

pivotYValue:Y坐标的伸缩值。

【实例演示】下面通过代码来演示如何设置一个简单的旋转变化动画效果。

public class firstActivity extends Activity {

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) { //重载onCreate方法

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

final ImageView image=(ImageView)findViewById(R.id.imageView1); //ImageView对象

Button btn1=(Button)findViewById(R.id.button1); //按钮对象

Button btn2=(Button)findViewById(R.id.button2);

final Animation rotateAnimation = new

RotateAnimation(0f,360f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);

//设置旋转变化动画对象

btn1.setOnClickListener(new View.OnClickListener() { //设置监听器

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

rotateAnimation.setDuration(3000); //持续时间

image.setAnimation(rotateAnimation); //设置动画

rotateAnimation.startNow(); //启动动画

}

});

btn2.setOnClickListener(new View.OnClickListener() { //设置监听器

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

rotateAnimation.cancel(); //取消动画执行

}

});

}

}

在这段代码中,首先通过RotateAnimation构造方法创建了一个旋转变化的动画对象。然后,在第一个按钮监听器中设置了动画的持续时间,之后启动该动画。在第二个按钮监听器中取消该动画。读者运行这段代码,将看到图片沿如图9.8所示的方向进行旋转。

RotateAnimation类:旋转变化动画类

RotateAnimation类是Android系统中的旋转变化动画类,用于控制View对象的旋转动作,该类继承于Animation类。RotateAnimation类中的很多方法都与Animation类一致,该类中最常用的方法便是RotateAnimation构造方法。

【基本语法】public RotateAnimation (float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)

参数说明

fromDegrees:旋转的开始角度。

toDegrees:旋转的结束角度。

pivotXType:X轴的伸缩模式,可以取值为ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。

pivotXValue:X坐标的伸缩值。

pivotYType:Y轴的伸缩模式,可以取值为ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。

pivotYValue:Y坐标的伸缩值。

【实例演示】下面通过代码来演示如何设置一个简单的旋转变化动画效果。

public class firstActivity extends Activity {

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) { //重载onCreate方法

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

final ImageView image=(ImageView)findViewById(R.id.imageView1); //ImageView对象

Button btn1=(Button)findViewById(R.id.button1); //按钮对象

Button btn2=(Button)findViewById(R.id.button2);

final Animation rotateAnimation = new

RotateAnimation(0f,360f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);

//设置旋转变化动画对象

btn1.setOnClickListener(new View.OnClickListener() { //设置监听器

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

rotateAnimation.setDuration(3000); //持续时间

image.setAnimation(rotateAnimation); //设置动画

rotateAnimation.startNow(); //启动动画

}

});

btn2.setOnClickListener(new View.OnClickListener() { //设置监听器

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

rotateAnimation.cancel(); //取消动画执行

}

});

}

}

在这段代码中,首先通过RotateAnimation构造方法创建了一个旋转变化的动画对象。然后,在第一个按钮监听器中设置了动画的持续时间,之后启动该动画。在第二个按钮监听器中取消该动画。读者运行这段代码,将看到图片沿如图9.8所示的方向进行旋转。



转自:http://book.51cto.com/art/201204/328298.htm

tip:旋转之后图片如何停留在原来的位置

这个类别做动态按钮之类不太好,不太智能; 做动态按钮的时候可以用android自带的重力传感器来做按钮

mManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mSensor = mManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
hasSurface = false;
inactivityTimer = new InactivityTimer(this);

mListener = new SensorEventListener() {
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}

public void onSensorChanged(SensorEvent event) {
float x = event.values[SensorManager.DATA_X];
float y = event.values[SensorManager.DATA_Y];
float z = event.values[SensorManager.DATA_Z];
Relayout(x, y, z);
}
};

protected void Relayout(float x, float y, float z){
final Resources r = getResources();
Drawable drawable;
if(x > -2.5 && x <= 2.5 && y > 7.5 && y <= 10  ){

drawable = r.getDrawable(R.drawable.buttond2);
ImageButton button_2 = (ImageButton)findViewById(R.id.button3);
button_2.setImageDrawable(drawable);
ImageButton button_3 = (ImageButton)findViewById(R.id.button4);
button_3.setImageDrawable(drawable);
ImageButton button_cancel = (ImageButton)findViewById(R.id.cancel);
drawable = r.getDrawable(R.drawable.cancel2);
button_cancel.setImageDrawable(drawable);

ImageButton button_pause = (ImageButton)findViewById(R.id.pause);
pauseflag = global.GetPauseFlag();
drawable = r.getDrawable(R.drawable.buttond2);
switch(pauseflag){
case 0:
drawable = r.getDrawable(R.drawable.pause2);
break;
case 1:
drawable = r.getDrawable(R.drawable.go2);
break;
}
button_pause.setImageDrawable(drawable);
}
if(x > 2.5 && x <= 7.5 && y > 2.5 && y <= 7.5){
drawable = r.getDrawable(R.drawable.buttond1);
ImageButton button_1 = (ImageButton)findViewById(R.id.button4);
button_1.setImageDrawable(drawable);
ImageButton button_3 = (ImageButton)findViewById(R.id.button3);
button_3.setImageDrawable(drawable);
ImageButton button_cancel = (ImageButton)findViewById(R.id.cancel);
drawable = r.getDrawable(R.drawable.cancel1);
button_cancel.setImageDrawable(drawable);

ImageButton button_pause = (ImageButton)findViewById(R.id.pause);
pauseflag = global.GetPauseFlag();
drawable = r.getDrawable(R.drawable.buttond1);
switch(pauseflag){
case 0:
drawable = r.getDrawable(R.drawable.pause1);
break;
case 1:
drawable = r.getDrawable(R.drawable.go1);
break;
}
button_pause.setImageDrawable(drawable);
}
if(x > 7.5 && x <= 10 && y > -2.5 && y <= 2.5){
drawable = r.getDrawable(R.drawable.buttond);
ImageButton button_1 = (ImageButton)findViewById(R.id.button4);
button_1.setImageDrawable(drawable);
ImageButton button_3 = (ImageButton)findViewById(R.id.button3);
button_3.setImageDrawable(drawable);
ImageButton button_cancel = (ImageButton)findViewById(R.id.cancel);
drawable = r.getDrawable(R.drawable.cancel);
button_cancel.setImageDrawable(drawable);

ImageButton button_pause = (ImageButton)findViewById(R.id.pause);
pauseflag = global.GetPauseFlag();
drawable = r.getDrawable(R.drawable.buttond);
switch(pauseflag){
case 0:
drawable = r.getDrawable(R.drawable.pause);
break;
case 1:
drawable = r.getDrawable(R.drawable.go);
break;
}
button_pause.setImageDrawable(drawable);
}
if(x > 2.5 && x <= 7.5 && y > -7.5 && y <= -2.5){
drawable = r.getDrawable(R.drawable.buttond7);
ImageButton button_1 = (ImageButton)findViewById(R.id.button4);
button_1.setImageDrawable(drawable);
ImageButton button_3 = (ImageButton)findViewById(R.id.button3);
button_3.setImageDrawable(drawable);
ImageButton button_cancel = (ImageButton)findViewById(R.id.cancel);
drawable = r.getDrawable(R.drawable.cancel7);
button_cancel.setImageDrawable(drawable);

ImageButton button_pause = (ImageButton)findViewById(R.id.pause);
pauseflag = global.GetPauseFlag();
drawable = r.getDrawable(R.drawable.buttond7);
switch(pauseflag){
case 0:
drawable = r.getDrawable(R.drawable.pause7);
break;
case 1:
drawable = r.getDrawable(R.drawable.go7);
break;
}
button_pause.setImageDrawable(drawable);
}
if(x > -2.5 && x <= 2.5 && y > -10 && y <= -7.5){
drawable = r.getDrawable(R.drawable.buttond6);
ImageButton button_1 = (ImageButton)findViewById(R.id.button4);
button_1.setImageDrawable(drawable);
ImageButton button_3 = (ImageButton)findViewById(R.id.button3);
button_3.setImageDrawable(drawable);
ImageButton button_cancel = (ImageButton)findViewById(R.id.cancel);
drawable = r.getDrawable(R.drawable.cancel6);
button_cancel.setImageDrawable(drawable);

ImageButton button_pause = (ImageButton)findViewById(R.id.pause);
pauseflag = global.GetPauseFlag();
drawable = r.getDrawable(R.drawable.buttond6);
switch(pauseflag){
case 0:
drawable = r.getDrawable(R.drawable.pause6);
break;
case 1:
drawable = r.getDrawable(R.drawable.go6);
break;
}
button_pause.setImageDrawable(drawable);
}
if((x > -7.5 && x <= -2.5 && y > -7.5 && y <= -2.5)){
drawable = r.getDrawable(R.drawable.buttond5);
ImageButton button_1 = (ImageButton)findViewById(R.id.button4);
button_1.setImageDrawable(drawable);
ImageButton button_3 = (ImageButton)findViewById(R.id.button3);
button_3.setImageDrawable(drawable);
ImageButton button_cancel = (ImageButton)findViewById(R.id.cancel);
drawable = r.getDrawable(R.drawable.cancel5);
button_cancel.setImageDrawable(drawable);

ImageButton button_pause = (ImageButton)findViewById(R.id.pause);
pauseflag = global.GetPauseFlag();
drawable = r.getDrawable(R.drawable.buttond5);
switch(pauseflag){
case 0:
drawable = r.getDrawable(R.drawable.pause5);
break;
case 1:
drawable = r.getDrawable(R.drawable.go5);
break;
}
button_pause.setImageDrawable(drawable);
}
if(x > -10 && x <= -7.5 && y > -2.5 && y < 2.5){
drawable = r.getDrawable(R.drawable.buttond4);
ImageButton button_1 = (ImageButton)findViewById(R.id.button4);
button_1.setImageDrawable(drawable);
ImageButton button_3 = (ImageButton)findViewById(R.id.button3);
button_3.setImageDrawable(drawable);
ImageButton button_cancel = (ImageButton)findViewById(R.id.cancel);
drawable = r.getDrawable(R.drawable.cancel4);
button_cancel.setImageDrawable(drawable);

ImageButton button_pause = (ImageButton)findViewById(R.id.pause);
pauseflag = global.GetPauseFlag();
drawable = r.getDrawable(R.drawable.buttond4);
switch(pauseflag){
case 0:
drawable = r.getDrawable(R.drawable.pause4);
break;
case 1:
drawable = r.getDrawable(R.drawable.go4);
break;
}
button_pause.setImageDrawable(drawable);
}
if(x > -7.5 && x <= -2.5 && y > 2.5 && y <= 7.5 ){
drawable = r.getDrawable(R.drawable.buttond3);
ImageButton button_1 = (ImageButton)findViewById(R.id.button4);
button_1.setImageDrawable(drawable);
ImageButton button_3 = (ImageButton)findViewById(R.id.button3);
button_3.setImageDrawable(drawable);
ImageButton button_cancel = (ImageButton)findViewById(R.id.cancel);
drawable = r.getDrawable(R.drawable.cancel3);
button_cancel.setImageDrawable(drawable);

ImageButton button_pause = (ImageButton)findViewById(R.id.pause);
pauseflag = global.GetPauseFlag();
drawable = r.getDrawable(R.drawable.buttond3);
switch(pauseflag){
case 0:
drawable = r.getDrawable(R.drawable.pause3);
break;
case 1:
drawable = r.getDrawable(R.drawable.go3);
break;
}
button_pause.setImageDrawable(drawable);
}
}


其实这个真的做一般应用的旋转按钮并不好,因为不能控制它在固定角度停止,所以最好的办法,还是使用重力传感器的监听比较好,实现简单,运行也流畅。强烈推荐~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: