Esempio n. 1
0
 /**
  * @param photoUrl 本地地址或网上地址
  * @param imageView
  */
 public void showPhoto(String photoUrl, ImageView imageView) {
   // 没有网显示本地头像
   if (photoUrl != null && !photoUrl.equals("")) {
     ImageLoader.getInstance().displayImage(photoUrl, imageView, ImageLoadOptions.getOptions());
   } else {
     imageView.setImageResource(R.drawable.ic_default_profile_head);
   }
 }
Esempio n. 2
0
  @Override
  public View bindView(final int position, View convertView, ViewGroup parent) {
    final BmobMsg item = list.get(position);
    if (convertView == null) {
      convertView = createViewByType(item, position);
    }

    // 通用
    ImageView iv_avatar = ViewHolder.get(convertView, R.id.head_imageView); // 头像
    ImageView iv_fail = ViewHolder.get(convertView, R.id.msg_error_imageView); // 错误标志
    final ProgressBar pgb_load = ViewHolder.get(convertView, R.id.msg_sending_progressBar); // 转圈
    TextView tv_time = ViewHolder.get(convertView, R.id.tv_time); // 时间

    // 文本类型
    TextView tv_message = ViewHolder.get(convertView, R.id.reply_textView); // 对话内容

    // 图片
    ImageView iv_imagemessage = ViewHolder.get(convertView, R.id.reply_imageView); // 图像

    // 语音
    final ImageView iv_voice = ViewHolder.get(convertView, R.id.iv_voice);
    final ImageView iv_anim_voice = ViewHolder.get(convertView, R.id.iv_anim_voice);
    final TextView tv_voice_length = ViewHolder.get(convertView, R.id.tv_voice_length);

    /** 头像 */
    String avatar = item.getBelongAvatar();
    if (avatar != null && !avatar.equals("")) {
      ImageLoader.getInstance()
          .displayImage(avatar, iv_avatar, ImageLoadOptions.getOptions(), firstDisplayListener);
    } else {
      iv_avatar.setImageResource(R.drawable.ic_default_profile_head);
    }
    iv_avatar.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View view) {
            // 根据谁的消息进入相关的activity,false来自对方,true来自我方
            if (fromWho(getItemViewType(position))) {
              // TODO
            } else {
              Intent intent = new Intent(mContext, UserInfoActivity.class);
              intent.putExtra("username", item.getBelongUsername());
              mContext.startActivity(intent);
            }
          }
        });

    /** 设置回复时间 */
    tv_time.setVisibility(View.VISIBLE);
    tv_time.setText(TimeUtil.getChatTime(Long.parseLong(item.getMsgTime())));

    /** 设定控件显示规则,发送状态改变 progress 和 fail image */
    if (getItemViewType(position) == TYPE_SEND_TXT
        || getItemViewType(position) == TYPE_SEND_VOICE
        || getItemViewType(position) == TYPE_SEND_LOCATION) {
      switch (item.getStatus()) {
          // 开始发送
        case BmobConfig.STATUS_SEND_START:
          pgb_load.setVisibility(View.VISIBLE);
          iv_fail.setVisibility(View.INVISIBLE);
          if (item.getMsgType() == BmobConfig.TYPE_VOICE) {
            tv_voice_length.setVisibility(View.VISIBLE);
          }
          break;
          // 发送成功
        case BmobConfig.STATUS_SEND_SUCCESS:
          pgb_load.setVisibility(View.INVISIBLE);
          iv_fail.setVisibility(View.INVISIBLE);
          if (item.getMsgType() == BmobConfig.TYPE_VOICE) {
            tv_voice_length.setVisibility(View.GONE);
          }
          break;
          // 发送失败
        case BmobConfig.STATUS_SEND_FAIL:
          pgb_load.setVisibility(View.INVISIBLE);
          iv_fail.setVisibility(View.VISIBLE);
          if (item.getMsgType() == BmobConfig.TYPE_VOICE) {
            tv_voice_length.setVisibility(View.GONE);
          }
          break;
        default:
          break;
      }
    }

    /** 根据类型显示内容 */
    final String text = item.getContent();
    switch (item.getMsgType()) {
        /** 显示文本 */
      case BmobConfig.TYPE_TEXT:
        try {
          // TODO 表情
          tv_message.setText(text);
        } catch (Exception e) {
        }
        break;
        /** 显示图片 */
      case BmobConfig.TYPE_IMAGE:
        try {
          if (text != null && !text.equals("")) {
            // 设定显示规则
            dealWithImage(position, pgb_load, iv_fail, iv_imagemessage, item);
          }
          // 点击图片事件
          iv_imagemessage.setOnClickListener(
              new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                  // TODO MessageAdapter 点击图片事件
                  Intent intent = new Intent(mContext, ImageActivity.class);
                  String imageUri = getImageUrl(item);
                  intent.putExtra("image", imageUri);
                  mContext.startActivity(intent);
                }
              });
        } catch (Exception e) {

        }
        break;
      case BmobConfig.TYPE_LOCATION:
        break;
        /** 语音 */
      case BmobConfig.TYPE_VOICE:
        try {
          if (text != null && !text.equals("")) {
            String content = item.getContent();
            Log.i("Test", "voice content is:" + content);
            if (item.getBelongId().equals(currentObjectId)) {
              // 通过content获取长度
              if (item.getStatus() == BmobConfig.STATUS_SEND_SUCCESS) {
                tv_voice_length.setVisibility(View.VISIBLE);
                String length = content.split("&")[2];
                tv_voice_length.setText(length + "\''");
              } else {
                tv_voice_length.setVisibility(View.INVISIBLE);
              }
            } else {
              // 检测指定的文件是否存在
              boolean isExist = BmobDownloadManager.checkTargetPathExist(currentObjectId, item);
              if (!isExist) {
                String uri = content.split("&")[0];
                final String length = content.split("&")[1];
                BmobDownloadManager downloadManager =
                    new BmobDownloadManager(
                        mContext,
                        item,
                        new DownloadListener() {
                          @Override
                          public void onStart() {
                            pgb_load.setVisibility(View.VISIBLE);
                            tv_voice_length.setVisibility(View.GONE);
                            iv_voice.setVisibility(View.INVISIBLE);
                          }

                          @Override
                          public void onSuccess() {
                            pgb_load.setVisibility(View.GONE);
                            tv_voice_length.setVisibility(View.VISIBLE);
                            tv_voice_length.setText(length + "\''");
                            iv_voice.setVisibility(View.VISIBLE);
                          }

                          @Override
                          public void onError(String s) {
                            pgb_load.setVisibility(View.GONE);
                            tv_voice_length.setVisibility(View.GONE);
                            iv_voice.setVisibility(View.INVISIBLE);
                          }
                        });
                downloadManager.execute(uri);
              } else {
                String length = content.split("&")[2];
                tv_voice_length.setText(length + "\''");
              }
            }
          }
          String localPath = "";
          if (item.getBelongId().equals(currentObjectId)) { // 如果是自己发送的语音消息,则播放本地地址
            localPath = item.getContent().split("&")[0];
          } else {
            // 如果是收到的消息,则需要先下载后播放
            localPath = getDownLoadFilePath(mContext, item);
            Log.i("voice", "收到的语音存储的地址:" + localPath);
          }
          initMediaPlayer(localPath);
          // 播放语音
          iv_voice.setOnClickListener(
              new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                  // 如果正在播放就停止播放
                  if (mediaPlayer.isPlaying()) {
                    stopAnimation(item, iv_voice, iv_anim_voice);
                    mediaPlayer.reset();
                    mediaPlayer.release();
                  } else {
                    // 如果停止播放就开始播放
                    startAnimation(item, iv_voice, iv_anim_voice);
                    mediaPlayer.start();
                  }
                }
              });
        } catch (Exception e) {

        }

        break;
      default:
        break;
    }
    return convertView;
  }