MainActivity.java
public class MainActivity extends Activity {
ImageView iv;
AnimationDrawable rocketAnimation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.iv);
iv.setBackgroundResource(R.drawable.clock);
rocketAnimation = (AnimationDrawable) iv.getBackground();
}
//提示对话框
public void clickBut(View v){
AlertDialog.Builder builder = new Builder(this);
builder.setTitle("对话框标题");
builder.setIcon(R.drawable.ic_launcher);
builder.setMessage("对话框内容");
builder.setPositiveButton("OK", new OkListener());
builder.setNegativeButton("cancel", new CancelListener());
AlertDialog dialog = builder.create();
dialog.show();
}
/**
* 对话框确定按钮-监听实现
*
*/
class OkListener implements OnClickListener{
@Override
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(MainActivity.this, " OK click", 1).show();
}
}
/**
* 对话框取消按钮-监听实现
*
*/
class CancelListener implements OnClickListener{
@Override
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(MainActivity.this, " cancel click", 1).show();
}
}
//单选对话框
public void singleBut(View v){
AlertDialog.Builder builder = new Builder(this);
builder.setTitle("单选对话框");
String[] items = new String[]{"item1","item2","item3",};
builder.setSingleChoiceItems(items, 1, new checkedItem());
builder.show();
}
class checkedItem implements OnClickListener{
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "items["+which+"]", 0).show();
dialog.dismiss();
}
}
//多选对话框
public void multiBut(View v){
AlertDialog.Builder builder = new Builder(this);
builder.setTitle("单选对话框");
String[] items = new String[]{"item1","item2","item3","item4"};
boolean[] checkedItems = new boolean[]{true,false,true,false};
builder.setMultiChoiceItems(items, checkedItems, new MultiListener());
builder.setPositiveButton("OK", new multiOkListener());
builder.setNegativeButton("cancel", new multiCancelListener());
builder.show();
}
class MultiListener implements OnMultiChoiceClickListener{
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
Toast.makeText(MainActivity.this, "items["+which+"]"+isChecked, 0).show();
}
}
class multiOkListener implements OnClickListener{
@Override
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(MainActivity.this, " OK click", 1).show();
}
}
class multiCancelListener implements OnClickListener{
@Override
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(MainActivity.this, " cancel click", 1).show();
}
}
//进度条对话框
public void progressBurListener(View v){
final ProgressDialog pd= new ProgressDialog(this);
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.setTitle("loading...");
pd.setMax(100);
pd.show();
new Thread(){
public void run(){
for(int i=0;i<100;i++){
try {
pd.setProgress(i);
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
pd.dismiss();
}
}.start();
}
//notification 通知
public void notificationButListener(View v){
//1.获取系统通知管理器
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//2.实例化notification 通知内容
Notification notification = new Notification(R.drawable.ic_launcher, "notification title",System.currentTimeMillis());
//点击通知是自动消失
notification.flags = Notification.FLAG_AUTO_CANCEL;
//参数4实现一个延期的intent 如:点击短信通知-跳转到短信界面;null 无操作
notification.setLatestEventInfo(this, "notofication标题", "notofication内容", null);
/* API 11以上支持
Notification.Builder builder = new Notification.Builder(this);
builder.setContentTitle("我是notification标题")
.setContentText("我是notification内容")
.setSmallIcon(R.drawable.notification)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
Notification notification = builder.build();
*/
nm.notify(0,notification);
}
//帧动画
public void ivButListener(View v){
Toast.makeText(this, "帧动画", 0).show();
rocketAnimation.start();
}
//tween 补间动画
public void tweenAnimationBut(View v){
Intent intent = new Intent(this,TweenActivity.class);
startActivity(intent);
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.ui.MainActivity" >
<Button
android:id="@+id/clickBut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="提示对话框"
android:onClick="clickBut"/>
<Button
android:id="@+id/clickBut2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="单选"
android:onClick="singleBut" />
<Button
android:id="@+id/clickBut3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="多选"
android:onClick="multiBut" />
<Button
android:id="@+id/progressBur"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="进度条"
android:onClick="progressBurListener"/>
<Button
android:id="@+id/notificationBut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="notification通知"
android:onClick="notificationButListener" />
<Button
android:id="@+id/ivBut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始倒计时"
android:onClick="ivButListener"
/>
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开启tweenAnimation"
android:onClick="tweenAnimationBut"/>
</LinearLayout>
clock.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/image5" android:duration="200" />
<item android:drawable="@drawable/image4" android:duration="200" />
<item android:drawable="@drawable/image3" android:duration="200" />
<item android:drawable="@drawable/image2" android:duration="200" />
<item android:drawable="@drawable/image1" android:duration="200" />
</animation-list>
TweenActivity.java-补间动画
package com.example.ui;
import com.example.ui.R.id;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
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.ImageView;
public class TweenActivity extends Activity {
ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tween);
iv = (ImageView) findViewById(id.iv);
}
/**
* tween
* 透明度变化
* AlphaAnimation(开始透明度,结束透明度)
*/
public void tweenButListener(View v){
AlphaAnimation animation = new AlphaAnimation(0.0f, 1.0f);
animation.setDuration(2000);//播放时间
animation.setRepeatCount(2);//播放2次
animation.setRepeatMode(Animation.REVERSE);//重复的模式-[渐显->渐隐->渐显...]
iv.startAnimation(animation);
}
/**
* 缩放ScaleAnimation
* 0.2f- 2.0f 从本事20% 放大 200%
*/
public void ScaleButListener(View v){
ScaleAnimation animation = new ScaleAnimation(
0.2f, //x轴-20%
2.0f, //x轴-200%
0.2f, //y轴-20%
2.0f, //y轴-200%
Animation.RELATIVE_TO_SELF, //X 相对与哪个点,[自身点]
0.5f, //自身中心点
Animation.RELATIVE_TO_SELF, //Y 相对与哪个点,[自身点]
0.5f);//自身中心点
animation.setDuration(2000);//播放时间
animation.setRepeatCount(2);//播放2次
animation.setRepeatMode(Animation.REVERSE);//重复的模式-[渐显->渐隐->渐显...]
iv.startAnimation(animation);
}
/**
* 旋转RotateAnimation
*/
public void RotateButListener(View v){
RotateAnimation animation = new RotateAnimation(
-45,
45,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.0f);
animation.setDuration(2000);//播放时间
animation.setRepeatCount(2);//播放2次
animation.setRepeatMode(Animation.REVERSE);//重复的模式-[渐显->渐隐->渐显...]
iv.startAnimation(animation);
}
/**
* 位移 TranslateAnimation
*/
public void TranslateButListener(View v){
TranslateAnimation animation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 1.0f,
Animation.RELATIVE_TO_SELF, 1.0f,
Animation.RELATIVE_TO_SELF, 1.0f,
Animation.RELATIVE_TO_SELF, 2.0f);
animation.setDuration(2000);//播放时间
animation.setRepeatCount(2);//播放2次
animation.setRepeatMode(Animation.REVERSE);//重复的模式-[渐显->渐隐->渐显...]
iv.startAnimation(animation);
}
/**
* 组合动画效果-把以上动画效果加入
*/
public void groupBut(View v){
AnimationSet set = new AnimationSet(false);
ScaleAnimation sa = new ScaleAnimation(0, 0, 0, 0, 0, 0, 0, 0);
RotateAnimation ra = new RotateAnimation(0, 0, 0, 0, 0, 0);
TranslateAnimation ta = new TranslateAnimation(0, 0, 0, 0, 0, 0, 0, 0);
set.addAnimation(sa);
set.addAnimation(ra);
set.addAnimation(ta);
iv.startAnimation(set);
}
}
activity_tween.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.ui.TweenActivity" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="tweenButListener"
android:text="透明度" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="缩放"
android:onClick="ScaleButListener"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="旋转"
android:onClick="RotateButListener"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="平移"
android:onClick="TranslateButListener"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" 组合"
android:onClick="groupBut"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"/>
</LinearLayout>
</LinearLayout>