ym——Android从零开始(23)(Notification+ Animation)(新)

转载请注明本文出自Cym的博客(http://blog.csdn.net/cym492224103),谢谢支持!


通知(Notification

定义:Notification就是在桌面的状态通知栏。

       一般而言,消息提示,可以通过Toast方式,提醒给用户看,而通过Notification方式的话,可以在状态栏显示出来。并且有声音,还有文字描述,并且可以出现在消息公告栏,这在QQ,飞信等常用即时通信软件中应用广泛。点击该在状态栏中的图标,就又可以回到原程序中。这样就是一个进入该程序的入口,同时还可以显示状态,比如离开,隐身,忙碌等状态。


普通通知

//通知管理服务
NotificationManager nm =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
           //通知
           Notification notification = newNotification(
                         android.R.drawable.stat_notify_chat,
                         "提示信息",
                         System.currentTimeMillis());
           Intent intent = newIntent(Intent.ACTION_CALL,Uri.parse("tel:5558"));
           //延时意图
           PendingIntent contentIntent =PendingIntent.getActivity(
                         this,
                         100,
                         intent,
                         0);
           //设置通知的点击事件
           notification.setLatestEventInfo(
                         this,
                         "标题",
                         "通知内容",
                         contentIntent);
           //设置通知点击之后自动消失
           notification.flags =Notification.FLAG_AUTO_CANCEL;
           //发送通知
           nm.notify(100, notification);

自定义通知

下载通知,进度条不断修改,需要多次发送通知

          //通知管理服务
           NotificationManager nm =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

           //通知
           Notification notification = newNotification(
                         android.R.drawable.stat_notify_chat,
                         "提示信息",
                         System.currentTimeMillis());


           Intent intent = newIntent(Intent.ACTION_CALL,Uri.parse("tel:5558"));
           //延时意图
           PendingIntentcontentIntent = PendingIntent.getActivity(
                         this,
                         100,
                         intent,
                         0);

           //设置通知的点击事件
           notification.setLatestEventInfo(
                         this,
                         "标题",
                         "通知内容",
                         contentIntent);

           //设置通知点击之后自动消失
           notification.flags =Notification.FLAG_AUTO_CANCEL;


           //发送通知
           nm.notify(100, notification);
    }

   public void send_custom(View v){
           final NotificationManager nm =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

           final Notification notification = newNotification();
           //图片
           notification.icon =android.R.drawable.stat_sys_download;
           //提示信息
           notification.tickerText = "自定义通知";
           //点击事件
           PendingIntent contentIntent =PendingIntent.getActivity(this,
                         100,
                         newIntent(Intent.ACTION_CALL,Uri.parse("tel:4444")),
                         0);
           notification.contentIntent =contentIntent;

           //布局
           final RemoteViewscontentView = new RemoteViews(getPackageName(), R.layout.other);
           notification.contentView = contentView;



           //自动消失
           notification.flags =Notification.FLAG_AUTO_CANCEL;
           nm.notify(100, notification);


           //通过线程去更新进度条的进度
           newThread(){
                  public void run() {
                         inti = 1;
                         while(i<= 100){
                       contentView.setProgressBar(R.id.pb,
                                     100,
                                     i,
                                     false);
                       SystemClock.sleep(200);
                       i++;
                       nm.notify(100,notification);
                         }
                         //下载完成取消
                         nm.cancel(100);
                        
                  };
           }.start();

动画

Tween 动画作用于控件,Frame动画作用于背景

1.Tween Animation  用于操作控件

       1.1    AlphaAnimation     (透明度)

       1.2    ScaleAnimation           (缩放)

       1.3    TranslateAnimation   (平移)

       1.4    RotateAnimation  (旋转)      


2.Frame Animation


3.Property Animation:点击查看


提示:动画可以通过xml文件、编码来实现


Tween 动画

Xml实现调用方式

// 得到要操作的控件
ImageView iv = (ImageView)findViewById(R.id.iv);
//准备动画                                                            // 不同xml不同效果
Animation animation =AnimationUtils.loadAnimation(this, R.anim.scale);
//保持动画执行完之后的状态
animation.setFillAfter(true);
// 给该设置动画
iv.startAnimation(animation);

Xml

Alpha:

<!-- 透明度改变动画
   android:fromAlpha="1.0" 从哪个透明度
   android:toAlpha="0.0"   到哪个透明度
   android:duration="2000" 执行时间
-->
<alphaxmlns:android="http://schemas.android.com/apk/res/android"
   android:fromAlpha="1.0"
   android:toAlpha="0.0"
   android:duration="2000">
</alpha>

Rotate:

<?xml version="1.0"encoding="utf-8"?>
<!--
旋转动画
       android:fromDegrees="0" 从哪个角度
       android:toDegrees="90"  旋转到哪个角度
       android:pivotX="50%"    以自己为中心
        android:pivotY="50%"
-->
<rotatexmlns:android="http://schemas.android.com/apk/res/android"
       android:fromDegrees="0"
       android:toDegrees="90"
       android:pivotX="50%"
       android:pivotY="50%"
       android:duration="2000">
</rotate>

Scale:

<?xml version="1.0"encoding="utf-8"?>
<!--
缩放动画
       android:fromXScale="1.0" 从x轴缩放的倍数
       android:toXScale="2.0" 到x轴缩放的倍数
       android:fromYScale="1.0" 从y轴缩放的倍数
       android:toYScale="0.5" 到y轴缩放的倍数
       android:pivotX="50%"  以图片自己的中心为中心点缩放
       android:pivotY="50%"
-->
<scalexmlns:android="http://schemas.android.com/apk/res/android"
       android:fromXScale="1.0"
       android:toXScale="2.0"
       android:fromYScale="1.0"
       android:toYScale="0.5"
       android:pivotX="50%"
       android:pivotY="50%"
       android:duration="2000" >
</scale>

Translate:

<?xmlversion="1.0" encoding="utf-8"?>
<!--
平移动画
       android:fromXDelta="0"  从x轴点的坐标
       android:toXDelta="200"  移动到x轴点的坐标
       android:fromYDelta="0"
       android:toYDelta="200"
100%p p相当于Parent
-->
<translatexmlns:android="http://schemas.android.com/apk/res/android"
       android:fromXDelta="0"
       android:toXDelta="200"
       android:fromYDelta="0"
       android:toYDelta="200"
       android:duration="2000">
</translate>

Set:

<?xml version="1.0"encoding="utf-8"?>
<!-- 动画集合 -->
<setxmlns:android="http://schemas.android.com/apk/res/android" >

   <alpha
       xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="3000"
       android:fromAlpha="1"
       android:toAlpha="0" />

   <rotate
       xmlns:android="http://schemas.android.com/apk/res/android"
       android:duration="3000"
       android:fromDegrees="0"
       android:pivotX="50%"
       android:pivotY="50%"
       android:toDegrees="90" />

   <scale
       xmlns:android="http://schemas.android.com/apk/res/android"
       android:duration="3000"
       android:fromXScale="1"
       android:fromYScale="1"
       android:pivotX="50%"
       android:pivotY="50%"
       android:toXScale="2"
       android:toYScale="0.5" />

   <translate
       xmlns:android="http://schemas.android.com/apk/res/android"
       android:duration="3000"
       android:fromXDelta="0"
        android:fromYDelta="0"
       android:toXDelta="50%p"
       android:toYDelta="50%p" />

</set>

通过代码实现:

New相应动画,给入相应属性即可,xml更好维护

Animation animation = new AlphaAnimation(1,0.3f);
           animation.setDuration(3000);
           //保持执行完之后的状


Frame动画

Activity

public class MainActivity extends Activity{
   private AnimationDrawable ad;
       /**Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
       ImageView iv = (ImageView) findViewById(R.id.iv);
       ad = (AnimationDrawable) iv.getBackground();

    }

   public void start(View v){
           ad.start();

    }
   public void stop(View v){
           ad.stop();
    }
}

Xml

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
   androidneshot="false" // 设置是否执行一次,还是多次
   >
    <item
       android:drawable="@drawable/a1"
       android:duration="200"/>
    <item
       android:drawable="@drawable/a2"
       android:duration="200"/>
    <item
       android:drawable="@drawable/a3"
       android:duration="200"/>
    <item
       android:drawable="@drawable/a4"
       android:duration="200"/>
    <item
       android:drawable="@drawable/a5"
       android:duration="200"/>
        <item
       android:drawable="@drawable/a6"
       android:duration="200"/>
    <item
       android:drawable="@drawable/a7"
       android:duration="200"/>
    <item
       android:drawable="@drawable/a8"
       android:duration="200"/>
    <item
       android:drawable="@drawable/a9"
       android:duration="200"/>
    <item
        android:drawable="@drawable/a10"
       android:duration="200"/>
        <item
       android:drawable="@drawable/a11"
       android:duration="200"/>
    <item
       android:drawable="@drawable/a12"
       android:duration="200"/>
    <item
       android:drawable="@drawable/a13"
       android:duration="200"/>
    <item
       android:drawable="@drawable/a14"
       android:duration="200"/>
    <item
       android:drawable="@drawable/a15"
       android:duration="200"/>
</animation-list>

Main.xml

<?xml version="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   androidrientation="vertical" >

   <ImageView
       android:id="@+id/iv"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:background="@drawable/frame"/>

   <Button
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       androidnClick="start"
       android:text="开始" />

   <Button
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       androidnClick="stop"
       android:text="停止" />


</LinearLayout>

如果不在xml里面设置图片的背景通过java代码设置的话:

ImageView iv =  (ImageView)findViewById(R.id.iv);
        iv.setBackgroundResource(R.drawable.frame);// 绑定frame动画图形
        ad = (AnimationDrawable)iv.getBackground();
        // 等待frame动画图形加载完毕才可以启动动画
        // 于是把启动动画放在一个主线程的消息队列里面让判定frame动画图形执行完毕后在启动动画
        getMainLooper().myQueue().addIdleHandler(new MessageQueue.IdleHandler() {
            
            @Override
            public boolean queueIdle() {
                ad.start(); // 启动动画
                return false;
            }
        });


课后问题

通知由那几部分组成

通知管理器

通知

通知图标、通知提示、通知时间

通知标题、通知内容、通知点击事件

通知管理器发送通知


实现通知的步骤

通过服务得到通知管理器

创建通知

设置通知图标、通知提示、通知时间

设置通知标题、通知内容、通知点击事件

通知管理器发送通知


动画有哪两种类型

Tween

frame


Tween有哪几种?

5

旋转 rotate

缩放 scale

透明度 alpha

平移 translate

集合 set


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值