资源描述
MARS-Android 视频教程实例源代码
第二季第八集
AVD版本
Build SDK和Mini SDK
Animations01
package mars.admations01;//当时建包时输错了字母
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.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageButton;
public class MainActivity extends Activity {
private Button scaleButton=null;
private Button rotateButton=null;
private Button alphaButton=null;
private Button translateButton=null;
private ImageButton imageButton=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scaleButton=(Button)findViewById(R.id.scaleButtonId);
scaleButton.setOnClickListener(new scaleButtonOnClickListener());
rotateButton=(Button)findViewById(R.id.rotateButtonId);
rotateButton.setOnClickListener(new rotateButtonOnClickListener());
alphaButton=(Button)findViewById(R.id.alphaButtonId);
alphaButton.setOnClickListener(new alphaButtonOnClickListener());
translateButton=(Button)findViewById(R.id.translateButtonId);
translateButton.setOnClickListener(new translateButtonOnClickListener());
imageButton=(ImageButton)findViewById(R.id.imageViewId);
}
class scaleButtonOnClickListener implements OnClickListener{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
System.out.println("scaleButtonOnClick");
AnimationSet animationSet=new AnimationSet(true);
ScaleAnimation scaleAnimation=new ScaleAnimation(1,0.1f,1,0.1f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
animationSet.addAnimation(scaleAnimation);
animationSet.setDuration(2000);
//animationSet.setFillBefore(false);
animationSet.setFillAfter(true);
imageButton.setAnimation(animationSet);
}
}
class rotateButtonOnClickListener implements OnClickListener{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
System.out.println("rotateButtonOnClick");
AnimationSet animationSet=new AnimationSet(true);
RotateAnimation rotateAnimation=new RotateAnimation(0,360,Animation.RELATIVE_TO_PARENT,1f,Animation.RELATIVE_TO_PARENT,0F);
rotateAnimation.setDuration(5000);
animationSet.addAnimation(rotateAnimation);
imageButton.setAnimation(animationSet);
}
}
class alphaButtonOnClickListener implements OnClickListener{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
System.out.println("alphaButtonOnClick");
AnimationSet animationSet=new AnimationSet(true);
AlphaAnimation alphaAnimation=new AlphaAnimation(1,0);
alphaAnimation.setDuration(1000);
animationSet.addAnimation(alphaAnimation);
imageButton.setAnimation(animationSet);
}
}
class translateButtonOnClickListener implements OnClickListener{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
System.out.println("translateButtonOnClick");
AnimationSet animationSet=new AnimationSet(true);
TranslateAnimation translateAnimation=new TranslateAnimation(Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,1f);
translateAnimation.setDuration(1000);
animationSet.addAnimation(translateAnimation);
imageButton.setAnimation(animationSet);
}
;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Activity_main.xml
<RelativeLayout xmlns:android="
xmlns:tools="
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
tools:context=".MainActivity" />
<Button
android:id="@+id/scaleButtonId"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="测试scale效果"
/>
<Button
android:id="@+id/rotateButtonId"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@id/scaleButtonId"
android:text="测试rotate效果"
/>
<Button
android:id="@+id/alphaButtonId"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@id/rotateButtonId"
android:text="测试alpha效果"
/>
<Button
android:id="@+id/translateButtonId"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@id/alphaButtonId"
android:text="测试translate效果"
/>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageButton
android:id="@+id/imageViewId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginTop="100dp"
android:src="@drawable/ic_launcher"
/>
</LinearLayout>
</RelativeLayout>
展开阅读全文