1.设置签名类
public class MyHandWritePanel extends PopupWindow {
BaseActivity mContext;
HandWrite handView;
int availableHeight;
private Bitmap bitmap;
private int displayHeight,displayWidth;
public MyHandWritePanel(BaseActivity context) {
super(context, null);
this.mContext = context;
// Rect outRect = new Rect();
// view.getWindowVisibleDisplayFrame(outRect);
// availableHeight = outRect.height();
DisplayMetrics dm = new DisplayMetrics();
mContext.getWindowManager().getDefaultDisplay().getMetrics(dm);
displayHeight=dm.heightPixels;
displayWidth=dm.widthPixels;
availableHeight=displayHeight;
setWidth(displayWidth);
setHeight(availableHeight);
setFocusable(true);
setContentView(createContentView());
setAnimationStyle(R.style.popwindow_anim);
}
private ViewGroup createContentView() {
RelativeLayout out = new RelativeLayout(mContext);
out.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
out.setGravity(Gravity.CENTER);
out.addView(createTitle());
out.addView(createBody());
out.addView(createFoot());
return out;
}
private LinearLayout createTitle() {
RelativeLayout.LayoutParams rlayout = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
(int) mContext.getResources().getDimension(R.dimen.fontSize_30_dip));
rlayout.addRule(RelativeLayout.ALIGN_PARENT_TOP,
RelativeLayout.TRUE);
LinearLayout title = new LinearLayout(mContext);
title.setId(1);
title.setGravity(Gravity.CENTER);
title.setLayoutParams(rlayout);
TextView tv = new TextView(mContext);
tv.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
tv.setText("签名板");
tv.setTextColor(mContext.getResources().getColor(R.color.white));
tv.setTextSize(mContext.getResources().getDimension(R.dimen.fontSize_20));
tv.setGravity(Gravity.CENTER);
title.addView(tv);
return title;
}
private LinearLayout createBody() {
RelativeLayout.LayoutParams rlayout = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT, availableHeight
- ViewUtil.dip2px(mContext, mContext.getResources()
.getDimension(R.dimen.fontSize_20) * 4));
rlayout.addRule(RelativeLayout.BELOW, 1);
rlayout.addRule(RelativeLayout.ABOVE, 3);
LinearLayout title = new LinearLayout(mContext);
title.setId(2);
title.setLayoutParams(rlayout);
handView = new HandWrite(mContext, null, null, null,
displayWidth, availableHeight
- ViewUtil.dip2px(mContext, 80));
title.addView(handView);
return title;
}
private LinearLayout createFoot() {
RelativeLayout.LayoutParams rlayout = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT, ViewUtil.dip2px(
mContext, ViewUtil.dip2px(mContext, mContext.getResources()
.getDimension(R.dimen.fontSize_50))));
rlayout.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,
RelativeLayout.TRUE);
LinearLayout title = new LinearLayout(mContext);
title.setId(3);
title.setOrientation(LinearLayout.HORIZONTAL);
title.setLayoutParams(rlayout);
title.setGravity(Gravity.CENTER);
LinearLayout.LayoutParams layParam = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
layParam.setMargins(
0,
0,
ViewUtil.dip2px(mContext,
mContext.getResources().getDimension(R.dimen.fontSize_10)),
0);
Button confirmBtn = new Button(mContext);
confirmBtn.setLayoutParams(layParam);
confirmBtn.setText("确定");
confirmBtn.setTextSize(mContext.getResources().getDimension(R.dimen.fontSize_16));
confirmBtn.setBackgroundResource(R.drawable.comp_button_bottom);
confirmBtn.setTextColor(mContext.getResources().getColor(R.color.text_color_dark));
confirmBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bitmap = handView.getmBitmap();
if (bitmap != null)
afterSign.setImage(bitmap);
// scaledSignImage.setImageBitmap(bitmap);
MyHandWritePanel.this.dismiss();
}
});
Button clearBtn = new Button(mContext);
clearBtn.setText("清空");
clearBtn.setTextSize(mContext.getResources().getDimension(R.dimen.fontSize_16));
clearBtn.setLayoutParams(layParam);
clearBtn.setBackgroundResource(R.drawable.comp_button_bottom);
clearBtn.setTextColor(mContext.getResources().getColor(R.color.text_color_dark));
clearBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
handView.clear();
}
});
Button cancelBtn = new Button(mContext);
cancelBtn.setText("取消");
cancelBtn.setTextSize(mContext.getResources().getDimension(R.dimen.fontSize_16));
cancelBtn.setLayoutParams(layParam);
cancelBtn.setBackgroundResource(R.drawable.comp_button_bottom);
cancelBtn.setTextColor(mContext.getResources().getColor(R.color.text_color_dark));
cancelBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
afterSign.cancel();
MyHandWritePanel.this.dismiss();
}
});
title.addView(confirmBtn);
title.addView(clearBtn);
title.addView(cancelBtn);
return title;
}
private MyHandWritePanel.AfterSign afterSign = new MyHandWritePanel.AfterSign() {
@Override
public void setImage(Bitmap bitmap) {
// TODO Auto-generated method stub
}
@Override
public void cancel() {
// TODO Auto-generated method stub
}
};
public void setAfterSign(MyHandWritePanel.AfterSign afterSign) {
this.afterSign = afterSign;
}
public interface AfterSign{
public void setImage(Bitmap bitmap);
public void cancel();
}
}
2.mainActivity
private MyHandWritePanel handWritePanel;
private ImageView imagePanel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_choise_sign);
handWritePanel=new MyHandWritePanel(this);
imagePanel=(ImageView)findViewById(R.id.sign_panel_image);
btnComp=(Button)findViewById(R.id.sign_panel_complete);
handWritePanel.setAfterSign(new MyHandWritePanel.AfterSign() {
@Override
public void setImage(Bitmap bitmap) {
// TODO Auto-generated method stub
ImageUtil.saveMyBitmap(bitmap, AbsConstant.SDCARD_ROOT_PATH+AbsConstant.getCachePicPath(), SING_PIC_NAME);
image_local_path=AbsConstant.SDCARD_ROOT_PATH+AbsConstant.getCachePicPath()+File.separator+SING_PIC_NAME;
imagePanel.setImageBitmap(bitmap);
}
@Override
public void cancel() {
// TODO Auto-generated method stub
imagePanel.setImageResource(R.drawable.signature_default);
}
});
imagePanel.setOnClickListener(onClick);
btnComp.setOnClickListener(onClick);
checkSign.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){
handWritePanel.showAtLocation(imagePanel, Gravity.CENTER, 0, 0);
}
}
});
}