package com.partynetwork.iparty.ishare;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.partynetwork.dataprovider.DataProvider.IJsonResultListener;
import com.partynetwork.dataprovider.json.NetworkEntity;
import com.partynetwork.dataprovider.json.struct.Contacts_setAttentionRequest;
import com.partynetwork.dataprovider.json.struct.Ishare_releaseIshareRequest;
import com.partynetwork.dataprovider.util.ActionUtil;
import com.partynetwork.dataprovider.util.BitmapUtil;
import com.partynetwork.iparty.R;
import com.partynetwork.iparty.app.AppContext;
import com.partynetwork.iparty.app.common.BitmapManager;
import com.partynetwork.iparty.contacts.ContactsListActivity;
import com.partynetwork.iparty.helper.IntentHelper;
import com.partynetwork.iparty.info.ContactsInfo;
import com.partynetwork.iparty.info.IsharePhotoInfo;
import com.partynetwork.iparty.ishare.IsharePublishActivity.publishMainListener;
import com.partynetwork.myview.mytoast.MenuBottomPop;
import com.lidroid.xutils.util.LogUtils;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.Html;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ImageView.ScaleType;
public class IsharePublishMain extends RelativeLayout implements
IJsonResultListener, OnClickListener, OnTouchListener {
public static final int SELECT_PICTURE = 110;
public static final int SELECT_CAMER = 111;
/**
* 当前对象的上下文
*/
private Activity activity;
/**
* 所有的控件顶层
*/
private RelativeLayout relativeLayout;
/**
* 图片放置的布局
*/
private RelativeLayout imageContent;
/**
* 四张图片
*/
private ImageView iv1;
private ImageView iv2;
private ImageView iv3;
private ImageView iv4;
/**
* 多张图片的ImageView集合数组
*/
private ImageView[] listImage = new ImageView[4];
/**
* 单张的图片
*/
private ImageView iv5;
/**
* 文字内容输入框
*/
private EditText title_et;
/**
* 切换权限的按钮
*/
private TextView purview_tv;
/**
* 音频的布局
*/
private LinearLayout voice_ll;
/**
* 音频的文本
*/
private Button voice_btn;
/**
* 音频的删除按钮
*/
private Button voice_delete_btn;
/**
* 录音按钮的布局
*/
private LinearLayout recording_ll;
/**
* 录音中间的文字
*/
private TextView recording_tv;
/**
* 主题的选择文字
*/
private TextView theme_tv;
/**
* 邀请的选择文字
*/
private TextView contacts_tv;
/**
* 地点图标
*/
private TextView address_tv;
/**
* 地点的删除按钮
*/
private ImageView address_delete;
/**
* 下方照片选择布局
*/
private RelativeLayout photoContent;
/**
* 分享布局
*/
private View shareLayout;
/**
* 照片选择布局
*/
private View choosePhotoLayout;
/**
* 照片展示布局
*/
private LinearLayout photoShowLayout;
/**
* 选择照片
*/
private TextView choosePhoto;
/**
* 选择拍照
*/
private TextView chooseCamera;
/**
* 选择投票
*/
private TextView chooseChoose;
/**
* 放照片的存放布局
*/
private LinearLayout chooseBody;
/**
* 照片数量
*/
private TextView photoNumber;
/**
* i分享发布的请求对象
*/
private Ishare_releaseIshareRequest request;
// 当前的音频是否正在播放
private int isPlaying;
// 权限
private int purview;
// 按下语音按钮后出现的界面视图
private View popupWindowView;
// 录音中间显示的图片控件
private ImageView popupWindowImage;
// 弹出窗下面显示的文字
private TextView pop_tv;
// 第一次按下的
private float oldTouchY;
/**
* 发布的主题
*/
private String[] themeArray;
/**
* 发布邀请的人
*/
private ContactsInfo[] contactsArray;
/**
* 默认的录音文件保存路径
*/
public String mVideoPath = null;
/**
* 用于音频录制的对象
*/
private MediaRecorder mRecorder = null;
/**
* 用于音频播放的对象
*/
private MediaPlayer mPlayer = null;
/**
* 回调监听
*/
private publishMainListener listener;
/**
* 存放选择的照片布局的list
*/
private List<RelativeLayout> choosePhotoList = new ArrayList<RelativeLayout>();
/**
* 照片的列表
*/
private List<IsharePhotoInfo> photoList = new ArrayList<IsharePhotoInfo>();
// 底部弹出窗
private MenuBottomPop menuBottonPop;
private String[] menuStr = new String[] { "相机拍摄", "本地相册" };
/** 本次生成的路径 */
private String imagePath;
private BitmapManager bitmapManager;
/**
*
* @param activity
* @param fileList
*/
public IsharePublishMain(Activity activity,
List<IsharePhotoInfo> photoList, publishMainListener listener) {
super(activity);
this.activity = activity;
this.photoList = photoList;
this.listener = listener;
View.inflate(activity, R.layout.share_publish_main, this);
init();
}
/**
* 初始化
*/
private void init() {
bitmapManager = new BitmapManager();
initView();
shareLayout = photoContent.getChildAt(0);
choosePhotoLayout = photoContent.getChildAt(1);
shareLayout.setVisibility(View.GONE);
choosePhotoLayout.setVisibility(View.VISIBLE);
photoShowLayout.setVisibility(View.GONE);
// 初始化imageview数组
listImage[0] = iv1;
listImage[1] = iv2;
listImage[2] = iv3;
listImage[3] = iv4;
imageContent.setVisibility(View.GONE);
request = new Ishare_releaseIshareRequest();
popupWindowView = LayoutInflater.from(activity).inflate(
R.layout.imessage_chat_popupwindow, null);
popupWindowImage = (ImageView) popupWindowView
.findViewById(R.id.imessage_chat_popupwindow_image);
pop_tv = (TextView) popupWindowView.findViewById(R.id.voice_pop_tv);
}
/**
* 初始化控件
*/
private void initView() {
/**
* 所有的控件顶层
*/
relativeLayout = (RelativeLayout) this
.findViewById(R.id.ichoose_publish_main_rl);
// relativeLayout.setOnTouchListener(this);
/**
* 图片放置的布局
*/
imageContent = (RelativeLayout) this.findViewById(R.id.image_content);
/**
* 四张图片
*/
iv1 = (ImageView) this.findViewById(R.id.photo_iv1);
iv2 = (ImageView) this.findViewById(R.id.photo_iv2);
iv3 = (ImageView) this.findViewById(R.id.photo_iv3);
iv4 = (ImageView) this.findViewById(R.id.photo_iv4);
/**
* 单张的图片
*/
iv5 = (ImageView) this.findViewById(R.id.photo_iv5);
/**
* 文字内容输入框
*/
title_et = (EditText) this.findViewById(R.id.title);
/**
* 切换权限的按钮
*/
purview_tv = (TextView) this
.findViewById(R.id.ichoose_publish_purview_tv);
purview_tv.setOnClickListener(this);
/**
* 音频的布局
*/
voice_ll = (LinearLayout) this.findViewById(R.id.voice_ll);
/**
* 音频的文本
*/
voice_btn = (Button) this.findViewById(R.id.voice_btn);
voice_btn.setOnClickListener(this);
/**
* 音频的删除按钮
*/
voice_delete_btn = (Button) this.findViewById(R.id.voice_delete_btn);
voice_delete_btn.setOnClickListener(this);
/**
* 录音按钮的布局
*/
recording_ll = (LinearLayout) this.findViewById(R.id.recording_ll);
// recording_ll.setOnTouchListener(this);
/**
* 录音中间的文字
*/
recording_tv = (TextView) this.findViewById(R.id.recording_tv);
/**
* 主题的选择文字
*/
theme_tv = (TextView) this.findViewById(R.id.theme_tv);
theme_tv.setOnClickListener(this);
/**
* 邀请的选择文字
*/
contacts_tv = (TextView) this.findViewById(R.id.contacts);
contacts_tv.setOnClickListener(this);
/**
* 地点图标
*/
address_tv = (TextView) this.findViewById(R.id.address_tv);
address_tv.setOnClickListener(this);
/**
* 地点的删除按钮
*/
address_delete = (ImageView) this.findViewById(R.id.address_delete_iv);
address_delete.setOnClickListener(this);
/**
* 下方照片选择布局
*/
photoContent = (RelativeLayout) this.findViewById(R.id.photo_content);
/**
* 照片展示布局
*/
photoShowLayout = (LinearLayout) this
.findViewById(R.id.choose_photo_content);
/**
* 选择照片
*/
choosePhoto = (TextView) this
.findViewById(R.id.ishare_publish_photo_tv);
choosePhoto.setOnClickListener(this);
/**
* 选择拍照
*/
chooseCamera = (TextView) this
.findViewById(R.id.ishare_publish_camera_tv);
chooseCamera.setOnClickListener(this);
/**
* 选择投票
*/
chooseChoose = (TextView) this
.findViewById(R.id.ishare_publish_choose_tv);
chooseChoose.setOnClickListener(this);
/**
* 放照片的存放布局
*/
chooseBody = (LinearLayout) this.findViewById(R.id.choose_photo_body);
/**
* 照片数量
*/
photoNumber = (TextView) this.findViewById(R.id.photo_number);
this.findViewById(R.id.share_renren).setOnClickListener(this);
this.findViewById(R.id.image_content).setOnClickListener(this);
this.findViewById(R.id.link_tv).setOnClickListener(this);
}
/**
* 事件监听
*
* @param view
*/
public void onClick(View view) {
switch (view.getId()) {
case R.id.ichoose_publish_purview_tv:
// 权限按钮
setPurview();
break;
case R.id.voice_delete_btn:
// 音频删除按钮
voice_ll.setVisibility(View.GONE);
deleteSound();
break;
case R.id.voice_btn:
// 音频按钮
onPlay();
if (isPlaying == 0) {
voice_btn.setBackgroundResource(R.drawable.voice_bg_pressed);
isPlaying = 1;
} else if (isPlaying == 1) {
voice_btn.setBackgroundResource(R.drawable.voice_bg_normal);
isPlaying = 0;
}
break;
case R.id.theme_tv:
// 主题选择
Intent intent = new Intent(activity, IshareThemeActivity.class);
if (themeArray != null) {
intent.putExtra(IshareThemeActivity.THEME_ARRAY, themeArray);
}
activity.startActivityForResult(intent,
IshareThemeActivity.THEME_REQUEST_CODE);
break;
case R.id.contacts:
// 人脉选择
Intent intent2 = new Intent(activity, ContactsListActivity.class);
if (contactsArray != null) {
intent2.putExtra(ContactsListActivity.CONTACTS_ARRAY,
contactsArray);
}
intent2.putExtra(ContactsListActivity.PAGE_TYPE,
ActionUtil.Contacts.MULTIPLE);
intent2.putExtra(ContactsListActivity.USER_TYPE,
ActionUtil.Contacts.FRIEND);
activity.startActivityForResult(intent2,
ContactsListActivity.CONTACTS_REQUEST_CODE);
break;
case R.id.link_tv:
// 链接
Intent intent5 = new Intent(activity, IshareLinkActivity.class);
activity.startActivityForResult(intent5,
IshareLinkActivity.LINK_REQUEST_CODE);
break;
case R.id.share_renren:
pretendedData();
break;
case R.id.address_tv:
// 地图按钮
IntentHelper.goMapActivity(activity);
break;
case R.id.address_delete_iv:
// 地址删除按钮
address_tv.setText("");
address_delete.setVisibility(View.GONE);
request.setPublishedLatitude(0);
request.setPublishedLocation("");
request.setPublishedLongitude(0);
break;
case R.id.ishare_publish_photo_tv:
// 选择照片
Intent intent3 = new Intent(Intent.ACTION_GET_CONTENT, null);
intent3.setType("image/*");
activity.startActivityForResult(intent3, SELECT_PICTURE);
break;
case R.id.ishare_publish_camera_tv:
// 选择拍照
Intent intent4 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
imagePath = BitmapUtil.getImagePath();
Uri uri = Uri.fromFile(new File(imagePath));
intent4.putExtra(MediaStore.EXTRA_OUTPUT, uri);
activity.startActivityForResult(intent4, SELECT_CAMER);
break;
case R.id.ishare_publish_choose_tv:
// 选择投票
listener.toNext();
break;
case R.id.image_content:
// 照片区域点击
listener.toNext();
break;
default:
break;
}
}
/**
* 批量数据
*/
private void pretendedData() {
int[] userIdArray = new int[100];
for (int i = 0; i < 100; i++) {
userIdArray[i] = i;
}
AppContext context = (AppContext) activity.getApplication();
Contacts_setAttentionRequest request = new Contacts_setAttentionRequest();
request.setToUserId(userIdArray);
request.setFromUserId(context.getLoginUid());
context.getmDataProvider().getJsonFromNetwork(request, this);
}
/**
* 滑动监听
*
* @param v
* @param event
* @return
*/
public boolean onTouch(View v, MotionEvent event) {
if (v.getId() == R.id.recording_ll) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
popupWindowImage
.setImageResource(R.drawable.voice_pop_bg_normal);
pop_tv.setText("手指上滑,取消发送");
relativeLayout.addView(popupWindowView);
recording_ll
.setBackgroundResource(R.drawable.stroke_bottom_red);
Drawable left = activity.getResources().getDrawable(
R.drawable.ichoose_publish_speak_icon_pressed);
left.setBounds(0, 0, left.getMinimumWidth(),
left.getMinimumHeight());
recording_tv.setCompoundDrawables(left, null, null, null);
recording_tv.setTextColor(Color.WHITE);
startRecording();
} else if (event.getAction() == MotionEvent.ACTION_UP) {
relativeLayout.removeView(popupWindowView);
recording_ll.setBackgroundResource(R.drawable.stroke_bottom);
Drawable left = activity.getResources().getDrawable(
R.drawable.ichoose_publish_speak_icon_normal);
left.setBounds(0, 0, left.getMinimumWidth(),
left.getMinimumHeight());
recording_tv.setCompoundDrawables(left, null, null, null);
recording_tv.setTextColor(Color.GRAY);
stopRecording();
if (event.getY() > 0) {
prepare();
} else {
deleteSound();
}
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
if (event.getY() > 0) {
popupWindowImage
.setImageResource(R.drawable.voice_pop_bg_normal);
pop_tv.setText("手指上滑,取消发送");
} else {
popupWindowImage
.setImageResource(R.drawable.voice_pop_bg_delete);
pop_tv.setText("移动至此处取消");
}
}
} else if (v.getId() == R.id.ichoose_publish_main_rl) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
oldTouchY = event.getY();
} else if (event.getAction() == MotionEvent.ACTION_UP) {
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
if (oldTouchY - event.getY() > 100) {
if (recording_ll.getVisibility() == View.GONE) {
Animation hyperspaceJumpAnimation = AnimationUtils
.loadAnimation(activity, R.anim.push_bottom_in);
recording_ll.startAnimation(hyperspaceJumpAnimation);
recording_ll.setVisibility(View.VISIBLE);
}
}
if (event.getY() - oldTouchY > 100) {
if (recording_ll.getVisibility() == View.VISIBLE) {
Animation hyperspaceJumpAnimation = AnimationUtils
.loadAnimation(activity, R.anim.push_bottom_out);
recording_ll.startAnimation(hyperspaceJumpAnimation);
recording_ll.setVisibility(View.GONE);
}
}
}
}
return true;
}
/**
* 删除音频
*/
private void deleteSound() {
File file = new File(mVideoPath);
if (file.exists()) {
file.delete();
}
mVideoPath = null;
}
/**
* 设置权限
*/
private void setPurview() {
if (purview == 0) {
Drawable left = this.getResources().getDrawable(
R.drawable.ichoose_publish_justfriend);
left.setBounds(0, 0, left.getMinimumWidth(),
left.getMinimumHeight());
purview_tv.setCompoundDrawables(left, null, null, null);
purview_tv.setText("仅i好友");
purview = 1;
} else if (purview == 1) {
Drawable left = this.getResources().getDrawable(
R.drawable.ichoose_publish_public);
left.setBounds(0, 0, left.getMinimumWidth(),
left.getMinimumHeight());
purview_tv.setCompoundDrawables(left, null, null, null);
purview_tv.setText("公开");
purview = 0;
}
}
/**
* 地图点选
*/
public void setAddress(String str) {
request.setPublishedLocation(str);
if (str.length() > 10) {
str = str.substring(0, 10) + "…";
}
address_tv.setText(str);
address_delete.setVisibility(View.VISIBLE);
}
/**
* 设置主题
*
* @param str
*/
public void setTheme(String[] str) {
if (str.length != 0) {
String sum = "";
for (int i = 0; i < str.length; i++) {
if (i == str.length - 1) {
sum += str[i];
} else {
sum += str[i] + "、";
}
}
if (sum.length() > 5) {
sum = sum.substring(0, 5) + "…" + str.length + "个";
}
theme_tv.setText(sum);
} else {
theme_tv.setText("");
}
}
/**
* @param contacts_tv
* 要设置的 contacts_tv
*/
public void setContacts(String[] contacts) {
if (contacts.length != 0) {
String sum = "";
for (int i = 0; i < contacts.length; i++) {
if (i == contacts.length - 1) {
sum += contacts[i];
} else {
sum += contacts[i] + "、";
}
}
if (sum.length() > 5) {
sum = sum.substring(0, 5) + "…" + contacts.length + "人";
}
this.contacts_tv.setText(sum);
} else {
this.contacts_tv.setText("");
}
}
/**
* 设置经纬度
*
* @param latitude
* @param longitude
*/
public void setLatLon(double latitude, double longitude) {
request.setPublishedLatitude(latitude);
request.setPublishedLongitude(longitude);
}
/**
* 获取请求体
*
* @return
*/
public int getRequest() {
String title = title_et.getText().toString();
request.setEventContent(title);
// 设置公开范围
request.setEventPurview(purview);
//
if (contactsArray != null) {
int[] userArray = new int[contactsArray.length];
for (int i = 0; i < contactsArray.length; i++) {
userArray[i] = contactsArray[i].getUserId();
}
request.setEventMentionUserArray(userArray);
}
if (themeArray != null) {
request.setEventThemeArray(themeArray);
}
if (recording_ll.getVisibility() == View.VISIBLE) {
request.setEventVideoTime(Integer.parseInt(voice_btn.getText()
.toString()));
}
return 1;
}
/**
* 把link 链接添加到文本区域
*
* @param str
*/
public void appendUrl(String url) {
String str = "<a href=\"" + url + "\">" + url + "</a>";
title_et.append(Html.fromHtml(str));
}
/**
* 播放
*
* @param start
*/
private void onPlay() {
if (mPlayer != null && mPlayer.isPlaying()) {
mPlayer.pause();
} else {
try {
if (mPlayer == null) {
prepare();
}
mPlayer.start();
} catch (Exception e) {
LogUtils.i("prepare() failed");
}
}
}
/**
* 准备播放
*/
private void prepare() {
mPlayer = new MediaPlayer();
try {
mVideoPath = Environment.getExternalStorageDirectory()
.getAbsolutePath();
mVideoPath += "/audiorecorddemo.mp3";
mPlayer.setDataSource(mVideoPath);
mPlayer.prepare();
if ((mPlayer.getDuration() / 1000) < 1) {
deleteSound();
LogUtils.i("录制音频少于1秒,自动删除!!");
} else {
voice_ll.setVisibility(View.VISIBLE);
voice_btn.setText(mPlayer.getDuration() / 1000 + "");
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 开始录制
*/
private void startRecording() {
mVideoPath = Environment.getExternalStorageDirectory()
.getAbsolutePath();
mVideoPath += "/audiorecorddemo.mp3";
mRecorder = new MediaRecorder();
// 设置麦克风
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
// 设置输出文件格式
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setOutputFile(mVideoPath);
// 设置编码格式
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {
mRecorder.prepare();
} catch (IOException e) {
LogUtils.i("prepare() failed");
}
mRecorder.start();
}
/**
* 暂停录制
*/
private void stopRecording() {
mRecorder.stop();
mRecorder.release();
mRecorder = null;
}
public Ishare_releaseIshareRequest getReturnRequest() {
return getRequest() == 1 ? request : null;
}
/**
* @param themeArray
* 要设置的 themeArray
*/
public void setThemeArray(String[] themeArray) {
this.themeArray = themeArray;
}
/**
* @param contactsArray
* 要设置的 contactsArray
*/
public void setContactsArray(ContactsInfo[] contactsArray) {
this.contactsArray = contactsArray;
}
public void onNetworkRequest() {
}
public void onResultSuccess(NetworkEntity entity) {
}
public void onResultFail(String result) {
}
public MediaRecorder getRecorder() {
return this.mRecorder;
}
public MediaPlayer getPlayer() {
return this.mPlayer;
}
public void setRecorderRelease() {
this.mRecorder.release();
this.mRecorder = null;
}
public void setPlayerRelease() {
this.mPlayer.release();
this.mPlayer = null;
}
/**
* 设置照片数组
*
* @param list
*/
public void setPhotoArray(List<IsharePhotoInfo> list) {
photoList = list;
if (photoList == null) {
imageContent.setVisibility(View.GONE);
request.setEventType(0);
} else {
imageContent.setVisibility(View.VISIBLE);
request.setEventType(1);
// 初始化图片
int size = photoList.size();
if (size == 1) {
iv5.setVisibility(View.VISIBLE);
bitmapManager.loadBitmap(photoList.get(0).getPhotoUrl(), iv5);
} else {
iv5.setVisibility(View.GONE);
for (int i = 0; i < photoList.size(); i++) {
bitmapManager.loadBitmap(photoList.get(i).getPhotoUrl(),
listImage[i]);
}
}
}
shareLayout.setVisibility(View.VISIBLE);
choosePhotoLayout.setVisibility(View.GONE);
photoShowLayout.setVisibility(View.GONE);
}
/**
* 添加图片
*
* @param filePath
*/
public void addPhoto(final String filePath) {
final IsharePhotoInfo photoInfo = new IsharePhotoInfo();
photoInfo.setPhotoUrl(filePath);
shareLayout.setVisibility(View.GONE);
choosePhotoLayout.setVisibility(View.GONE);
photoShowLayout.setVisibility(View.VISIBLE);
final RelativeLayout layout = new RelativeLayout(activity);
ImageView iv = new ImageView(activity);
bitmapManager.loadBitmap(filePath, iv);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
iv.setScaleType(ScaleType.CENTER_CROP);
layout.addView(iv, params2);
ImageView close = new ImageView(activity);
close.setImageResource(R.drawable.iparty_publish_delete);
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params1.leftMargin = 5;
params1.topMargin = 5;
close.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
choosePhotoList.remove(layout);
photoList.remove(photoInfo);
showPhotoList();
}
});
layout.addView(close, params1);
photoList.add(photoInfo);
choosePhotoList.add(layout);
showPhotoList();
}
/**
* 显示选中列表的图片
*/
private void showPhotoList() {
DisplayMetrics dm = new DisplayMetrics();
dm = getResources().getDisplayMetrics();
LayoutParams param = new LayoutParams(dm.widthPixels / 3,
LayoutParams.FILL_PARENT);
param.rightMargin = 10;
chooseBody.removeAllViews();
int size = choosePhotoList.size();
if (size == 0) {
shareLayout.setVisibility(View.GONE);
choosePhotoLayout.setVisibility(View.VISIBLE);
photoShowLayout.setVisibility(View.GONE);
}
photoNumber.setText(size + "张照片");
for (int i = 0; i < size; i++) {
chooseBody.addView(choosePhotoList.get(i), param);
}
addChoosePhoto();
}
/**
* 添加最后的选择图片控件
*/
private void addChoosePhoto() {
DisplayMetrics dm = new DisplayMetrics();
dm = getResources().getDisplayMetrics();
RelativeLayout layout = new RelativeLayout(activity);
layout.setGravity(Gravity.CENTER);
layout.setBackgroundColor(Color.parseColor("#EF6565"));
ImageView iv = new ImageView(activity);
iv.setId(1);
iv.setImageResource(R.drawable.iparty_publish_add_photo_icon);
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params1.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
layout.addView(iv, params1);
TextView tv = new TextView(activity);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params2.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
params2.addRule(RelativeLayout.BELOW, 1);
tv.setText("添加照片");
tv.setTextColor(Color.WHITE);
tv.setTextSize(12);
layout.addView(tv, params2);
layout.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
showPopMenu(menuStr, menuClick);
}
});
LayoutParams param = new LayoutParams(dm.widthPixels / 3,
LayoutParams.FILL_PARENT);
param.rightMargin = 10;
chooseBody.addView(layout, param);
}
/**
* 选择照片的监听
*/
private OnClickListener menuClick = new OnClickListener() {
public void onClick(View v) {
menuBottonPop.dismiss();
if (v.getTag().equals(menuStr[0])) {
// 相机拍照
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
imagePath = BitmapUtil.getImagePath();
Uri uri = Uri.fromFile(new File(imagePath));
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
activity.startActivityForResult(intent, SELECT_CAMER);
} else if (v.getTag().equals(menuStr[1])) {
// 相册选取
Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
intent.setType("image/*");
activity.startActivityForResult(intent, SELECT_PICTURE);
}
}
};
/**
* 显示底部菜单
*/
private void showPopMenu(String[] str, OnClickListener menuClick) {
menuBottonPop = new MenuBottomPop(activity, str, menuClick);
menuBottonPop.showAtLocation(choosePhoto);
}
/**
* 获得本次拍照的保存路径
*
* @return
*/
public String getImagePath() {
return imagePath;
}
}