我正在开发一个应用程序,我可以将用户从设备图片库中选择的图像添加到imageview动态地添加到布局。当添加一个图像时,一切都很完美。当我尝试添加另一个图像时,第一个图像从布局中丢失。我不知道为什么会发生这种情况请给我解决方案。
当图库打开onStop方法被调用。这是否意味着重新创建了活动。 ?
这是onActivityResult()
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case SELECT_IMAGE:
if (resultCode == RESULT_OK) {
selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filepath = cursor.getString(columnIndex);
cursor.close();
yourSelectedImage = BitmapFactory.decodeFile(filepath);
if (yourSelectedImage != null) {
final DragImageView dynamicImgView = new DragImageView(
getApplicationContext(), yourSelectedImage);
final RelativeLayout.LayoutParams params = new
RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
dynamicImgView.setLayoutParams(params);
relativeLayout.addView(dynamicImgView);
}
}
break;
default:
break;
}
}
我动态创建Imageview的代码。
public class DragImageView extends ImageView{
private float mLastTouchX;
private float mLastTouchY;
private float mDeltaX;
private float mDeltaY;
private Bitmap bmpImg;
public DragImageView(Context context, Bitmap bmpImg) {
super(context);
this.bmpImg = bmpImg;
init();
// TODO Auto-generated constructor stub
}
public DragImageView(final Context context, final AttributeSet attrs){
super(context, attrs);
init();
}
private void init() {
// TODO Auto-generated method stub
setImageBitmap(bmpImg);
setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
final int action = event.getAction();
mLastTouchX = event.getRawX();
mLastTouchY = event.getRawY();
switch (action) {
case MotionEvent.ACTION_DOWN: {
RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) getLayoutParams();
mDeltaX = mLastTouchX - lParams.leftMargin;
mDeltaY = mLastTouchY - lParams.topMargin;
break;
}
case MotionEvent.ACTION_MOVE: {
mLastTouchX = event.getRawX();
mLastTouchY = event.getRawY();
final RelativeLayout.LayoutParams params = (LayoutParams) getLayoutParams();
params.leftMargin = (int) (mLastTouchX - mDeltaX);
params.topMargin = (int) (mLastTouchY - mDeltaY);
setLayoutParams(params);
break;
}
}
invalidate();
return true;
}
});
}
答案 0 :(得分:0)
我有解决方案..我已经检查了它并且它运行正常:)
调用相机像这样:
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, SELECT_IMAGE);
和ActivityResult将是这样的:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case SELECT_IMAGE:
if (resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
yourSelectedImage = (Bitmap) extras.get("data");
if (yourSelectedImage != null) {
final DragImageView dynamicImgView = new DragImageView(
getApplicationContext(), yourSelectedImage);
final RelativeLayout.LayoutParams params = new
RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
dynamicImgView.setLayoutParams(params);
relativeLayout.addView(dynamicImgView);
}
}
break;
default:
break;
}
}
答案 1 :(得分:0)
If
条件下的代码应该是这样的
if (yourSelectedImage != null) {
final DragImageView dynamicImgView = new DragImageView(
getApplicationContext(), yourSelectedImage);
final RelativeLayout.LayoutParams params = new
RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
if(relativeLayout.getChildCount()>0){
int lastChild = relativeLayout.getChildCount();
params.addRule(RelativeLayout.BELOW,relativeLayout.getChildAt(lastChild-1).getId());
}
dynamicImgView.setLayoutParams(params);
relativeLayout.addView(dynamicImgView);
}
答案 2 :(得分:0)
// Try this way,hope this will help you to solve your problem.
// define this variable for dynamic ImageView id in activity.
int imageViewId=1;
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case SELECT_IMAGE:
if (resultCode == RESULT_OK) {
selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filepath = cursor.getString(columnIndex);
cursor.close();
yourSelectedImage = BitmapFactory.decodeFile(filepath);
if (yourSelectedImage != null) {
final DragImageView dynamicImgView = new DragImageView(
getApplicationContext(), yourSelectedImage);
dynamicImgView.setId(imageViewId++);
final RelativeLayout.LayoutParams params = new
RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
if(imageViewId>1){
params.addRule(RelativeLayout.BELOW,imageViewId-1);
}
dynamicImgView.setLayoutParams(params);
relativeLayout.addView(dynamicImgView);
}
}
break;
default:
break;
}
}