`

Android动画处理缩放,平移,旋转

阅读更多

                              在安卓中,定义动画方案有很多种,首先我们由深入浅,之所以要由深入浅是因为提高大家的兴趣,再者是最近计算机图形老师要求的实验,要求使用VS的软件作画,语言要求c++,小编不仅很硬气的选择了java语言,还很霸道的选择了安卓来实现,看得助教一愣一愣的,硬是让我验过了,学有所用就是这样,在学习之前大家可以深入了解一下计算机图形的矩阵思想,这个在之后会将,大家也可以先去了解一下,我们这节课简单的使用已经封装好的简便方法实现上述功能,至于具体算法,之后会讲。

     建立三个Activity分别实现缩放,平移,旋转

这里为了显示清楚,小编采取了分页模式中的ActivityGroup,来使得页面分别显示三个Activity,感兴趣的可以在我博客里找有讲述,觉得实现麻烦的可以利用跳转功能,如果连跳转都觉得麻烦的建议使用方法直接按钮定义也可以。

首先布局文件定义,因为三个xml异曲同工,这里就只列出一个例子:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/image2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="Translate"
        android:src="@drawable/jump" />

</LinearLayout>

 上述绑定了监听方法,实现不同功能需要你定义不同的监听方法,这里需要你自己定义

缩放的Activity代码

public class MyActivity extends Activity {
	private ImageView image=null;
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.setContentView(R.layout.mylayout);
		image=(ImageView)super.findViewById(R.id.image1);
	}
	public void scale(View v){
		AnimationSet set=new AnimationSet(true);
		ScaleAnimation scale=new ScaleAnimation(
				1,0.0f,//X轴从满屏缩小到无
				1,0.0f,//Y轴从满屏缩小到无
				Animation.RELATIVE_TO_SELF,0.5f,//以自身0.5宽度为轴缩放
				Animation.RELATIVE_TO_SELF,0.5f);//以自身0.5宽度为轴缩放
		scale.setDuration(3000);//三秒完成动画
		set.addAnimation(scale);//增加动画
		MyActivity.this.image.startAnimation(set);
		
	}
}

 

平移代码:

public class MyActivity2 extends Activity{
	private ImageView image=null;
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.setContentView(R.layout.mylayout2);
		image=(ImageView)super.findViewById(R.id.image2);
	}
	public void Translate(View v){
		AnimationSet set=new AnimationSet(true);
		TranslateAnimation trans=new TranslateAnimation(
				Animation.RELATIVE_TO_SELF,0.0f,
				Animation.RELATIVE_TO_SELF,0.5f,//以自身0.5宽度为轴
				Animation.RELATIVE_TO_SELF,0.0f,
				Animation.RELATIVE_TO_SELF,1.5f);//以y轴原点进行计算
		trans.setDuration(3000);//三秒完成动画
		set.addAnimation(trans);//增加动画
		MyActivity2.this.image.startAnimation(set);
		
	}

 

旋转代码

public class MyActivity3 extends Activity {
	private ImageView image=null;
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.setContentView(R.layout.mylayout3);
		image=(ImageView)super.findViewById(R.id.imageView1);
	}
	public void Rotate(View v){
		AnimationSet set=new AnimationSet(true);
		RotateAnimation rotate=new RotateAnimation(
				0,60,//旋转角度
				Animation.RELATIVE_TO_SELF,0.5f,//以自身0.5宽度为轴
				Animation.RELATIVE_TO_SELF,0.0f);//以y轴原点进行计算
		rotate.setDuration(3000);//三秒完成动画
		set.addAnimation(rotate);//增加动画
		MyActivity3.this.image.startAnimation(set);
		
	}
}

 实现效果如下:

缩放:


平移:


旋转:




 
 

 
 

 
 

  • 大小: 101.4 KB
  • 大小: 26.8 KB
  • 大小: 7.9 KB
  • 大小: 8.6 KB
  • 大小: 8.5 KB
  • 大小: 7.8 KB
0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics