package com.jqyd.camera.library; import java.io.File; import java.lang.reflect.Array; import java.util.ArrayList; import java.util.List; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.app.ProgressDialog; import android.content.Context; import android.content.DialogInterface; import android.util.AttributeSet; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import com.example.camera.R; import com.example.camera.R.color; import com.jqyd.utils.PhotoDataItem; import com.jqyd.utils.PhotoItemFactory; import com.jqyd.utils.PhotoUtil; /** * 照片显示维护控件 * @author houweiliang */ public class PhotoDisplayBlock2 extends LinearLayout{ /** * <p>参数</p> */ public static interface Param{ /** * 初始图片 * @return List<PhotoData> */ public List<AccessoryDTO> initPhotos(); } /** * <p>照片添加按钮回调函数</p> */ public static interface AddPhotoButtonCallBack{ /** * 点击图片按钮的回调函数 * @param currentPhoto {@link PictureDTO}类型,对照片数据的封装 */ public void preform(AccessoryDTO currentPhoto); } /** * <p>照片修改按钮回调函数</p> */ public static interface UpdatePhotoButtonCallBack{ /** * 点击图片按钮的回调函数 * @param currentPhoto {@link PhotoData}类型,对照片数据的封装 */ public void preform(AccessoryDTO currentPhoto); } private LayoutParams wrap = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); /** * 颜色间间隔像素 */ private static final int MarginSpace = 2; private AccessoryDTO[] Photos; private AddPhotoButtonCallBack onAddPhotoCallBack; //private UpdatePhotoButtonCallBack onUpdatePhotoCallBack; private View innerView; private LinearLayout lPhotos; private Button btn_Add; public PhotoDisplayBlock2(Context context) { super(context); // TODO Auto-generated constructor stub //初始化控件界面 initUI(); } public PhotoDisplayBlock2(Context context, AttributeSet attrs){ super(context, attrs); //初始化控件界面 initUI(); } public PhotoDisplayBlock2(Context context, Param param){ super(context); //初始化控件参数 initPara(param); //初始化控件界面 initUI(); } /** * 初始化控件参数 * @param param */ private void initPara(Param param){ if(null == param){ throw new IllegalArgumentException("请创建该类的参数对象" ); } if(param.initPhotos()!=null && param.initPhotos().size() >0 ){ this.Photos = (AccessoryDTO[])Array.newInstance(AccessoryDTO.class, param.initPhotos().size()); this.Photos = param.initPhotos().toArray(this.Photos); }else{ this.Photos = new AccessoryDTO[0]; } } /** * 初始化控件界面 */ private void initUI(){ wrap.bottomMargin = MarginSpace; innerView = inflate(getContext(), R.layout.photos_display2, this); lPhotos = (LinearLayout)innerView.findViewById(R.id.lPhotos); btn_Add = (Button)innerView.findViewById(R.id.btn_add); //注册添加照片事件 regAddPhoto(); //初始化花色区域 initPhotos(Photos); } public void hideButton(){ btn_Add.setVisibility(View.INVISIBLE); } /** * 注册添加照片事件处理程序 */ private void regAddPhoto(){ btn_Add.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if(onAddPhotoCallBack!=null){ onAddPhotoCallBack.preform(null); } } }); } /** * 根据参数强制刷新控件界面,不能保证照片重复 * @param list */ private void initPhotos(AccessoryDTO[] Photos){ lPhotos.removeAllViews(); // if(Photos!=null&& Photos.length>0){ // System.out.println("Photos.length"+Photos.length); // System.out.println("Photos.1"+Photos[0].getName()); // System.out.println("Photos.last"+Photos[Photos.length-1].getName());} if(Photos!=null && Photos.length>0){ lPhotos.setPadding(3, 0, 0, 3); for(AccessoryDTO cd : Photos){ createPhotoDataItem(cd); } }else{ TextView txt=new TextView(getContext()); txt.setText("点击右侧【+】添加照片"); txt.setTextColor(color.gray); lPhotos.addView(txt); lPhotos.setPadding(10, 0, 0, 0); if(!btn_Add.isShown()){ btn_Add.setVisibility(View.VISIBLE); } } } /** * 根据参数创建一个新的照片块 * @param PhotoData */ public void createPhotoDataItem(final AccessoryDTO cd){ final PhotoDataItem ci = PhotoItemFactory.createPhotoDataItem(getContext(), cd); //ci.setBackgroundColor(Color.WHITE); ci.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_coloritem_down)); ci.getRootView().setOnClickListener(new OnClickListener(){ @Override public void onClick(View v){ showPhoto(cd); } }); lPhotos.addView(ci,wrap); } /** * 显示照片详情 * @param photoData */ private void showPhoto(final AccessoryDTO photoData){ // TODO Auto-generated method stub AlertDialog.Builder builder = new Builder(getContext()); View displayview = inflate(getContext(), R.layout.photo_detail, null); ImageView img_Display = (ImageView) displayview.findViewById(R.id.linColorBlockBackground); PhotoUtil.showPicture(getContext(),img_Display,photoData,300,300); //builder.setTitle("选择反馈方式"); builder.setView(displayview); // builder.setPositiveButton("编辑", new DialogInterface.OnClickListener() { // public void onClick(DialogInterface dialog, int which) { // dialog.dismiss(); // if(onUpdatePhotoCallBack!=null){ // onUpdatePhotoCallBack.preform(photoData); // } // } // }); builder.setNegativeButton("关闭", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }); builder.setNeutralButton("删除", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.cancel(); ProgressDialog mdialog=new ProgressDialog(getContext()); mdialog.setMessage("正在删除,请稍后..."); mdialog.show(); remove(photoData); PhotoUtil.photos.remove(photoData); File file=new File(photoData.getPath()); if(file.exists()){ file.delete(); } initPhotos(Photos); mdialog.dismiss(); mdialog.cancel(); } }); builder.create().show(); } /** * 刷新花色编辑区 * @param Colors */ public void RefreshColors(List<AccessoryDTO> Photos){ if(null != Photos){ this.Photos = (AccessoryDTO[])Array.newInstance(AccessoryDTO.class, Photos.size()); this.Photos = Photos.toArray(this.Photos); } initPhotos(this.Photos); } /** * 返回当前花色所有照片 * @return */ public List<AccessoryDTO> getPhotos(){ List<AccessoryDTO> list = new ArrayList<AccessoryDTO>(); if(null != this.Photos && this.Photos.length > 0){ for(AccessoryDTO dto : this.Photos){ list.add(dto); } } return list; } /** * 添加照片,并刷新显示控件 * @return */ public void addPhoto(AccessoryDTO photo){ this.Photos = arryCopyOf(this.Photos, new AccessoryDTO[this.Photos.length+1]); this.Photos[this.Photos.length-1] = photo; initPhotos(Photos); } // /** // * 设置更新照片回调函数 // * @param onUpdatePhotoCallBack // */ // public void setOnUpdatePhotoCallBack(UpdatePhotoButtonCallBack onUpdatePhotoCallBack) { // this.onUpdatePhotoCallBack = onUpdatePhotoCallBack; // } /** * 设置添加照片回调函数 * @param onAddPhotoCallBack */ public void setOnAddPhotoCallBack(AddPhotoButtonCallBack onAddPhotoCallBack) { this.onAddPhotoCallBack = onAddPhotoCallBack; } private <T> T[] arryCopyOf(T[] original, T[] newArray) { for (int i=0; i<original.length; i++) { newArray[i] = original[i]; } return newArray; } private void remove(AccessoryDTO dto){ if(null != dto && null != this.Photos && this.Photos.length > 0){ List<AccessoryDTO> list = new ArrayList<AccessoryDTO>(); for(AccessoryDTO d: this.Photos){ list.add(d); } list.remove(dto); if(list.size()>0){ this.Photos = (AccessoryDTO[])Array.newInstance(AccessoryDTO.class, list.size()); this.Photos = list.toArray(this.Photos); }else{ this.Photos = new AccessoryDTO[0]; } } } }