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

自定义view 类似QQ好友来消息的动画效果

2011-05-09 12:20 323 查看
 

///////////////////////////////////////////////////////////////主activity/////////////////////////////////////////////////////////////////

MessageSplashView messageSplashView;// 闪动VIEW 自定义的

 

@Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  // 设置全屏显示 & 无title
  requestWindowFeature(Window.FEATURE_NO_TITLE);
  getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
    WindowManager.LayoutParams.FLAG_FULLSCREEN);

  setContentView(R.layout.main);

 

 // ///////////////////
  // 好友消息的处理
  messageSplashView = (MessageSplashView) findViewById(R.id.messageSplashView);
  messageSplashView.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {

    if (messageSplashView.isshow) {

     //发送一个移除消息的广播,改变其他接收到广播消息的activity状态
     Intent i = new Intent();
     i.putExtra("broadcastType", 1);
     i.setAction("com.oneshow.chatmessage.broadcast");
     sendBroadcast(i);
     
     messageSplashView.isshow = false;
     Intent intent = new Intent();
     Bundle bundle = new Bundle();
     bundle.putString("strAccount", strAccount);
     bundle.putInt("chatfriend", chatfriend);
     /* 将Bundle对象assign给Intent */
     intent.putExtras(bundle);
     /* 调用Activity */
     intent.setClass(MyActivity.this, FriendChat.class);//
     startActivity(intent);

    }
   }
  });

// //////////////////

 

DataReceiver dataReceiver;// 接收器
 int chatfriend;// 发来信息的好友id

 @Override
 protected void onStart() {
  System.out.println("OnStart();");
  regBroadcast();
  super.onStart();
 }

 private void regBroadcast() {
  dataReceiver = new DataReceiver();
  IntentFilter filter = new IntentFilter();// 创建IntentFilter对象
   filter.addAction("com.oneshow.chatmessage.broadcast");
//  filter.addAction("com.oneshow.HallActivity");
  registerReceiver(dataReceiver, filter);// 注册Broadcast Receiver
 }

 public class DataReceiver extends BroadcastReceiver {

  @Override
  public void onReceive(Context context, Intent intent) {
   int broadcastType = intent.getExtras().getInt("broadcastType");
   Log.i("HallActivity", "DataReceiver onReceive broadcastType="+broadcastType);
   switch(broadcastType)
   {
   
   case 0://添加广播消息
    chatfriend = intent.getExtras().getInt("nSrcPID");
    messageSplashView.isshow = true;
    messageSplashView.invalidate();
    break ;
   case 1://移除广播消息
    messageSplashView.isshow = false;
    break ;
   
   }
   
   

  }

 }

 

///////////////////////////////////////////////////////////////自定义view/////////////////////////////////////////////////////////////////

 

/**
 * 有消息时候 的 闪动 动画
 */
public class MessageSplashView extends LinearLayout {

 final static String TAG = "MessageSplashView";
 Bitmap friendmsg0;// 信封bmp
 Bitmap friendmsg1;// 信封bmp1
 public boolean isshow;//是否显示
 MyView myView;// view 对象
 Paint paint;

 @Override
 protected void onDraw(Canvas canvas) {
  super.onDraw(canvas);
 }

 public MessageSplashView(Context context, AttributeSet attrs) {
  super(context, attrs);
  friendmsg0 = BitmapFactory.decodeResource(context.getResources(),
    R.drawable.friendmsg0);
  friendmsg1 = BitmapFactory.decodeResource(context.getResources(),
    R.drawable.friendmsg1);
  TypedArray a = context.obtainStyledAttributes(attrs,
    R.styleable.MessageSplashView);
  // x = a.getInteger(R.styleable.MessageSplashView_drawx, 0);
  // y = a.getInteger(R.styleable.MessageSplashView_drawy, 0);
  isshow = a.getBoolean(R.styleable.MessageSplashView_isshow, false);
  paint = new Paint();
  myView = new MyView(context);
  addView(myView);
 }

 // **控制闪动的计数器**//
 int count = 0;
 int curIndex;

 class MyView extends View {
  public MyView(Context context) {
   super(context);
  }

  @Override
  protected void onDraw(Canvas canvas) {
   super.onDraw(canvas);
   if (!isshow)
    return;
   if (friendmsg0 != null) {
    try {
     count++;
     Thread.sleep(500);
     if (count % 2 == 0) {
      canvas.drawBitmap(friendmsg0, 0, 0, paint);
     } else {
      canvas.drawBitmap(friendmsg1, 0, 0, paint);
     }
    } catch (Exception e) {
     e.printStackTrace();
    }

   }
   this.invalidate();
  }
 }

}

 

///////////////////////////////////////////////////////////////布局相关0/////////////////////////////////////////////////////////////////

/res/layout-hdpi/main.xml

 

<?xml version="1.0" encoding="utf-8"?>
<!-- 布局 -->
<RelativeLayout 
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:test="http://schemas.android.com/apk/res/com.oneshow"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/bgback3"
    >
   
    <ImageView
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:src="@drawable/bg"
     />
     
  
 <!-- 接收好友发来信息的闪烁效果 自定义view进行绘制-->
  <com.oneshow.views.MessageSplashView
    android:id="@+id/messageSplashView"
    android:layout_width="48px"
  android:layout_height="35px"
   android:layout_alignParentBottom="true"
   android:layout_centerHorizontal="true"
        android:layout_marginBottom="10.0px"
    />
</RelativeLayout >

///////////////////////////////////////////////////////////////布局相关/////////////////////////////////////////////////////////////////

/res/values/attrs.xml

<?xml version="1.0" encoding="utf-8" ?>
 <declare-styleable name="MessageSplashView">
  <attr name="drawx" format="integer" />
  <attr name="drawy" format="integer" />
  <attr name="isshow" format="boolean" />
 </declare-styleable>
</resources>

 

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息